Welcome, Guest. Please Login
Tinderbox
  News:
IMPORTANT MESSAGE! This forum has now been replaced by a new forum at http://forum.eastgate.com and no further posting or member registration is allowed. The forum is still accessible via read-only access for reference purposes. If you wish to discuss content here, please use the new forum. N.B. - posting in the new forum requires a fresh registration in the new forum (sorry - member data can't be ported).
  HomeHelpSearchLogin  
 
Pages: 1 2 
Send Topic Print
Exporting to DEVONthink using AppleScript (Read 26784 times)
Sumner Gerard
Full Member
*
Offline



Posts: 359

Exporting to DEVONthink using AppleScript
Nov 29th, 2012, 11:20pm
 
Responding to Lisa's question in this thread.

To export selected notes directly to DEVONthink (without having to set up an export template, save to a file, then import into DEVONthink, etc., etc.) you can do this.

Put this in a Tinderbox code note named Send2DevonAS:

Code:
-- In DEVONthink the new records will appear in your default group for new notes; change incoming group to current group or other value if you want to change that behavior.
-- Make sure there are no straight apostrophes in this script or in the Tinderbox attribute values. A good solution is using Mark Anderson "quotify" to convert to curlies. See http://www.eastgate.com/Tinderbox/forum//YaBB.pl?num=1265006946/0#4

on run argv
	--put values of TB attributes from command line into AppleScript variables:
	set {recordName, recordContent, recordURL} to {item 1, item 2, item 3} of argv

	--now just your usual brilliant AppleScript code-- can add date, etc:
	tell application id "DNtp" to create record with {name:recordName, content:recordContent, URL:recordURL, type:text} in incoming group of database 1
end run
 



And this action code in a new stamp I've named Send to Devon:

Code:
runCommand("osascript -e '"  +$Text("Send2DevonAS") +"' '" +$Name +"' '"+ $Text +"' '"+ $URL+"'") 



Then select the notes in Tinderbox that you want to export to DEVONthink and apply the stamp.  That's it. They'll all be sent over in a flash (minus, of course, any text formatting, which I think you lose exporting the hard way as well).

The only tricky part, I found, was getting the quoting right in the stamp action code. You need it to concatenate to: osascript -e followed by a space followed by the script surrounded by single quotes followed by a space followed by the first attribute surrounded by singlestraightquotes followed by a space followed by the second attribute surrounded by singlestraightquotes, etc. Because of the command line quoting you need to avoid any single straight quotes in the script itself and remove any that lurk in attribute values using something like Mark A's "quotify" to convert to curlies. Perhaps some day Tinderbox will have a friendly wrapper that shields the user from these inconveniences, something like:

      runAScript($Name("MyASCodeNote"),$MyTbAttrib1,$MyTbAttrib2, $MyTbAttrib3, …)   and,

      runAScript("/path/to/script.scpt",$MyTbAttrib1,$MyTbAttrib2, $MyTbAttrib3, …)

Anyway, in the AppleScript the Tinderbox attribute values included by the stamp's action code on the command line are accessed as item 1, item 2, item 3 etc. of the mandatory "run handler", here named argv.
Back to top
 
« Last Edit: Nov 30th, 2012, 12:09am by Sumner Gerard »  
  IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 5689
Southsea, UK
Re: Exporting to DEVONthink using AppleScript
Reply #1 - Nov 30th, 2012, 3:25am
 
Thanks for sharing this. I've not time to experiment just now but it occurs to me that a code note might help - see here, especially the latter part about using eval(). If the latter three inputs could be encapsulated like the AS script some of the quote balancing issues goes away.

Although a code note, used as a function of sorts, doesn't formally have inputs the calling note can always set the desired attributes in the code note (and zero them out when done). In pseudocode:

$InputA("a code note") = $Name;
$InputB("a code note") = $Text;
$InputC("a code note") = $URL;
... now evaluate $Text of code note using $Input values as input parameters
... then zero out values after
$InputA("a code note") =;
$InputB("a code note") =;
$InputC("a code note") =;

It occurs to me the above method could create a single Applescript list to pass to your export script rather than N separate variables.

Whilst this might seem like even more set-up up front, it goes to the same end that once set up, it is very easy to invoke the actual process desired as often as and whenever needed.
Back to top
 
