Last active
March 7, 2024 12:11
-
-
Save stracker-phil/814960afa93e414ad95769aabe6e4d1a to your computer and use it in GitHub Desktop.
Hookmark script: Obsidian | New Item
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
-- Fix Invalid "prefUrl" value | |
-- (My personal preference is "hook-file", so this is my default) | |
set validChoices to {"hook-file", "obsidian-default", "obsidian-advanced-URI"} | |
if prefUrl is not in validChoices then | |
set prefUrl to "hook-file" | |
do shell script "defaults write com.cogsciapps.hook integration.obsidian.URL.scheme " & prefUrl | |
end if | |
set schemaSuccess to "hook://x-callback-url/link-to-new" | |
set schemaError to "hook://x-callback-url/error" | |
set charset to NSMutableCharacterSet's URLQueryAllowedCharacterSet's mutableCopyWithZone:(missing value) | |
charset's removeCharactersInString:"&=?" | |
set fileName to "$title" | |
set hookLink to "$encoded_link" | |
set encodedLink to "$user_link" | |
set fileNameTemp to NSString's stringWithString:fileName | |
--Remove / \ : | ^ as those cause issues with Obsidian. | |
set fileNameTemp to fileNameTemp's stringByReplacingOccurrencesOfString:"/" withString:"" | |
set fileNameTemp to fileNameTemp's stringByReplacingOccurrencesOfString:":" withString:"" | |
set fileNameTemp to fileNameTemp's stringByReplacingOccurrencesOfString:"%5C" withString:"" | |
set fileNameTemp to fileNameTemp's stringByReplacingOccurrencesOfString:"%5E" withString:"" | |
set fileNameTemp to fileNameTemp's stringByReplacingOccurrencesOfString:"%7C" withString:"" | |
set cleanFileName to fileNameTemp as string | |
if fileName ends with fileType then | |
set fileType to "" | |
end if | |
-- Build the default note content (a link to the original file) | |
set backLinkTemp to NSString's stringWithString:encodedLink | |
set backLink to backLinkTemp's stringByAddingPercentEncodingWithAllowedCharacters:charset | |
set noteContent to "[" & cleanFileName & "](" & backLink & ")" | |
set urlKey to "" | |
-- Schema: Advanced-URI | |
if prefUrl is "obsidian-advanced-URI" then | |
set urlKey to "advanceduri" | |
set urlSuccess to schemaSuccess & "?plusencoded=yes&src=" & hookLink | |
set createUrl to "obsidian://advanced-uri?filename=" & cleanFileName & fileType & "&data=" & noteContent | |
else | |
if prefUrl is "hook-file" then | |
set urlKey to "file" | |
end if | |
set urlSuccess to schemaSuccess & "?plusencoded=yes&titleKey=name&src=" & hookLink | |
set createUrl to "obsidian://new?name=" & cleanFileName & fileType & "&content=" & noteContent | |
end if | |
if urlKey is not "" then | |
set urlSuccess to urlSuccess & "&urlKey=" & urlKey | |
end if | |
set urlSuccessTemp to NSString's stringWithString:urlSuccess | |
set urlSuccess to urlSuccessTemp's stringByAddingPercentEncodingWithAllowedCharacters:charset | |
set fullUrl to createUrl & "&x-success=" & urlSuccess & "&x-error=" & schemaError | |
-- Create the Obsidian note | |
set myScript to "open " & quoted form of fullUrl | |
do shell script myScript | |
-- Focus Obsidian, so we can start writing without first clicking into the note | |
tell application "Obsidian" to activate | |
-- Hook the Obsidian note to the file | |
return "obsidian://hook-get-address" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NB: The last line is important to correctly hook the .md file with the original document.