Tinderbox Forum

Hook Script to add a new note to Tinderbox

Open the preferences of Hook, click on the “Scripts” tab and then open the “New Item”.
Paste this into the script window:

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

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

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

And now try it out - have a saved TBX file open. Now go to Safari and open hook (ctrl+H). Next go to “Hook to new” in the menu of hook:

Select Tinderbox in the list of apps and Hook will create a new note within an “inbox” container in your TBX file.

1 Like

Cool!

Is there a way to have $URL populated with the HOOK URL? I can get the note create but can’t seem to get the URL.

if you select the note and open Hook again (ctrl+H) you see the link and can jump back to Safari

Not sure what I’m doing wrong.

Hook stores the a:b link relation in a database. Whenever you open any of the linked items you get all related objects. So if you open Hook in front of a Safari page, Hool will copy the URL to this page. If you now call “Hook to new…” a note in TBX will get created and the link to this note will be stored together with the link to the safari url in the Hook database. If you open Hook again on the same Safari URL you will see the link to this specific TBX note, if you select the note in TBX and open Hook you will see the link to the Safari page in Hook.

Strange. I’m not seeing the UI that you are. Clearly, I’m doing something wrong.

but you do get the Hook dialog if Safari is the front app and you press CTRL+H?

because I saw the created notes - Hook created two notes for you

Yes, but I’m not getting this:
image

what happens if you press CTRL+H and TBX is the front app???

If there is no link in the Hook clipboard (you start in TBX) the dialog would look like this:

Yes, I get that, but I don’t get the link back to Safari. Maybe easier if we hopped on a zoom.

It would depend on what you selected as the source. If you hook a particular note to webPageFoo, then you will only see the hook to webPageFoo when you invoke Hook on that note.
There’s no “nearby” yet. you can also see your tinderbox links in search and in recent.

If you Copy Link on a Tinderbox item, and then paste, you will see the specific URL you are dealing with.

There are of course design decisions to make in choosing the URL format for file-based apps like Tinderbox, Curio and Scrivener that have components and that have their own URL schemes. See for instance Principles That Determine the URL Scheme Returned by Copy Link for a Given App – Hook.

The current integration scripts for Tinderbox are discussed here: Tinderbox and Hook - Discussion & Help - Hook Productivity Forum (we might have made slight changes after that, I would need to check). There is also a Tinderbox forum topic related to that.

Those integrations were developed before Tinderbox 9. It may be that Tinderbox 9 has additional capabilities (e.g., in its AppleScript) that could or should be leveraged for Hook-Tinderbox integration.

For some apps, like DEVONthink , Curio and EagleFiler, Hook offers two different integration scripts: one built into the app, the other is published on line. Discussed:

I think some of the considerations re Curio are pertinent to Tinderbox. For Curio and EagleFiler, we opted to use the hook://file/ URL scheme, but with Tinderbox we opted for a subscheme,so they look like this: hook://tbx/Tinderbox-01b?view=outline+select=1645233672;?filepath=/Users/lucb/Documents/Tinderbox-01b.tbx. However, one can easily change the integration script to simply return a native tinderbox URL like this: tinderbox://Tinderbox-01/?view=map+select=1645233672; Perhaps one day it would make sense to have a hook://file/ scheme that embeds an escaped tinderbox URL as a parameter in a deep link (a more complex version of what Hook does for deep PDF and quicktime links).

of course, it is possible to dynamically choose the scheme as a function of the some automation. E.g., if an entire file is selected, use hook://file/. If a Tinderbox note is selected, one could then use the native Tinderbox URL or hook://tbx/.

The bottom line is that users can determine the URL scheme they’d like Hook to use. It’s in the Get Address tab of Script Editor.

(I should point out that Hook also supports x-callback-url. We’ll publish more on that next quarter.)

Maybe my reply is TMI or should be in a different topic.

3 Likes

a small update of my script - it will add the URL copied with Hook to the $URL attribute of the new note in TBX:

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

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

-- 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
5 Likes

That is exactly what I wanted!!! Thanks.

Have one more ask. :pray: Is there a way to have hook copy selected text in a browser (there is a copy select and link but I could not get it to work) or copied text on the clipboard and then pass this text to the $Text of a note? If that could happen, that would be magical.

1 Like

You could create a shortcut in Keyboard Maestro like this one:


Make sure to set Tinderbox as a default Template (in Hook preferences).

2 Likes

I will look into this - but I’m too busy today…

Cool, I sorta got this working. It does not seem consistent though. For the first entry, it never seems to copy the clipboard to the new note. It does seem to work on the second, but then again does not work. Not sure how to debug the flow. Are you experiencing this? It also seems to open a new tab every time an item is added, which is a bit annoying. I think we’re on to something, though. This will be AWESOME once we work out the kinks.

That’s strange, it works fine for me, i.e. without issues you’re experiencing. Maybe try to change the hotkey or pause time…

Hi Detlef,

I might be missing something but, I want to add a Hook tag to tinderbox to any item created much like I do with the Finder and Devonthink.

Is there a simple way to add the Hook tag to your script for tinderbox?

On a side note:
This script I found recently adds the Hook Tag automatically to any Devonthink item for those of you interested I recently found. It works for me, but use at your own risk. I use it as a smart rule in Devonthink.

Here is the link:

Thanks
Tom

Hi Tom,

if you use the New command in Hook to create a new note in TBX with hook - you find the Hook URL in the attribute $URL:

hook://tbx/Zettelkasten/Literaturwissenschaft/Gender%20Studies/%C3%9Cbungen/Block%204?view=outline+select=1652711713;?filepath=/Users/detlefbeyer/Documents/Tinderbox/Zettelkasten.tbx

If you just paste a Hook item on a Tinderbox note you will find the Hook path to this note in the $NoteURL attribute:

tinderbox://Zettelkasten/Literaturwissenschaft/Gender%20Studies/%C3%9Cbungen/Block%204?view=outline+select=1652711713;

Both should give you enough information to create an agent in TBX and “flag” the notes.

1 Like