« Last Edit: Nov 30th, 2012, 3:27am by Mark Anderson »  

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v6
(TB consulting - email me)
WWW shoantel   IP Logged
Paul Walters
Full Member
*
Offline



Posts: 267

Re: Exporting to DEVONthink using AppleScript
Reply #2 - Nov 30th, 2012, 5:55am
 
Sumner, excellent concept -- and very extensible to aligning other attributes to properties of a DEVONthink record.  I am thinking specifically of DEVONthink's Tags (a list property) and using a DEVONthink item link (x-devonthink-item://) that was previously harvested from DEVONthink as the URL.  By doing the last, one could use Tinderbox for annotating DEVONthink records (documents) and create the proper link-backs between the two.

The possibilities are rich.  Applause for this, Sumner.  Cool

Unfortunately, no joy with the stamp as suggested.  Over here, it did not plug and play, but when I changed it to

Code:
eval(runCommand("osascript -e '"  +$Text("Send2DevonAS") +"' '" +$Name +"' '"+ $Text +"' '"+ $URL+"'")) 


It works as desired.  I believe this approach is consistent with Mark Anderson's suggested best practice for using code notes.

A suggestion.  In the code note you can substitute

Code:
in incoming group of database 1 



with

Code:
in display group selector 



and the DEVONthink Group Selector HUD will open so that you can choose the destination group, on the fly.
Back to top
 
 
  IP Logged
Sumner Gerard
Full Member
*
Offline



Posts: 359

Re: Exporting to DEVONthink using AppleScript
Reply #3 - Nov 30th, 2012, 1:28pm
 
Thanks to both for the tips. The concept of feeding inputs into attributes of code notes is intriguing, Mark. How would that work in practice? Is the idea to feed the values into the AppleScript in the code note and just send the script itself to the command line, with values already substituted for the variables?

The way I understand it's working now is that osascript looks for separate arguments placed by the stamp on the command line and places them in a single AppleScript list accessed as item 1, item 2, item 3, etc. of the run handler (as described here). That's convenient, even with the current command line quoting tedium, which I hope eventually will be reduced by a friendly wrapper as a (presumably, says he hopefully) low-cost way to integrate Tinderbox better within the ecosystem of other Mac apps that support AppleScript to one degree or another.

On my machine the stamp works with and without the eval(). Not exactly sure why that could be the case. Will have to restudy the effects of eval().

Great tip on DEVONthink's display group selector, Paul. I was wondering how that was done. When exporting multiple Tinderbox notes at a time need to figure out how to call the group selector for the first note only and apply that choice to the other notes; otherwise it asks for each note.

As you say there are many possibilities, going both to and from DEVONthink (coming from DEVONthink can easily update the value of a Tinderbox attribute on the left side of a runCommand statement that runs an AppleScript).  And not just for DEVONthink and other applescriptable apps. This technique, for example, can easily be applied (it takes three lines within run and tell) to export a tab-separated-data table many apps can consume, leaving just the heavy-duty stuff, such as hierarchical structure or tens of thousands of notes, for Tinderbox export code.
Back to top
 
« Last Edit: Nov 30th, 2012, 3:24pm by Sumner Gerard »  
  IP Logged
russ lipton
Full Member
*
Offline



Posts: 126

Re: Exporting to DEVONthink using AppleScript
Reply #4 - Nov 30th, 2012, 6:19pm
 
I'm probably twenty years too late to the party, but could someone say why/how Devonthink complements their TbX work specifically (e.g., why it wouldn't be done as well in Tbx)?

I should add that I use EagleFiler to capture and tag 'stuff' and have been satisfied enough, but obviously Devonthink covers different ground. And it has artificial intelligence, right? Why on earth have I dawdled?

(If this is the wrong thread for that more general question, please move as appropriate).
Back to top
 
 
  IP Logged
Paul Walters
Full Member
*
Offline



Posts: 267

Re: Exporting to DEVONthink using AppleScript
Reply #5 - Nov 30th, 2012, 10:33pm
 
Over here, Russ, the short answer is that DEVONthink is where I put .pdfs, .docs, images, etc.  Just as you use EagleFiler.  Why one vs. the other -- kind of off topic for here, but just a preference I suppose.   Tinderbox doesn't store docs, but excels at the creative aspect of working with concepts that DEVONthink can't handle.   I store my .tbx documents in DEVONthink groups (folders) alongside the other documents I'm working with.  I don't do much writing using DEVONthink.  I use it for research, do the early working out of concepts and writing in Tinderbox, then finish off in whatever editor is appropriate for the particular final work product I have in mind.
Back to top
 
