Tinderbox Forum

Export to DEVONthink using AppleScript

I’ve been away a while. TB8 looks good.

To try out the new scripting feature, I put together the bare bones script below for exporting selected notes from Tinderbox to DEVONthink. Copy-paste into Script Editor, select various Tinderbox notes, and run.

From the dictionary it wasn’t easy to figure out that I needed ‘selections’ (in the plural). But the rest was straightforward, a LOT easier now than years ago via runCommand and osascript!

This is plain text. Is there a (not too hard) way to bring over formatting? Or links between notes?

The script (edited per Rob’s suggestions):

    -- select notes in Tinderbox 8 and run to export them to DEVONThink
tell application "Tinderbox 8"
	tell front document's selections
		--selections (plural) is the magic word; non-sequential, multiple levels ok
		--each line below makes a list of that attribute's value for all selected notes
		set theNames to name
		set theTexts to value of attribute "Text"
		--set theURLs to value of attribute "URL"
		set noteURLs to value of attribute "NoteURL"
		set theTags to value of attribute "Tags"
	end tell
end tell
tell application id "DNtp"
    activate
	set destGroup to display group selector
	repeat with i from 1 to length of theNames
		create record with {name:theNames's item i, content:theTexts's item i, URL:noteURLs's item i, tags:theTags's item i, type:text} in destGroup
	end repeat
end tell

1 Like

FWIW to let your readers copy, paste and test your code, and use it without glitches introduced by the wiki software conversions to smart characters, you can enclose it above and below with three backticks (and specify the computer language in lower case just after the first 3 backticks).

e.g. something like:

```applescript
tell application "Tinderbox 8"
    tell front document's every selection
        -- selections (plural) is the magic word; non-sequential, multiple levels ok
        -- each line below makes a list of that attribute's value for all selected notes
        set theNames to name
        set theTexts to value of attribute "Text"
        -- set theURLs to value of attribute "URL"
        set noteURLs to value of attribute "NoteURL"
        set theTags to value of attribute "Tags"
    end tell
end tell

tell application id "DNtp"
    activate
    set destGroup to display group selector
    repeat with i from 1 to length of theNames
        create record with {name:item i of theNames, content:item i of theTexts, URL:item i of noteURLs, tags:item i of theTags, type:text} in destGroup
    end repeat
end tell
```

Two other very minor tricks, in case they are of any interest:

  • adding activate in the first line of the "DNtp" scope will bring the selector into UI focus
  • in your Applescript code you can use length of foo in lieu of foo's length to avoid a code coloring glitch which occurs when the wiki software misinterprets apostrophes/single quote chars
1 Like

Thanks for the tips.

My approach for formatting is to use the HTML export rather than the text. But, maybe it’s possible for RTF formatting to move over.

In fact, looking at your code, what if you try

create record with {name:theNames's item i, content:theTexts's item i, URL:noteURLs's item i, tags:theTags's item i, type:rtf}

Note type:rtf vs type:text. I’m not positive (haven’t tried it), but I think your code creates a plain text file rather than an RTF file. So if that’s the case, try RTF and see if the formatting comes over.

re: links… the “best” approach I’ve found is to do string substitution of the links (also in that post I shared above). It’s kinda painful. Maybe there’s a better way now with the more integrated scripting. And hopefully the brilliant minds at Eastgate and DEVON recognize the great potential in more closely integrating the tools and come up with a smooth export & linking strategy.

1 Like

I usually export as HTML then use textutil to convert. textutil has a lot of variations – and one could also consider using Pandoc. Creating RTF directly by means of a script is ugly, and using the HTML intermediate steps yields much nicer formatting. Assuming you know CSS and other HTML formatting skills.

textutil -format html -convert rtf

Thanks @Pat and @PaulWalters!

‘Publish Tinderbox to DEVONthink’ is impressive! Both formatting and links go over. Being only a scripter without a coding background, at this point I only dimly understand how it does its magic.

In my humble script changing the DEVONthink record type does not help, seemingly a limitation of AppleScript, not DEVONthink or Tinderbox. Assigning rtf to an AppleScript variable coerces to plain text.

So the simplest way I can think of to bring formatting over, via html as suggested, is this:

Change the default HTML Page template to

^text^

“Export”$Text to a new attribute by applying a stamp like this:

$theHTML=exportedString(this, $HTMLExportTemplate)

Then use a slightly modified script (with type changed to html, and for content: using theHTMLs instead of theTexts).

Is there a way to convert to html “on the fly” or some other better way to do this that would not require storing the same “text” in both the original and in html?

Thanks very much for this script! I cannot seem to wrap my head around scripts so you are appreciated!