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
1 Like

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
2 Likes

Thanks for this. Just had to figure out that Obsidian has to be opened for it to work!

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.

Considering the date string conversion method by @stevelw used in the following post: Add date to Fantastical title - Share your Hook scripts - Hook Productivity Forum, I have modified/combined the scripts provided also by @stevelw for achieving the desired result.

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



4 Likes