« Last Edit: Nov 30th, 2012, 10:36pm by Paul Walters »  
  IP Logged
russ lipton
Full Member
*
Offline



Posts: 126

Re: Exporting to DEVONthink using AppleScript
Reply #6 - Nov 30th, 2012, 11:24pm
 
Paul, thanks. To increase bandwidth and reduce noise, I have gone ahead and downloaded Devonthink Pro on trial and will give it a thorough going-over. Why not answer my own question first if possible, eh?
Back to top
 
 
  IP Logged
lisah2u
Full Member
*
Offline



Posts: 2

Re: Exporting to DEVONthink using AppleScript
Reply #7 - Dec 1st, 2012, 10:36am
 
Thanks Sumner & Paul!

Got the code note working. Very slick using the display group selector! It was my first time using a code note, so it took a bit of reading and fiddling. Now I feel like I have much better way to exchange information  with DT (in conjunction with the DT explode to TB script.)

It would be nice to have a compendium of all the sorts of useful Applescripts that people have come up with for Tinderbox.

Lisa
Back to top
 
 
  IP Logged
Paul Walters
Full Member
*
Offline



Posts: 267

Re: Exporting to DEVONthink using AppleScript
Reply #8 - Dec 1st, 2012, 12:37pm
 
Sumner Gerard wrote

Quote:
When exporting multiple Tinderbox notes at a time need to figure out how to call the group selector for the first note only and apply that choice to the other notes; otherwise it asks for each note.

Since the "on run argv" code is executed for each note, it's not a loop -- something that you can control in that part of the code.  Perhaps some sort outer loop or wrapper could get the group path ("location" in DEVONthink's record properties) and put it into an environment variable or store it in an attribute.
Back to top
 
« Last Edit: Dec 1st, 2012, 12:39pm by Paul Walters »  
  IP Logged
Sumner Gerard
Full Member
*
Offline



Posts: 359

Re: Exporting to DEVONthink using AppleScript
Reply #9 - Dec 2nd, 2012, 12:53am
 
@Paul

Yes, the looping and wrapping starts getting complicated for me, which is why I left it at incoming group, hoping experts here could show me the waySmiley .

The following, strictly speaking, is import rather than export, but still directly addresses Tinderbox-DEVONthink integration.

