Skip to content

Instantly share code, notes, and snippets.

@thomasvs
Forked from TfTHacker/ArchiveAndProcessNextFile.js
Last active April 23, 2022 21:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomasvs/343c740825407f0a84f350e4e40bedd0 to your computer and use it in GitHub Desktop.
Save thomasvs/343c740825407f0a84f350e4e40bedd0 to your computer and use it in GitHub Desktop.
Obsidian: Archive current file and then open next file in folder (Templater script)
<%*
/*
Updated: 1/22/2022
Author: TfTHacker & thomasvs
Gist: https://gist.github.com/TfTHacker & https://gist.github.com/thomasvs/343c740825407f0a84f350e4e40bedd0
Twitter: https://twitter.com/TfTHacker & https://twitter.com/thomasvs
Requirements: Templater Plugin for Obsidian
Description: This script performs the following actions:
1. Moves current file using the file explorer's move feature
2. Opens next file in current folder into the active window pane in Obsidian
Usage: 1. install and enable the Templater plugin
2. drop this file with an .md extension in the Templater template directory
3. install and enable the Hotkeys for templates plugin
4. in its settings, activate the hotkey for MoveAndProcessNextFile
5. configure Hotkeys in settings; assign Ctrl-Alt-M to Hotkeys for templates: Insert from Templater: MoveAndProcessNextFile
*/
// get a sorted list of file names in the current path
// FIXME: is there a way to do this inside the function initiating the rename and pass it to the rename callback?
let currentFolderPath = tp.file.folder(true);
let currentFilePath = (currentFolderPath == '/' ? '' : currentFolderPath + '/') + tp.file.title + '.md';
let sortFileList = (await app.vault.adapter.list(currentFolderPath)).files.sort( (a, b) => a.localeCompare(b));
// FIXME: I don't fully understand the execution model, where to store state, and how to unregister a callback, so using
// this global variable to store the event callback ref so it only executes once.
let renameref = null;
// FUNCTION: event callback for 'rename' event
const renamed = (event) => {
//console.log(event);
console.log('renamed file with name ' + event.name);
advance(event.name);
if (renameref) this.app.vault.offref(renameref);
}
// set up rename callback and trigger the move file dialog
// FIXME: ideally, we'd pass the current file we expect to be renamed
renameref = this.app.vault.on('rename', renamed);
this.app.commands.executeCommandById('file-explorer:move-file');
// FUNCTION: advance the file explorer to the next file in the list
const advance = (name) => {
let currentFileIndexInCurrentFolder = sortFileList.findIndex(e => e == currentFilePath);
if (sortFileList.length > 1) {
// open next file in folder, if one exists
let nextFileToOpen = '';
if (currentFileIndexInCurrentFolder == sortFileList.length - 1)
// last file, so roll over to start of list
nextFileToOpen = sortFileList[0];
else
nextFileToOpen = sortFileList[currentFileIndexInCurrentFolder + 1];
console.log('Advancing explorer from ' + currentFolderPath + '/' + name + ' to ' + nextFileToOpen);
app.workspace.activeLeaf.openFile(app.vault.getAbstractFileByPath(nextFileToOpen));
}
}
%>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment