Obsidian Hook to New

This will add the note to the frontmost Obsidian vault. You can add the key vault=<YOUR VAULT NAME> to the URL call to have it go to a named vault (See Using obsidian URI tor details).

set tag to "from-Hook"
set linkToParent to "$user_link"

set title to "$title"
set encodedTitle to do shell script "python -c \"import urllib, sys; print (urllib.quote(sys.argv[1]))\" " & quoted form of title
set doubleEncodedTitle to do shell script "python -c \"import urllib, sys; print (urllib.quote(sys.argv[1]))\" " & quoted form of encodedTitle

set template to "---" & return & "tags: [" & tag & "]" & return & "---" & return & "# " & title & return & return & "Parent: [" & title & "](" & linkToParent & ")" & return & return
set encodedTemplate to do shell script "python -c \"import urllib, sys; print (urllib.quote(sys.argv[1]))\" " & quoted form of template

set obsidianURL to "obsidian://new?name=" & doubleEncodedTitle & "&content=" & encodedTemplate
set myScript to "open " & quoted form of obsidianURL
do shell script myScript

delay 0.2

-- presere clipboard as Obsidian will put the note's link there
set originalClipboard to the clipboard
set the clipboard to ""

set myScript to "open " & quoted form of "obsidian://hook-get-address"
do shell script myScript

delay 0.2

set childMarkdownLink to the clipboard
set the clipboard to originalClipboard

set AppleScript's text item delimiters to "]("
set theItems to every text item of childMarkdownLink
set AppleScript's text item delimiters to ""
set childLink to text 1 thru -2 of the last item of theItems

return childLink
3 Likes

Thank you! This works perfectly!

2 Likes

Wow, works like a charm. Thank you very much for sharing this.

Is there a way to specify a folder within obsidian? Right now it places it within the default folder for new notes assigned in the vault. I also did not understand how to specify the vault.

2 Likes

I figured it out!
replace this in the script:

set obsidianURL to "obsidian://new?name=Prime%20Vault&file=02%20Quick%20Notes%2FHook%20Notes%2F"

Here I have it set to create a new file in a vault called “Prime Vault” then into the folder “02 Quick Notes/Hook Notes”

On another note, How can I adjust the title to be the date and time in a Zettelkasten fashion. The reason is so that it creates a unique title. This is because changing the title after creating the file will break the hook link.

1 Like

Not quite — “&name=“ is the name of the new note. To set the vault it’s “&vault=Prime%20Vault”

1 Like

You can use date string of current date and time string of current date to put together an identifier to add to the start of the title variable.

https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_classes.html#//apple_ref/doc/uid/TP40000983-CH1g-DontLinkElementID_561

1 Like

Thank you so much. now it is working perfectly!

1 Like

Excellent! Thanks for figuring this out and making the script available here. All worked straight out of the box. Made a few minor tweaks to the YAML and the appearance of my Obsidian note – and started using this with great delight!

1 Like

@stevelw – Thank you for posting. This is great!

Perhaps I missed it, but is there a way I can specify a default folder in Obsidian for my Hook notes?

– Robert

set encodedFolderAndDoubleEncodedTitle to do shell script "python -c \"import urllib, sys; print (urllib.quote(sys.argv[1]))\" " & quoted form of (“My Hook Folder/“ & encodedTitle)

set obsidianURL to "obsidian://new?file=" & encodedFolderAndDoubleEncodedTitle & "&content=" & encodedTemplate