Quote:
using a DEVONthink item link (x-devonthink-item://) that was previously harvested from DEVONthink as the URL

You may have noticed working examples here and here that take advantage of TB's spreadsheet import to automatically create new notes with the links back to DEVONthink.

My minimalist approach to pulling the DEVONthink item link into an existing TB note is to place this in a code note named "GetDTlinkAS":

Code:
tell application id "DNtp"
	set mySelection to selection
	if the length of mySelection is 1 then return reference URL of first item in mySelection -- selection is always a list, so ask for item
end tell 



run by a stamp like this:

Code:
$URL=runCommand("osascript -e '"  +$Text("GetDTlinkAS") +"' ") 



enclosing the runCommand in an eval() if necessary.

This has no error handling but only does its thing after one and only one DT record and one or more TB notes have been selected.  It overwrites anything in $URL, of course, so change that to a new attribute named DtURI or whatever if needed.

@Lisa

I have a TBX with a collection of short AppleScripts that help integrate TB with other apps. Will clean it up and post elsewhere on the forum, probably after the holidays.
Back to top
 
 
  IP Logged
Paul Walters
Full Member
*
Offline



Posts: 267

Re: Exporting to DEVONthink using AppleScript
Reply #10 - Dec 2nd, 2012, 5:39am
 
Another great idea, Sumner

Here's a suggested tweak that adds error handling

Code:
tell application id "DNtp"
	if selection is not {} then
		return the reference URL of (the first item of (the selection as list))
	else
		return "Nothing selected in DEVONthink"
	end if
end tell 



By the way, everything in DEVONthink has a reference URL.  So, if the selection is a group or a DEVONthink Smart Group (i.e., a saved Search), then the reference URL of the group is what is returned.  Clicking the reference URL for a group will open a new window with that group hoisted in DEVONthink.  In the case where a Tinderbox note list "linked" to a collection of documents in DEVONthink, this might be useful.
Back to top
 
« Last Edit: Dec 2nd, 2012, 5:59am by Paul Walters »  
  IP Logged
Sumner Gerard
Full Member
*
Offline



Posts: 359

Re: Exporting to DEVONthink using AppleScript
Reply #11 - Dec 2nd, 2012, 4:38pm
 
Thanks for the error checking tweak, which works well. For those who don't mind living a little dangerously, Paul's approach inspired an even more minimalist way to bring a DEVONthink link into Tinderbox, with just one line of AppleScript in the code note:

Code:
tell application id "DNtp" to return reference URL of (first item of (selection as list)) 



Nice tip on reference URLs for DEVONthink groups and smart groups, not just for individual records. Had no idea.  That will be useful.
Back to top
 
« Last Edit: Dec 2nd, 2012, 4:40pm by Sumner Gerard »  
  IP Logged
Sumner Gerard
Full Member
*
Offline



Posts: 359

Re: Exporting to DEVONthink using AppleScript
Reply #12 - Mar 1st, 2013, 10:49pm
 
Here's a way to export notes to DEVONthink without the shell quoting fuss encountered upthread.

Action in a stamp I call Send2DNtp:

Code:
runCommand("automator -i - ~/tb2dntp.workflow",$Name+"\n"+$Text+"\n"+$URL) 



And this code in a Run AppleScript action in Automator saved as a workflow named tb2dntp.workflow in the path indicated in the stamp:

Code:
on run {input, parameters}
	set {this_name, this_text, this_url} to {item 1, item 2, item 3} of input
	tell application id "DNtp" to create record with {name:this_name, rich text:this_text, type:rtf, URL:this_url} in current group of current database
end run 



The two-argument form of runCommand feeds the values in $Name, $Text, and $URL to the command automator via stdin and the AppleScript in the Automator application receives the values as input. As described here  input is a list delimited by linefeed characters, hence the "\n" between the attributes in the stamp action code. (Any linefeeds within $Text will generate "extra" items in input that will need to be coerced back together in the AppleScript.)

Usage: With the target group in DEVONthink selected, in Tinderbox select the notes to export and apply the stamp.  If exporting hierarchy isn't critical, this can be more convenient than exporting to a file that then needs to be imported into DEVONthink.

Question: From reading here it seems Tinderbox stores formatted $Text as rtf internally. Is that purely internal or is there a way to export it? (The formatting is lost here.)
Back to top
 
« Last Edit: Mar 1st, 2013, 10:50pm by Sumner Gerard »  
  IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 5689
Southsea, UK
Re: Exporting to DEVONthink using AppleScript
Reply #13 - Mar 2nd, 2013, 4:41am
 
The page you refer to documents the internal XML of a TBX.  If you want to access the RTF data you need to read and parse the XML. TB $Text in an action code sense is plain, unstyled text. A simple way to prove that is to style some $Text and then apply the rule: $Text = $Text. You'll find all the styling has gone.

I've no idea how/if this will change in v6 but if you've specific $Text-related needs it's probably worth writing directly to Eastgate explaining what they are.

It's worth adding that ^text^ outputs HTML-styled text but which is a subset of the styling available in a note text window. Off the top of my head, styling not HTML exported: text colour/font/size, highlights, sub/superscript - there are probably a few others. The reason for this 'partial' export supports is the style features either post-date the original ^text^ export design and/or don't easily translate to HTML. CSS wasn't the thing it is today when TB was first designed.
Back to top
 
« Last Edit: Mar 2nd, 2013, 4:51am by Mark Anderson »  

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v6
(TB consulting - email me)
WWW shoantel   IP Logged
Sumner Gerard
Full Member
*
Offline



Posts: 359

Re: Exporting to DEVONthink using AppleScript
Reply #14 - Mar 3rd, 2013, 10:42pm
 
Thanks. Tinderbox for me hasn't been a natural place to compose/edit text, formatted or unformatted, so I haven't really thought much about formatting needs. Judging from impressively long entries on Mark B's blog composed in the upcoming version of TB, am guessing that may change, though. Seems markdown may be a good option when it's important to preserve formatting.
Back to top
 
 
  IP Logged
Pages: 1 2 
Send Topic Print