Programmatically getting the Hook URL of a Finder item

I wrote the following AppleScript for getting the Hook URL of a selected item in Finder, which for some strange reason does not work as expected:

tell application "Finder"
	set the clipboard to ""
	activate
end tell

tell application "System Events"
	--activate Hook
	keystroke space using {command down, shift down}
	delay 1
	--Copy Link 
	keystroke "c" using {command down}
end tell

return the clipboard

The script returns the name of the item and not its Hook URL.

Also this script has the same problem:

tell application "Finder"
	set the clipboard to ""
	activate
end tell
tell application "System Events"
	--activate Hook
	keystroke space using {command down, shift down}
	delay 1
    --open submenu
	keystroke "t" using {control down}
	delay 1
	key code 125 -- down arrow
	key code 125
	key code 125
	delay 1
	key code 36 --enter
	delay 1
end tell
return the clipboard

To make things even more strange, I happen to use the Alfred 4 app which has a clipboard history feature, and after executing the above script I can see on the Alfred clipboard viewer the Hook url!.

This made me experiment a little with Alfred and AppleScript…

Using the following script the Hook URL IS returned when the merging clipboard feature is activated in Alfred! The only problem with this script is that it returns both the last item in the clipbard before executing the script and the Hook URL. When I deactivate the merging feature the script returns only the name of the selected item in Finder as the previous script.

> tell application "Finder"
> 	set the clipboard to ""
> 	activate
> end tell
> 
> tell application "System Events"
> 	--activate Hook
> 	keystroke space using {command down, shift down}
> 	delay 1
> 	--Copy Link 
> 	keystroke "c" using {command down}
> 	--activate Alfred's viewer for clipboard
> 	keystroke space using {command down, shift down}
> 	delay 1
> 	--use the first item in the clipbοard viewer
> 	keystroke 2 using {command down}
> 	delay 1
> 	set theLink to the clipboard
> 	
> end tell
> return theLink

Any help is welcome!

Welcome to the Hook Productivity Forum, @johnsidi. We are just about to release Hook 1.5 with automation. So you should be able to ask Hook directly for this via AppleScript.

Correction, whereas Hook automation will be in 1.5, it will not operate on the current selection. It will enable the user to get all hook bookmarks (for URLs that have been linked or on which Copy Link was applied) and what’s been hooked (bidirectionally linked) to a given URL.

Regarding your question: clipboard managers can mess with the clipboard. We recommend excluding Hook from the list of apps that are observed by the clipboard manager. Does that solve the problem?

No, but I have found out that if you replace “the clipboard” with “do shell script “pbpaste”” the problem is solved! strange things… :slight_smile:

So the following script works fine:

tell application "Finder"
	activate
	tell application "System Events"
		--activate Hook
		keystroke space using {command down, shift down}
		delay 1
		--Copy Link 
		keystroke "c" using {command down}
		delay 1
		set HookLink to do shell script "pbpaste"
	end tell
end tell
return HookLink
1 Like

Not sure if this was the issue, but if the Hook environment needs to make a hidden import of Foundation, or any other ObjC libraries, the import of these can shadow the import of standard additions (of which the clipboard is a part).

The trick is to explicitly import the standard additions library.

So for example, while this will fail:

use framework "Foundation"

set x to the clipboard

The following should work:

use framework "Foundation"
use scripting additions

set x to the clipboard

No, the following script still returns the file name and not the Hook URL:

use framework "Foundation"
use scripting additions

tell application "Finder"
	activate
	tell application "System Events"
		--activate Hook
		keystroke space using {command down, shift down}
		delay 1
		--Copy Link 
		keystroke "c" using {command down}
		delay 1
		--set the clipboard to (do shell script "pbpaste")
	end tell
end tell
return the clipboard

If the line “set the clipboard to (do shell script “pbpaste”)” is part of the executable code then the result is a Hook URL.

Anyway, I hope that in the future Hook will provide full scripting support (and not only for accessing its database) for itself! Then all the “hacks” that I used in the script for getting the Hook URLs of a Finder selection would not be needed and I would be able to get these and others from any other compatible app using a simple loop.

PS you wouldn’t want to include

use framework "Foundation"

there.

(I only included it in that example to demonstrate shadowing – it’s possible that Hook’s own script-running environment imports the Foundation framework out of sight somewhere, but in your own script it wouldn’t provide anything, and might create a problem)