Workspace.on('file-open') method
Workspace › on(‘file-open’)
Official
Triggered when the active file changes. The file could be in a new leaf, an existing leaf, or an embed.Despite its name, this is a "focused file changed" event, not a "file was opened" one. It fires whenever Workspace.getActiveFile returns a different file than it did last time, so moving the focus to another leaf that already shows another file fires it too, while opening the file that is already active does not. The
file is null when the newly active leaf shows no file.Signature:
on(name: "file-open", callback: (file: TFile | null) => unknown, ctx?: unknown): EventRefParameters:
| Parameter | Type | Description |
|---|---|---|
| name | "file-open" | Should be 'file-open'. |
| callback | (file: TFile | null) => unknown | The callback function. |
| ctx? | unknown | The context passed as this to the callback function. |
Returns: EventRef — The event reference.
Example:
app.workspace.on('file-open', (file) => { console.log(file);});