# Programmatically getting the Hook URL of a Finder item

**URL:** https://discourse.hookproductivity.com/t/programmatically-getting-the-hook-url-of-a-finder-item/1330
**Category:** Bug Reports
**Created:** [March 24, 2020, 4:16pm UTC](https://discourse.hookproductivity.com/t/programmatically-getting-the-hook-url-of-a-finder-item/1330 "2020-03-24T16:16:37Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![johnsidi](https://discourse.hookproductivity.com/user_avatar/discourse.hookproductivity.com/johnsidi/32/732_2.png) [@johnsidi](https://discourse.hookproductivity.com/u/johnsidi)
#### Post date: [March 24, 2020, 4:16pm UTC](https://discourse.hookproductivity.com/t/programmatically-getting-the-hook-url-of-a-finder-item/1330/1 "2020-03-24T16:16:37Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![LucB](https://discourse.hookproductivity.com/user_avatar/discourse.hookproductivity.com/lucb/32/2228_2.png) [@LucB](https://discourse.hookproductivity.com/u/LucB)
#### Post date: [March 24, 2020, 10:15pm UTC](https://discourse.hookproductivity.com/t/programmatically-getting-the-hook-url-of-a-finder-item/1330/2 "2020-03-24T22:15:38Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![LucB](https://discourse.hookproductivity.com/user_avatar/discourse.hookproductivity.com/lucb/32/2228_2.png) [@LucB](https://discourse.hookproductivity.com/u/LucB)
#### Post date: [March 26, 2020, 12:14am UTC](https://discourse.hookproductivity.com/t/programmatically-getting-the-hook-url-of-a-finder-item/1330/3 "2020-03-26T00:14:31Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![johnsidi](https://discourse.hookproductivity.com/user_avatar/discourse.hookproductivity.com/johnsidi/32/732_2.png) [@johnsidi](https://discourse.hookproductivity.com/u/johnsidi)
#### Post date: [March 26, 2020, 10:37am UTC](https://discourse.hookproductivity.com/t/programmatically-getting-the-hook-url-of-a-finder-item/1330/4 "2020-03-26T10:37:04Z")

</div>

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

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
```

---

<div class="post-metadata">

### Author: ![RobTrew](https://discourse.hookproductivity.com/user_avatar/discourse.hookproductivity.com/robtrew/32/212_2.png) [@RobTrew](https://discourse.hookproductivity.com/u/RobTrew)
#### Post date: [March 26, 2020, 8:53pm UTC](https://discourse.hookproductivity.com/t/programmatically-getting-the-hook-url-of-a-finder-item/1330/5 "2020-03-26T20:53:36Z")

</div>

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:

```auto
use framework "Foundation"

set x to the clipboard

```

The following should work:

```auto
use framework "Foundation"
use scripting additions

set x to the clipboard

```

---

<div class="post-metadata">

### Author: ![johnsidi](https://discourse.hookproductivity.com/user_avatar/discourse.hookproductivity.com/johnsidi/32/732_2.png) [@johnsidi](https://discourse.hookproductivity.com/u/johnsidi)
#### Post date: [March 27, 2020, 10:15am UTC](https://discourse.hookproductivity.com/t/programmatically-getting-the-hook-url-of-a-finder-item/1330/6 "2020-03-27T10:15:12Z")

</div>

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](https://discourse.hookproductivity.com/t/how-to-create-markdown-rtf-html-mediawiki-latex-mathematica-links-using-hook-urls-to-one-or-more-selected-items-in-finder/1334) would not be needed and I would be able to get these and others from any other compatible app using a simple loop.

---

<div class="post-metadata">

### Author: ![RobTrew](https://discourse.hookproductivity.com/user_avatar/discourse.hookproductivity.com/robtrew/32/212_2.png) [@RobTrew](https://discourse.hookproductivity.com/u/RobTrew)
#### Post date: [March 27, 2020, 4:45pm UTC](https://discourse.hookproductivity.com/t/programmatically-getting-the-hook-url-of-a-finder-item/1330/7 "2020-03-27T16:45:18Z")

</div>

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)
