Tinderbox and Hook

The native tinderbox:// url-scheme does most of the work for free,
but it doesn’t record the filepath of the active document.

(It was first conceived for linking within open documents, and any file not open or in the MRU list is hidden to it.).

SO

  1. We can usefully capture the tinderbox:// url itself (possible through osascript in Tinderbox 8), but
  2. we also need to append a ?filepath=<encodedurl> option to it, where encodedurl is derived from the file path of the active Tinderbox document.
  3. Because of this ?filepath addition, which is outside the scope of the tinderbox:// scheme itself, we also need a Hook Open Item script, to handle the document finding, giving it a suitable custom scheme name, perhaps tbx://

For Name and Address, Hook now allows us to write a single ‘Get Address’ script which returns both Name and Addresss in a markdown [Name](Address) format.

As the Get Address script needs to percent-encode the file path, and the Open Item script needs to decode that url-encoding, JavaScript may be a little more straightforward than AppleScript (JS has built in encode and decode functions).

For example, we can, first for the Open Item script:

  1. Enter tbx in the Hook:// document-identifier field at the bottom of the Open Script panel in Hook preferences.
  2. Paste in the whole code below, from the //JavaScript comment line at the top, which Hook requires to identify the script language, down to })(); at the end.
//JavaScript
(() => {
    'use strict';
    const
        parts = "$0".split(/\?filepath=/),
        tbx = Application('Tinderbox 8');
    return (
        tbx.activate(),
        tbx.open(Path(
            decodeURIComponent(
                parts.slice(-1)[0] // Document filepath.
            )
        )),
        Object.assign(
            Application.currentApplication(), {
                includeStandardAdditions: true
            }
        ).openLocation(
            `tinderbox://${parts[0].split('//')[1]}`
        )
    );
})();

and then for the Get Address script.

Paste the the whole of the following (again, including \\JavaScript at the top, down to })(); at the end.

//JavaScript
(() => {
    'use strict';

    const ds = Application('Tinderbox 8').documents;
    return 0 < ds.length ? (() => {
        const 
            doc = ds.at(0),
            note = doc.selectedNote();
        return null !== note ? (
            `[${note.name()}](${
                'tbx' + note.attributes.byName('NoteURL')
                .value().slice('tinderbox'.length) + 
                '?filepath=' + encodeURIComponent(doc.file().toString())
             })`
        ) : '';
    })() : '';
})();