Using AppleScript and two handlers (one for JSON Parsing and other for URL encoding, both from stackoverflow), I’ve managed to make a version that will work following the same @technicalpickles specifications for getting the URL.
As a known but very likely workable limitation, I couldn’t make the script work properly with multiple vaults, and thus there’s a segment still in progress. Somebody with better AppleScript knowledge could possibly make a better job here.
This is the Get URL
script:
# GOAL
-- Fetches an Obsidian URI link trough AppleScript on the background
-- Known limitations: currently works only with one vault
# WORKFLOW
-- Step 0: replace selected variables with your own data
-- Step 1A: gets last vault from Obsidian JSON
-- Step 1B: gets last file relative path via parsing of JSON
-- Step 2: encodes the relative path to URL format
-- Step 3: generates obsidian link
# HANDLERS
-- Uses a JSONtoRecord handler from user CJK posted at Stackoverflow (https://stackoverflow.com/a/51623993/13936032)
use framework "Foundation"
use scripting additions
--------------------------------------------------------------------------------
property ca : a reference to current application
property NSData : a reference to ca's NSData
property NSDictionary : a reference to ca's NSDictionary
property NSJSONSerialization : a reference to ca's NSJSONSerialization
property NSString : a reference to ca's NSString
property NSUTF8StringEncoding : a reference to 4
--------------------------------------------------------------------------------
on JSONtoRecord from fp
local fp
set JSONdata to NSData's dataWithContentsOfFile:fp
set [x, E] to (NSJSONSerialization's ¬
JSONObjectWithData:JSONdata ¬
options:0 ¬
|error|:(reference))
if E ≠ missing value then error E
tell x to if its isKindOfClass:NSDictionary then ¬
return it as record
x as list
end JSONtoRecord
--------------------------------------------------------------------------------
-- This second handler encodes the URL format without errors
on urlEncode(input)
tell current application's NSString to set rawUrl to stringWithString_(input)
set theEncodedURL to rawUrl's stringByAddingPercentEscapesUsingEncoding:4 -- 4 is NSUTF8StringEncoding
return theEncodedURL as Unicode text
end urlEncode
# START OF THE STEPS
(*
# IN PROGRESS - STEP 1A - Gets lastVault Name and Path data from obsidian.json at home folder
-- Gets the last vault JSON info
set home to the path to home folder
set v to the POSIX path of home & "Library/Application Support/obsidian/obsidian.json"
set JSONVaultRecord to JSONtoRecord from v
set lastVaultID to last_open of JSONVaultRecord
set lastVault to lastVaultID of vaults of JSONVaultRecord
set vaultPath to |path| of lastVault
*)
# STEP 1B - Gets user inputted vault Name and Path
set vaultName to "Obsidian"
set vaultPOSIXPath to "/Users/xxx/Documents/Obsdian/"
# STEP 2 - Gets relative path to lastOpenFile
-- Gets the workspace JSON file
set f to vaultPOSIXPath & ".obsidian/workspace"
set JSONFilesRecord to JSONtoRecord from f
set lastOpenFile to item 1 of lastOpenFiles of JSONFilesRecord
# STEP 3 - Encodes the relative path to a URL-friendly format
set encodedRelativePath to urlEncode(lastOpenFile)
# STEP 4 - Mounts the URI scheme link from variables vaultName and encodedRelativePath
set obsidianURL to "obsidian://open?vault=" & vaultName & "&file=" & encodedRelativePath
return obsidianURL
This is the Get Name
script:
# GOAL
-- Fetches an Obsidian URI link trough AppleScript on the background
-- Known limitations: currently works only with one vault
# WORKFLOW
-- Step 0: replace selected variables with your own data
-- Step 1A: gets last vault from Obsidian JSON
-- Step 1B: gets last file relative path via parsing of JSON
-- Step 2: encodes the relative path to URL format
-- Step 3: generates obsidian link
# HANDLERS
-- Uses a JSONtoRecord handler from user CJK posted at Stackoverflow (https://stackoverflow.com/a/51623993/13936032)
use framework "Foundation"
use scripting additions
--------------------------------------------------------------------------------
property ca : a reference to current application
property NSData : a reference to ca's NSData
property NSDictionary : a reference to ca's NSDictionary
property NSJSONSerialization : a reference to ca's NSJSONSerialization
property NSString : a reference to ca's NSString
property NSUTF8StringEncoding : a reference to 4
--------------------------------------------------------------------------------
on JSONtoRecord from fp
local fp
set JSONdata to NSData's dataWithContentsOfFile:fp
set [x, E] to (NSJSONSerialization's ¬
JSONObjectWithData:JSONdata ¬
options:0 ¬
|error|:(reference))
if E ≠ missing value then error E
tell x to if its isKindOfClass:NSDictionary then ¬
return it as record
x as list
end JSONtoRecord
--------------------------------------------------------------------------------
-- This second handler encodes the URL format without errors
on urlEncode(input)
tell current application's NSString to set rawUrl to stringWithString_(input)
set theEncodedURL to rawUrl's stringByAddingPercentEscapesUsingEncoding:4 -- 4 is NSUTF8StringEncoding
return theEncodedURL as Unicode text
end urlEncode
(*
# IN PROGRESS STEP 1A - Gets lastVault Name and Path data from obsidian.json at home folder
-- Gets the last vault JSON info
set home to the path to home folder
set v to the POSIX path of home & "Library/Application Support/obsidian/obsidian.json"
set JSONVaultRecord to JSONtoRecord from v
set lastVaultID to last_open of JSONVaultRecord
set lastVault to lastVaultID of vaults of JSONVaultRecord
set vaultPath to |path| of lastVault
*)
# STEP 1B - Gets user inputted vault Name and Path
set vaultName to "Obsidian"
set vaultPOSIXPath to "/Users/xxx/Documents/Obsdian/"
# STEP 2 - Gets relative path to lastOpenFile
-- Gets the workspace JSON file
set f to vaultPOSIXPath & ".obsidian/workspace"
set JSONFilesRecord to JSONtoRecord from f
set lastOpenFile to item 1 of lastOpenFiles of JSONFilesRecord
# STEP 3 - Encodes the relative path to a URL-friendly format
set encodedRelativePath to urlEncode(lastOpenFile)
# STEP 4 - Mounts the URI scheme link from variables vaultName and encodedRelativePath
set obsidianURL to "obsidian://open?vault=" & vaultName & "&file=" & encodedRelativePath
set fileName to name of (info for (vaultPOSIXPath & lastOpenFile))
return fileName```