Obsidian Transitional Scripts

If you are using Hook 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 Hook’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

Hook Productivity App Newsletter

Sign-up to receive emails about Hook productivity app. Our emails sometimes include coupons for discounts on great Hook-linkable apps made by other developers.





 Marketing permission: I give my consent to to be in touch with me via email using the information I have provided in this form for the purpose of news, updates and marketing.

What to expect: If you wish to withdraw your consent and stop hearing from us, simply click the unsubscribe link at the bottom of every email we send or contact us at support@cogsciapps.com. We value and respect your personal data and privacy. To view our privacy policy, please visit our website. By submitting this form, you agree that we may process your information in accordance with these terms.