Tinderbox Forum

Resolve a File URL to a Path

Does anyone know if there is a way to resolve a URL, like a Hook URL, to the local file path? I would like to be able to use this method to resolve URLs to paths for populating the <img scr=> HTML tag.

From what I understand, it should be possible via AppleScript. Check this Hook forum discussion.

this should do it (see the discussion in the Hook forum) - use it for the “new item” tab of the Tinderbox script in Hook-preferences:

use AppleScript version “2.4” – Yosemite (10.10) or later
use scripting additions
use framework “Foundation”

set myMacPath to getFilePathFromHook()

tell application id “Cere” to activate

tell application id “Cere”
if (count of documents) is equal to 0 then
return “No document is available”
end if

set openFile to file of front document
if openFile is missing value then
return “Hook can’t link unsaved files”
end if

set filePath to my encodedPath(POSIX path of (openFile as alias)) as string

tell front document
if not (exists note “Inbox”) then make new note with properties {name:“Inbox”}
set theContainer to note “inbox”
set myNote to make new note with properties {name:"$title"} at theContainer
set value of attribute “URL” of myNote to “$user_link”
set value of attribute “TheFilePath” of myNote to myMacPath
end tell

set tinderboxURL to value of attribute “NoteURL” of myNote
set tinderboxURL to (text (1 + (length of “tinderbox://”)) thru -1 of tinderboxURL)
set tinderboxURL to “tbx://” & tinderboxURL

return tinderboxURL & “?filepath=” & filePath
end tell

on getFilePathFromHook()
set myPath to “no path”
tell application “Hook”
set bks to bookmark from active window
if bks is not {} then
set myPath to path of item 1 of bks
set myPath to “not found”
end if
end tell

return myPath
end getFilePathFromHook

– encodedPath :: FilePath → Percent Encoded String
on encodedPath(fp)
tell current application
(its ((NSString’s stringWithString:fp)'s ¬
stringByAddingPercentEncodingWithAllowedCharacters:(its NSCharacterSet’s ¬
URLPathAllowedCharacterSet))) as string
end tell
end encodedPath

here is a very basic TBX file with a prototype to add the “TheFilePath” attribute:
hookme.zip (21.5 KB)

1 Like