Hi, I am a very new user here - owning a Hookmark Pro licence for over a year but never having found the time to get acquainted to it.
A couple of days ago I updated my licence and finally took the time to look into it.
I am especially interested in the applications supported by hookmark and the scripts that make it possible.
So I started my exploration with the Finder integration scripts - and started to scratch my head about the sloppy coding. I don’t intend to propose myself as overly clever, but in parts I wonder how they do work at all.
The default Get Address script:
tell application "Finder"
set theItems to selection
set n to count of theItems
if n = 1 then
try
set ext to name extension of item 1 of theItems
if ext = "hook" or ext = "HOOK" or ext = "hookmark" then
set filepath to item 1 of theItems as alias
return read POSIX path of filepath
end if
end try
get URL of item 1 of theItems
else
set thePath to (POSIX path of (target of the front window as string))
set filepath to "file://" & thePath
end if
end tell
– This script has three return exits, one explicit, two implicit. I do know that this is possible in Apple Script of course and even very common in some programming languages. But how is an interested newcomer supposed to make sense of this?
– There is a seemingly useless try statment in the first half of the if statement (am I overlooking something here?) whereas in the second half one seems to be missing to properly catch cases with no folder window open on a desktop without any file selected and give meaningful (instead of cryptic) user feedback. Of course it’s dumb to make Hookmark call this script under this condition but, hey, I was dumb enough, too, which is why I know. Or simply check what happens using the default script if a Finder information window happens to be frontmost, or the window listing all drives on your system plus the network, or …
– There are completely unused or redundant variables (making the code unnecessarily complex, IMHO).
– There are two approaches to get a file URL in this code close to one another, one official and safe, the other one hand-made and grotesquely bad. I wonder how it does work at all. Maybe only because file URLs finally get wrapped up in hook://file/ URLs? I don’t know.
This code
set thePath to (POSIX path of (target of the front window as string))
set filepath to “file://” & thePath
won’t return a valid file URL in many cases, e. g. a folder window named (awkwardly for demonstration purposes as) “Is thiß a folder?” will return a file URL without any escapes
file:///Users/yourusername/Desktop/Is thiß a folder?/
whereas the code two lines above, asking the Finder for a (valid!) URL will return a properly escaped result:
file:///Users/yourusername/Desktop/Is%20thi%C3%9F%20a%20folder%3F/
Just open a terminal window and compare
open file:///Users/yourusername/Desktop/Is thiß a folder?/
or
open ‘file:///Users/peterjhartmann/Desktop/Is thiß a folder?/’
and
open file:///Users/yourusername/Desktop/Is%20thi%C3%9F%20a%20folder%3F/
(you have to make sure to provide a folder with this name on your desktop and replace ‘yourusername’ by your actual username of course.)
Michael Tsai, the developer of EagleFiler even went out of his way to provide a specially crafted ASObjC urlEncode() handler in his integration script for it, making sure to remove question marks from the final result (or at least I assume that it’s his code) - just check for yourself. Maybe it should be used in the Finder or elsewhere, too? I am not sure.
– Instead of giving the user intelligible feedback, Apple’s traditionally cryptic programmer’s errors are handed down giving him or her no clue about what is going wrong.
– And sadly, the Hookmark “Script Editor” does not provide formatted code (as Keyboard Maestro, Automator etc. does) and wraps lines, so scripts are almost impossible to read and understand. Yes, I know - I’m using Script Debugger, too. Feature request: “Please (at least) provide a button to open the script in the default script editor!” (Shouldn’t be too difficult to implement.)
My reworked version:
tell application “Finder”
set theItems to selection
if (count of theItems) = 1 then
try -- why this try statment? what could cause an error? missing extension will return "",
-- not error, the existance of item 1 is guaranteed and the alias coercion
-- has to work as item 1 is guaranteed to be an existing file;
-- try...end as an odd replacement for if...then...else?
-- I am not sure what I am overlooking here, so I left it in.
set ext to name extension of item 1 of theItems
if ext = "hook" or ext = "HOOK" or ext = "hookmark" then
set filepath to item 1 of theItems as alias
return POSIX path of filepath
end if
end try
return URL of item 1 of theItems
else
try
return URL of (target of the front window)
on error
error "There is neither a single file selection, nor an appropriate folder window in front to hook up to."
end try
end if
end tell
I won’t go into detail but my reworked Get Name script looks like this:
tell application “Finder”
set theItems to selection
if (count of theItems) = 1 then
return name of item 1 of theItems
else
try -- silently ignore information windows etc.
return (name of target of the front window) -- "target" is the official terminology;
-- how does "folder" work at all?
on error
error "There is neither a single file selection, nor an appropriate folder window in front to hook up to."
end try
end if
end tell
Hoping that I didn’t offend anybody I do appreciate enlightening comments. Maybe I am wrong all the way? Thanks!