Get the sorted file list via JS API #167
-
Hi, I'm trying to write QuickAdd macros for switching to the previous/next file in the file tree, my current script looks like this: Code// inbox-next.js
const folderPath = "inbox/";
const getNextFile = () => {
const files = app.vault
.getFiles()
.filter((file) => file.path.startsWith(folderPath));
files.sort((a, b) => b.path.localeCompare(a.path));
const activeFile = app.workspace.getActiveFile();
if (activeFile == null) return files[0];
const activeFileIndex = files.findIndex((file) => file.path === activeFile.path);
if (activeFileIndex === -1) return files[0];
const nextFileIndex = (activeFileIndex + 1) % files.length;
return files[nextFileIndex];
}
module.exports = async () => {
const nextFile = getNextFile();
const leaf = app.workspace.getUnpinnedLeaf();
leaf.openFile(nextFile);
}; This is easy to do within a specific folder ( #159 seems related, but I don't think my request requires any dependencies on internal data model of File Explorer as it's essentially just a function that accepts a file list and a sortspec and returns a new file list sorted accordingly (though I'm not at all familiar with Obsidian internals, so I might be wrong). Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
the relevant API function has been already there, intentionally, yet not exposed as public. Scan The function takes as parameter a The result of the function is exactly the same order which user sees in the File Explorer for the supplied folder.
Let me know if you can access the Long term, this function can be explicitly exposed, plus a *.d.ts definition file to copy & paste. Plus some more defensive coding to make it safer for external callers. |
Beta Was this translation helpful? Give feedback.
Hi @garlicbreadcleric
the relevant API function has been already there, intentionally, yet not exposed as public.
Scan
main.ts
file of the plugin for member functionorderedFolderItemsForBookmarking(...)
The function takes as parameter a
TFolder
object, works in the context of current state of Obsidian and the custom-sort plugin to determine the sorting for the folder (standard UI-selected or custom), performs the sorting and returns ordered list ofTAbstractFile
items (that meansTFolder
andTFile
instances)The result of the function is exactly the same order which user sees in the File Explorer for the supplied folder.
As simple as it sounds, there are the non-obvious things: