I want to extend the functionality of the Daily Notes core plugin, but in order to do what I want, I need to know the values of the “Date format” and “New file location” settings. How do I access settings values from other plugins?
The only thing I’ve found that’s even close is a question about accessing global settings but there’s only one reply and the link in it is broken. Help?
You can get Daily Notes plugin instance using this:
Disclaimer: This is not in public API, so it might be changed anytime.
// Assume "this" is your "Plugin" instance
/*
Different between "Plugin" and "PluginInstance" in the case of internal plugin.
The actual configuration is stored in the "PluginInstance" instance.
There are two ways to get the access:
- get the "Plugin" instance through "getPluginById" then go to "plugin.instance"
field, since "getPluginById" returns "Plugin" instance,
- directly use "getEnabledPluginById" method.
*/
// Will return null if it's disabled.
let dailyNotesPluginInstance = this.app.internalPlugins.getEnabledPluginById("daily-notes");
// Always return Daily Notes' "Plugin" instance
dailyNotesPluginInstance = this.app.internalPlugins.getPluginById("daily-notes").instance;
// Access its configuration
let dailyNotesPluginOptions = dailyNotesPluginInstance.options;
// Will return default date format if "Date format" field was empty
let dateFormat = dailyNotesPluginInstance.getFormat();
1 Like
Thank you! I can get the object for the Daily Notes plugin, but retrieving the format logs an error: Uncaught (in promise) TypeError: dailyNotesPlugin.getFormat is not a function
. So if that works for you, I have no idea what’s going on with mine.
I experimented a little and got this far:
let dailyNotesPluginOptions = dailyNotesPlugin.instance.options;
let dateFormat = dailyNotesPluginOptions.dateFormat;
which doesn’t throw an error, but dateFormat
is undefined
.
Any ideas?
Apologize, I made a mistake here. getFormat
method belongs to the Daily Notes’ PluginInstance
instance , which can be accessed through dailyNotesPlugin.instance
.
Therefore, we should use dailyNotesPlugin.instance.getFormat()
.
That means “Date format” is still blank in the settings. You would like to use getFormat()
instead.
I’ve made some changes to my previous reply above. You can read it again.
It works! Thank you so much. Two follow up questions:
- I also need the value of the “New File Location” setting. I’ve tried a few guesses, but no luck. How/where can I find the name of that function as well?
- I notice that the line
let dailyNotesPluginOptions = dailyNotesPluginInstance.options;
isn’t used, and things appear to work without it. Is there a reason you included it that I’m missing?
dailyNotesPluginInstance.options;
This field is used to access Daily Notes configuration. The interface should look like this:
interface DailyNotesOptions {
autorun?: boolean; // Open daily note on startup
folder?: string; // New file location
format?: string; // Date format
template?: string; // Template file location
}
Each option may be undefined
, that means it still uses the default value (either empty or false), and the user have not changed it yet.
If you only need the date format, you don’t need it (use getFormat()
instead). If you want to access other configuration, like “New file location”, you would need it.
Is there any way to get the types for smth that isn’t part of the public API so it works better with typescript?