Skip to content

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): EventRef

Parameters:

ParameterTypeDescription
name"file-open"Should be 'file-open'.
callback(file: TFile | null) => unknownThe callback function.
ctx?unknownThe context passed as this to the callback function.

Returns: EventRef — The event reference.

Example:

app.workspace.on('file-open', (file) => {
console.log(file);
});