How to "Hook to Copied Link" with AppleScript

I have checked the dictionary of in ScriptEditor, however, it seems Hook doesn’t offer a way to programmatically realizing “Hook to Copied Link” function, or I’m missing something? Hope someone can tell me. :thinking:

I ask this question because I want to implement “Hook to Copied Link” with AppleScript then I can implement all my workflow based on without the need to open it’s GUI, which can save lot of time for me. I would be very grateful if someone could help me. :smiley:

1 Like

that is correct. Our AppleScript has atomic actions allowing users to compose programs.We’ll likely later share some shortcuts that are more expansive.

1 Like

Wow, can’t wait to see, thank you Luc. It would be nice if Hook share it’s implementation with AppleScript of it’s GUI actions (like following actions), which can significantly boost our efficiency! :zap:

@realCrush There is a Finder script “Hook to Clipboard” which may help: Scripts and Services for Hook (macOS)

1 Like

Thank you neuon! I’ve check the script you mentioned. However it doesn’t match my need, that script require a url as source to make bookmark, however I (and maybe most people) will use “Copy Markdown Link”, so in clipboard we only have the markdown format text like [xxx](url), and hard to extract the url in it.
I actually have no idea how process information from clipboard automatically, they can automatically create hook no matter what kind of information in your clipboard (markdown text or a pure url). I think they might achieve this based on Hook’s database, however, we have no way to access it with AppleScript, at least for now.
So I hope Hook can share some implementation of GUI actions with AppleScript, which will open a door for deeper integration for Hook and other Apps, thus lead a super powerful workflow. :zap:

For me, just an AppleScript command to access last hook bookmark is very enough for me to do lot of things! :star_struck:

@realCrush Give this script a try; should do the job. It has had brief testing using Hook’s Copy Link and Copy Markdown Link including Titles full of escaped characters. Feel free to message me if you have any questions :+1:

-- Make bookmark with Hook link copied to clipboard
-- Handles Hook's Copy link and Copy Markdown link
-- Modified version of Brett Terpstra's script "Hook to Clipboard" @

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

property subStrMarker : "](" -- marker for splitting markdown link parts
property searchEscape : "\\"

-- get data from clipboard
set _pb to the clipboard as record
set _link to «class utf8» of _pb
set _title to Unicode text of _pb

-- remove any escaped characters
set _title to my findReplace(_title, searchEscape, "")
set _link to my findReplace(_link, searchEscape, "")

-- get link sub-strings
set _theParts to (my splitLink(_link))
if (count of _theParts) > 1 then -- link is a markdown link [title](url)
	set {_title, _url} to my getData(_theParts, _link)
	set _url to _link -- is a copied link
end if

-- create a bookmark from the clipboard's processed link
tell application "Hook"
	set _hook to make bookmark with data _url
	set name of _hook to _title
end tell

-- create links with files selected in Finder
tell application "Finder"
	set _files to selection
	if (count of _files) > 0 then
		repeat with _file in _files
			tell application "Hook"
				set x to (percent encode POSIX path of (_file as string))
				set _target to make bookmark with data x
				hook _hook and _target
			end tell
		end repeat
		display dialog "No files selected"
	end if
end tell

-- Helpers below here -----------------

# get data for the _link name and url
on getData(_theParts, _link)
	set _urlLength to (length of last item of _theParts) + 1 -- account for leading "("
	set _linkLength to (length of _link) -- total length of link
	set _splitPoint to ((_linkLength - _urlLength))
	-- get trimmed [title] and trimmed (url)
	set _title to (characters 2 thru (_splitPoint - 1) of _link) as text -- remove [  ]
	set _url to (characters (_splitPoint + 2) thru (_linkLength - 1) of _link) as text -- remove ( ) 
	return {_title, _url}
end getData

# split markdown url into sub-strings
on splitLink(_theLink)
	-- if _theLink is a link, returned as one item - does not contain "]("
	-- if _theLink is a markdown link, returns at least two substrings.  (name, address)
	-- Last sub-string is the url
	set oldDelims to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {subStrMarker}
	set _textItems to text items of _theLink
	set AppleScript's text item delimiters to oldDelims
	return _textItems
end splitLink

# used to de-escape links (e.g. copied from Hook)
on findReplace(t, toFind, toReplace)
	set {tid, text item delimiters} to {text item delimiters, toFind}
	set t to text items of t
	set text item delimiters to toReplace
	set t to t as text
	set text item delimiters to tid
	return t
end findReplace


Thank you neuon, awsome! :star_struck: Your script is very helpful, but seems only works with, and might not suit for other apps, so this may not be a general solution. I wish Hook can provide an easy and programmable way to trigger it’s actions without open Hook’s GUI.

Using hotkeys to trigger actions and simulate hotkeys presssing with AppleScript is a possible solution, however I’ve tried it and found bugs exist with hotkeys itself. Hook hotkeys has bugs - #3 by realCrush

@realCrush The above Script can be modified to work with all apps and I will post that later. That script is about working with a Markdown link already on the clipboard.

From the video it seems what you may need is an alternative to Hook’s Copy Markdown Link because Hook’s global shortcuts are not working as expected?

The following script should do that for you. By default the script expects to be run just like Hook (i.e. from the target window). If you use FastScripts with a global Shortcut set for the same combination as Hook uses for Copy Markdown Link (but with Hook’s disabled), it should all mimic using Hook.

If you use an app like Script Editor to run the Script, the script needs to be changed, because the current active app (frontmost) at execution is not the active app for Hook to target. To change the script, delete the (* and *) characters where shown. Then the script will target the previous app which was active before Script Editor was made the active app to run the Script.

# Copy Markdown Link [Hook]
# Copies a markdown link for the active window to 
# the clipboard without using Hook's Global Shortcuts or UI
# The AppleScript version can be used in an automation tool (FastScripts,Keyboard Maestro, BetterTouchTool, etc.)

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

#  Following section required if Script run from Script Editor/Debugger

-- delete the two above characters

-- Make sure target app is frontmost
tell application "System Events"
	tell (first application process whose frontmost is true)
		-- the "it" is this script
		set visible of it to false
		delay 0.2
		-- now, the "it" becomes the previous frontmost app
		set theActiveApp to name of it
	end tell
end tell

-- delete the below two characters

-- get a link
tell application "Hook"
		set _target to bookmark from active window
		if _target is equal to missing value then error
	end try
end tell

-- get link data
tell application "Hook"
	set _name to name of _target
	set _address to address of _target
end tell

-- copy to clipboard as markdown link
set _link to "[" & _name & "](" & _address & ")"
set the clipboard to _link

1 Like

Thanks neuon, I also wrote few lines of code to realize the same thing, your method and mine rely on bookmark from active window to work, and this function seems to be a generalized one, however in practice, this function doesn’t work for many apps, at least for and, so we might still have no way to get hook links in a general way without GUI, at least for now. :pensive:

there’s temporarily an issue with the AppleScript with x-callback-url. We hope to have this solved in Hook 3.7.

Thank you Luc, I hope so.