Obsidian Transitional Scripts

If you are using Hookmark public beta 2 and would like to revert your Obsidian scripts to something compatible with pre-Obsidian-v0.14.5, you can use the following in Hookmark’s Integration Script Preferences:

-- to use Obsidian's advanced URL script, type this at the command line
-- defaults write com.cogsciapps.hook integration.obsidian.URL.scheme obsidian-advanced-URI
-- https://hookproductivity.com/help/integration/using-hook-with-obsidian

set prefUrl to ""
try
    set prefUrl to (do shell script "defaults read com.cogsciapps.hook integration.obsidian.URL.scheme")

on error errMsg
end try

if prefUrl is not missing value and prefUrl is not "" and prefUrl is not "obsidian-default" then

    if prefUrl is "obsidian-advanced-URI" then
        set urlKey to "advanceduri"
    else if prefUrl is "hook-file" then
        set urlKey to "fileuri"
        -- An invalid value for com.cogsciapps.hook integration.obsidian.URL.scheme  has been set. There, we present the following options and set the default here.
    else
        set thePrefChoices to {"obsidian-default (obsidian://)", "obsidian-advanced-URI (obsidian://advanced-uri)", "hook-file (hook://file/)"}
        set thePrefChoice to choose from list thePrefChoices with prompt "Please select one of the following URL schemes with which to interact with Obsidian:" default items {"obsidian-default (obsidian://)"}
        if thePrefChoice is not false then
            set x to thePrefChoice as text
            set AppleScript's text item delimiters to {" "}
            set urlKey to text item 1 of x

            do shell script "defaults write com.cogsciapps.hook integration.obsidian.URL.scheme  " & urlKey
        else
            return
        end if

    end if


    set callbackURL to "hook://x-callback-url/setCurrentNode%3FurlKey%3D" & urlKey & "%26plusencoded%3Dyes"
    set callbackURLError to "hook://x-callback-url/setCurrentNodeError"


    set myURL to "obsidian://hook-get-advanced-uri?" & "&x-error=" & callbackURLError & "&x-success=" & callbackURL

    set myScript to "open '" & myURL & "'"

    do shell script myScript
    return "hook://x-callback-url/setCurrentNode"

else

    set myURL to "obsidian://hook-get-address"
    set myScript to "open '" & myURL & "'"
    do shell script myScript
    delay 0.4
    repeat 50 times -- poll clipboard for ~2.5 seconds
        try
            if (the clipboard) is not equal to "" then
                exit repeat
            end if
        end try
        delay 0.05
    end repeat
    get the clipboard
end if

New Item script

use framework "Foundation"
use scripting additions


property NSString : a reference to current application's NSString
property NSMutableCharacterSet : a reference to current application's NSMutableCharacterSet

set fileType to ".md"

set prefUrl to ""
try
    set prefUrl to (do shell script "defaults read com.cogsciapps.hook integration.obsidian.URL.scheme")

on error errMsg
end try

if prefUrl is not missing value and prefUrl is not "" and prefUrl is not "obsidian-default" then

    if prefUrl is "obsidian-advanced-URI" then
        set urlKey to "advanceduri"
    else if prefUrl is "hook-file" then
        set urlKey to "fileuri"
        -- An invalid value for com.cogsciapps.hook integration.obsidian.URL.scheme  has been set. There, we present the following options and set the default here.
    else
        set thePrefChoices to {"obsidian-default (obsidian://)", "obsidian-advanced-URI (obsidian://advanced-uri)", "hook-file (hook://file/)"}
        set thePrefChoice to choose from list thePrefChoices with prompt "Please select one of the following URL schemes with which to interact with Obsidian:" default items {"obsidian-default (obsidian://)"}
        if thePrefChoice is not false then
            set x to thePrefChoice as text
            set AppleScript's text item delimiters to {" "}
            set urlKey to text item 1 of x

            do shell script "defaults write com.cogsciapps.hook integration.obsidian.URL.scheme  " & urlKey
        else
            return
        end if

    end if

    if urlKey is "obsidian-default" then
        display dialog "The Hook to New > Obsidian command is only available when the Obsidian Advanced URI community plugin is installed (https://github.com/Vinzent03/obsidian-advanced-uri). And it requires the URL scheme to be set to obsidian-advanced-URI or hook-file. Please see https://hookproductivity.com/help/integration/using-hook-with-obsidian#advancedURI for more information"
        return
    end if



    set callbackURL to "hook://x-callback-url/link-to-new"
    set encodedSrc to "$encoded_link"
    set callbackURLError to "hook://x-callback-url/error"



    set encodedTitle to "$encoded_title"
    set encodedLink to "$user_link"


    if encodedTitle ends with fileType then
        set fileType to ""
    end if

    set theString to NSString's stringWithString:encodedLink
    set charset to NSMutableCharacterSet's URLQueryAllowedCharacterSet's mutableCopy
    charset's removeCharactersInString:"&=?"
    set encodedLink to theString's stringByAddingPercentEncodingWithAllowedCharacters:charset

    set theString to NSString's stringWithString:encodedTitle


    --remove / \ : because Obsidian would not create a file if the file name contains those characters
    set theString to theString's stringByReplacingOccurrencesOfString:"/" withString:""
    set theString to theString's stringByReplacingOccurrencesOfString:"%5C" withString:""
    set theString to theString's stringByReplacingOccurrencesOfString:":" withString:""


    set encodedTitle to theString's stringByAddingPercentEncodingWithAllowedCharacters:charset


    set callbackURL to callbackURL & "?src=" & encodedSrc & "&urlKey=" & urlKey & "&plusencoded=yes"


    set theString to NSString's stringWithString:callbackURL

    set callbackURL to theString's stringByAddingPercentEncodingWithAllowedCharacters:charset




    set myURL to "obsidian://advanced-uri?filename=" & encodedTitle & fileType & "&data=[" & encodedTitle & "](" & encodedLink & ")&mode=new&x-success=" & callbackURL & "&x-error=" & callbackURLError
    set myScript to "open " & quoted form of myURL

    do shell script myScript

    get "hook://link-to-new"
else

    display dialog "The Hook to New > Obsidian command is only available when the Obsidian Advanced URI community plugin is installed (https://github.com/Vinzent03/obsidian-advanced-uri). And it requires the URL scheme to be set to obsidian-advanced-URI or hook-file. Please see https://hookproductivity.com/help/integration/using-hook-with-obsidian#advancedURI for more information"

end if