Forked from TfTHacker/ArchiveAndProcessNextFile.js
Last active Jan 22, 2022
Obsidian: Archive current file and then open next file in folder (Templater script)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <%* | |
| /* | |
| 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 | |
| */ | |
| // 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