-
Notifications
You must be signed in to change notification settings - Fork 954
Create obsidian-rename-note-considiering-git.template.sh #1035
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| #!/bin/bash | ||
| #!/usr/local/bin/obsidian | ||
|
|
||
| # How to use this script? | ||
| # It's a template which needs further setup. Duplicate the file, | ||
| # remove `.template.` from the filename and set vaultPath to your Obsidian Vault. | ||
|
|
||
| # Note: Obsidian v0.8.15+ required | ||
| # Install via: 1) https://obsidian.md 2) brew install --cask obsidian | ||
|
|
||
|
|
||
| # Parameters | ||
|
|
||
| # Required parameters: | ||
| # @raycast.schemaVersion 1 | ||
| # @raycast.title Rename Obsidian-Note while keeping git-history | ||
| # @raycast.mode fullOutput | ||
|
|
||
| # Optional parameters: | ||
| # @raycast.argument1 { "type": "text", "placeholder": "New file name"} | ||
|
|
||
| # Documentation: | ||
| # @raycast.description Rename the Obsidian-Note currently sected in the frontmost finder window and stored in a Vault that is a git-repository by both updating links to the renamed note with the new name and keeping the git-history of the renamed note. | ||
| # @raycast.author JNBARY | ||
|
|
||
|
|
||
| # Configuration: | ||
| # Path to the Obsidian-Vault you want to act on: | ||
| vaultPath="/Users/JNBARY/Obsidian" | ||
|
|
||
|
|
||
| # Main program | ||
| cd $vaultPath | ||
|
|
||
| # check if Vault is a git-directory | ||
| if [ ! -d .git ]; | ||
| then | ||
| echo "Error: $vaultPath is no git-directory" | ||
| exit 1 | ||
| fi | ||
|
|
||
|
|
||
| # Get Finder selection | ||
| oldPath=$(osascript <<'EOF' | ||
| tell application "Finder" | ||
| return "" & (POSIX path of (selection as alias)) & "\n" | ||
| end tell | ||
| EOF | ||
| ) | ||
|
|
||
|
|
||
| newName="$1" | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. newName_withEnding="$1" |
||
|
|
||
| oldName_withEnding="${oldPath##*/}" | ||
| oldName="${oldName_withEnding%.md}" | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oldName="${oldName_withEnding%.*}" |
||
|
|
||
| oldVaultPath="${oldPath#$vaultPath/}" | ||
|
|
||
| newPath="${oldPath/$oldName/$newName}" | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. newName="${newName_withEnding%.*}" |
||
|
|
||
| newVaultPath="${newPath#$vaultPath/}" | ||
|
|
||
|
|
||
| if [ ! -f "$oldPath" ]; | ||
| then | ||
| echo "Error: File does not exist: $oldPath" | ||
| exit 1 | ||
| fi | ||
|
|
||
|
|
||
| # rename file via Obsidian CLI to change links in Obsidian Notes | ||
| echo -e "Renaming file:"$oldName" via Obsidian CLI to update links...\n" | ||
| obsidian move file="$oldName" path="$oldVaultPath" to="$newVaultPath" | ||
|
|
||
|
|
||
| if [ ! -f "$newPath" ]; then | ||
| echo "Error: Obsidian CLI rename failed." | ||
| exit 1 | ||
| fi | ||
|
|
||
|
|
||
| # reset to old filename via mv - does not affect changed links in Obsidian Notes | ||
| echo -e "\nRestoring original filename via mv...\n" | ||
| mv "$newPath" "$oldPath" | ||
|
|
||
|
|
||
| if [ ! -f "$oldPath" ]; then | ||
| echo "Error: mv restore failed." | ||
| exit 1 | ||
| fi | ||
|
|
||
|
|
||
| # rename file via git mv to keep file history | ||
| echo -e "Renaming file via git mv...\n" | ||
| git mv "$oldPath" "$newPath" | ||
|
|
||
|
|
||
| echo "Rename completed." | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.