File-Menu when Selecting Multiple Files at Once?

I’m currently developing a plugin that performs actions on all files within a folder. While that part is working fine, I was curious about being able to apply that same action when multi-selecting files, so they don’t have to be in a folder.

The function I use is

this.app.workspace.on("file-menu", (menu, file, source) => {
	if (file instanceof TFolder) {
             //add menu 
		});
	}
})

Initially I wanted to see if file may turn into an array when selecting multiple files, so I used console.log({file}) before my if statement to see what would happen. To my surprise, console.log did not run when I right-clicked on a selection of multiple files. Why is this? Does the file-menu event not run when right-clicking on a selection?

1 Like

Any update here? I would also be interested in this.

@FEZ & @ahackel

There is an documented function for this:

// @ts-ignore (files-menu event exists)
this.app.workspace.on("files-menu", (menu: menu, files: TFile[]) => {
	// ...
});
1 Like

We should probably add that to the public API… good point.

1 Like

My apologies, I got my answer in the Obsidian Discord and never updated this.

For anyone else who comes across this, I used the function that Fevol posted. I’ve also made 2 plugins with this, for mass-adding tags and mass-adding properties.