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
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.
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.
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!
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
I didn’t like the messy titles this produced, so I updated the script to encode the title (to make sure it’s file system safe) and then reinstate some common characters known to be file safe. You can easily add more replacement lines for other characters you don’t want encoded.
…
set encodedTitle to do shell script "python -c \"import urllib, sys; print (urllib.quote(sys.argv[1]))\" " & quoted form of title
set encodedTitleWithAllowedCharacters to findAndReplaceInText(encodedTitle, "%20", " ")
set encodedTitleWithAllowedCharacters to findAndReplaceInText(encodedTitleWithAllowedCharacters, "%E2%80%94", "—")
set doubleEncodedTitle to do shell script "python -c \"import urllib, sys; print (urllib.quote(sys.argv[1]))\" " & quoted form of encodedTitleWithAllowedCharacters
set template to "---" & return & "tags: [" & tag & "]" & return & "---" & return & "# " & title & return & return & "Parent: [" & title & "](" & linkToParent & ")" & return & return
…
return childLink
on findAndReplaceInText(theText, theSearchString, theReplacementString)
set AppleScript's text item delimiters to theSearchString
set theTextItems to every text item of theText
set AppleScript's text item delimiters to theReplacementString
set theText to theTextItems as string
set AppleScript's text item delimiters to ""
return theText
end findAndReplaceInText
Do you mind sharing the code for achieving this? I did not figure out how to convert the date string from Tuesday, August 24, 2021 to 20210824 and time string from 6:42:00 PM to 1842.
The complete script with the Zettelkasten suffix is as follows:
set tag to "from-Hook"
set linkToParent to "$user_link"
set theDate to (current date)
set theYearString to year of theDate as text
set theMonthString to (month of theDate as integer) as string
if (the length of theMonthString is 1) then set theMonthString to "0" & theMonthString
set theDayString to day of theDate as text
if (the length of theDayString is 1) then set theDayString to "0" & theDayString
set theHourString to hours of theDate as text
if (the length of theHourString is 1) then set theHourString to "0" & theHourString
set theMinuteString to minutes of theDate as text
if (the length of theMinuteString is 1) then set theMinuteString to "0" & theMinuteString
set theDateString to theYearString & theMonthString & theDayString & theHourString & theMinuteString
set title to "$title" & " - " & theDateString
set encodedTitle to do shell script "python -c \"import urllib, sys; print (urllib.quote(sys.argv[1]))\" " & quoted form of title
set encodedTitleWithAllowedCharacters to findAndReplaceInText(encodedTitle, "%20", " ")
set encodedTitleWithAllowedCharacters to findAndReplaceInText(encodedTitleWithAllowedCharacters, "%E2%80%94", "—")
set doubleEncodedTitle to do shell script "python -c \"import urllib, sys; print (urllib.quote(sys.argv[1]))\" " & quoted form of encodedTitleWithAllowedCharacters
set template to "---" & return & "title: " & title & return & return & "tags: ['#" & tag & "']" & return & return & "created at: '" & theDateString & "'" & return & "completed at: " & return & "modified at: " & return & "---" & return & return & "# " & title & return & return & "The following summary is mainly based on [" & title & "](" & linkToParent & ")." & return & return & "# Note" & return & 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
on findAndReplaceInText(theText, theSearchString, theReplacementString)
set AppleScript's text item delimiters to theSearchString
set theTextItems to every text item of theText
set AppleScript's text item delimiters to theReplacementString
set theText to theTextItems as string
set AppleScript's text item delimiters to ""
return theText
end findAndReplaceInText
The content of the generated note Obsidian Hook to New - Share your Hook scripts - Hook Productivity Forum - 202108241932.md is as follows:
---
title: Obsidian Hook to New - Share your Hook scripts - Hook Productivity Forum - 202108241932
tags: ['#from-Hook']
created at: '202108241932'
completed at:
modified at:
---
# Obsidian Hook to New - Share your Hook scripts - Hook Productivity Forum - 202108241932
The following summary is mainly based on [Obsidian Hook to New - Share your Hook scripts - Hook Productivity Forum - 202108241932](https://discourse.hookproductivity.com/t/obsidian-hook-to-new/3510/5).
# Note