-
Notifications
You must be signed in to change notification settings - Fork 0
rename
rename.kmmacros
(with backups)
rename-nb.kmmacros
(without backups)
Renames a specified note and performs a find and replace operation on the content of all notes to update [[wikilinks]]
to the renamed note.
Assumes notes are wikilinked using full basenames (filenames without extensions) rather then UIDs. That is, [[Another Note]]
links to Another Note.txt
, without regard to the file extension. The chief benefit of a UID scheme is to obviate the need for a macro like this.
To leave the modification times of edited files as they were, uncomment the relevant lines in the invoked shell script. This might be desirable if, in your note-taking software, you sort your notes by modification time and want to see the notes you've been working on recently at the top, rather than the notes updated by this macro. Note that if The Archive is open while this macro is invoked, it won't recognise that the edited files have changed, and you may as a result end up overwriting what this macro has changed.
Links:
- Prompt the user for the note to be renamed, in an auto-complete list search.
- Prompt the user for the new title.
- Back up and rename the subject note.
- Back up all notes containing
[[Old Title]]
, and replace all instances of[[Old Title]]
with[[New Title]]
.
-
The macro should check that a note with the new title doesn't already exist; if does, it should abort.
-
The shell script compiling a list of all notes should use
find
ormdfind
rather thanls
, for the reasons described on Greg's Wiki: Bash Guide: Practices (under "5. Don't Ever Do These") and Why you shouldn't parse the output of ls(1).
Compile a list of all notes:
#!/usr/bin/env zsh -f
cd "$KMVAR_Instance_Notes_Directory"
print -rC1 -- **/*(N-om:t:r)
Rename the note and update [[wikilinks]]
to it, taking backups:
cd "$KMVAR_Instance_Notes_Directory"
# Create a backup directory.
backups="$KMVAR_Instance_Backup_Directory/$(date "+%Y-%m-%d, %H.%M") - Rename \"$KMVAR_Instance_Old_Title\" to \"$KMVAR_Instance_New_Title\""
mkdir "$backups"
# Identify the file to be renamed.
r=$(mdfind 'kMDItemFSName=="'"$KMVAR_Instance_Old_Title.*"'"' -onlyin .)
# Alternatively, using `find`:
# r=$(find . -name "$KMVAR_Instance_Old_Title.*")
# Back up the note to be renamed.
r_filename=${r##*/}
cp -p "$r" "$backups/$r_filename"
touch "$r"
mv "$r" "${r//$KMVAR_Instance_Old_Title/$KMVAR_Instance_New_Title}"
# Identify notes containing wikilinks to the renamed note.
grep -rl "\[\[$KMVAR_Instance_Old_Title\]\]" . | while read -r f ; do
# Back up the notes.
# Exclude the just-renamed note, which may include a wikilink to itself (such as in a YAML header).
f_basename=${f##*/}
f_basename=${f_basename%.*}
if [[ "$f_basename" != "$KMVAR_Instance_New_Title" ]]; then
cp -p "$f" "$backups/$f"
fi
# Update the wikilinks.
# Uncomment the commented lines in the following block if you prefer that the modification times of notes containing wikilinks are *not* updated. These lines save the modification timestamp against an empty temporary file. An alternative method would be to use `stat` to save the modification time to a variable.
# touch "$f.temp"
# touch -r "$f" "$f.temp"
old_escaped=$(sed 's/[^^]/[&]/g; s/\^/\\^/g' <<<"$KMVAR_Instance_Old_Title")
new_escaped=$(sed 's/[&/\]/\\&/g' <<<"$KMVAR_Instance_New_Title")
sed -i "" "s/\[\[$old_escaped\]\]/\[\[$new_escaped\]\]/g" "$f"
# touch -r "$f.temp" "$f"
# rm "$f.temp"
done
Version | Date | Changes |
---|---|---|
1.11 | 2020-07-26 | Escape regex metacharacters before feeding sed
|
1.10 | 2020-07-26 | Add success notifications |
1.09 | 2020-07-26 | Add missing shebangs; list all notes recursively |
1.08 | 2020-07-26 | Use zsh glob qualifiers |
1.07 | 2020-07-24 | Remove default triggers; use simpler macro names |
1.06 | 2020-07-23 | Fix syntax in backups file path; clean up variable names |
1.05 | 2020-07-23 | Fix quoting to account for spaces in KM variables |
1.04 | 2020-07-22 | Use mdfind in place of find
|
1.03 | 2020-07-09 | Add escapes to grep calls |
1.02 | 2020-07-08 | Update modification times by default |
1.01 | 2020-07-07 | Use instance rather than global variables |
1.00 | 2020-07-02 | Initial commit |