Autolinking Bookends and DEVONthink records

Users asking other users for AppleScripts that work with Bookends.
zvh
Posts: 32
Joined: Sun Aug 27, 2017 12:47 am

Re: Autolinking Bookends and DEVONthink records

Post by zvh »

Jon can correct me if I'm wrong, but I'm not sure that there's yet a way to add attachments to existing records using AppleScipt. You might be able to cobble something together by using Finder's library to make the move, but it would likely be finicky and wouldn't, for instance, utilize your Bookends renaming preferences.

If I'm wrong and that function does exist, then I could probably script something rather easily.
Jon
Site Admin
Posts: 10202
Joined: Tue Jul 13, 2004 6:27 pm
Location: Bethesda, MD
Contact:

Re: Autolinking Bookends and DEVONthink records

Post by Jon »

Yes, you can add attachments to existing records using AppleScript. Please check out Bookends' AppleScript dictionary, and see the documentation, or this link to our sites page on scripting Bookends, for more information.

https://www.sonnysoftware.com/scriptingbookends/

Jon
Sonny Software
zvh
Posts: 32
Joined: Sun Aug 27, 2017 12:47 am

Re: Autolinking Bookends and DEVONthink records

Post by zvh »

Thanks for clarifying, Jon. I've just looked through the manual and dictionary again and can't figure out how to add an attachment item to a currently existing publication item--at least in a way that will import and rename the file. I can easily add a new publication item, whether via quick add or as a record, and can do things with the attachments of current pub items, but could you point me to the part of the documentation where it specifies importing new attachments to existing references?

No worries if not. I know you're busy and this is more for tinkering than anything else. Not at all an urgent problem and, again, the problem is entirely on my end.
Jon
Site Admin
Posts: 10202
Joined: Tue Jul 13, 2004 6:27 pm
Location: Bethesda, MD
Contact:

Re: Autolinking Bookends and DEVONthink records

Post by Jon »

No problem. Fetch the names of the existing attachments (in the "attachments" field, append the new one (the names are separated with \n), then send the edited attachments field back to Bookends to replace the original.

Jon
Sonny Software
zvh
Posts: 32
Joined: Sun Aug 27, 2017 12:47 am

Re: Autolinking Bookends and DEVONthink records

Post by zvh »

Thanks so much for your help!

This doesn't seem to import the actual file from its original path though, does it? (Or should it?) I can add to the attachments field, but this just returns "Can't find file" in Bookends, while the item in the attachment items list is incomplete. Here's part of what I've been experimenting with, for reference:

Code: Select all

tell application id "DNtp"
	set sel to the selection
	set thePath to path of item 1 of sel
	set theName to name of item 1 of sel
	set newItem to {path:thePath, name:theName, id:theName}
	tell application "Bookends"
		tell front library window
			set theRecord to publication items whose id is theQuery
			set theItem to item 1 of theRecord
			set theAttachments to attachments of theItem
			set theAttachments to theAttachments & linefeed & thePath
			# set theAttachments to theAttachments & linefeed & theName
			# add newItem to attachment items of publication item theRecord
			# set theAttachments to attachment items of item 1 of theRecord
			# add {path:thePath, name:theName, id:theName} to attachment items of item 1 of theRecord
			# copy newItem to end of theAttachments
		end tell
	end tell
end tell
Jon
Site Admin
Posts: 10202
Joined: Tue Jul 13, 2004 6:27 pm
Location: Bethesda, MD
Contact:

Re: Autolinking Bookends and DEVONthink records

Post by Jon »

It doesn't import anything at all. It just tells Bookends the reference is attached to the file with the name you added, and it will look in the default attachment folder for that file. As I wrote, actually getting the pDF into the default attachment folder is up to you.

Jon
Sonny Software
fmeres
Posts: 12
Joined: Sun Apr 14, 2019 1:23 pm

Re: Autolinking Bookends and DEVONthink records

Post by fmeres »

As I understand it in order for the script to work then it should have DT export the pdf to the Bookends attachment folder and have Bookends add the file to the pre-existing (selected) reference, right?
Jon
Site Admin
Posts: 10202
Joined: Tue Jul 13, 2004 6:27 pm
Location: Bethesda, MD
Contact:

Re: Autolinking Bookends and DEVONthink records

Post by Jon »

That is correct. By setting adding the PDF name to the Bookends database all you're telling Bookends to do is *look* in the default folder for the PDF of that name. Bookends won't move the file there.

Jon
Sonny Software
Dellu
Posts: 269
Joined: Sun Mar 27, 2016 5:30 am

Re: Autolinking Bookends and DEVONthink records

Post by Dellu »

Hi Zach,
I was trying to modify the script to change the creation date of the pdf file to the publication date.

Code: Select all

{theKey, thePath, theAuthor, theEditor, theTitle, theWords, theDate} to {citekey, path of attachment items, authors, editors, title, keywords, publication date string} of myItem

Code: Select all

do shell script "/usr/local/bin/exiftool -title=" & quoted form of theTitle & " -author=" & quoted form of theAuthor & "  -keywords=" & quoted form of theWords & " -fileCreateDate=" & quoted form of theDate & " -overwrite_original " & quoted form of thisPath

can you help me please?
I couldn't figure out why this cannot change the creation date.
Dellu
Posts: 269
Joined: Sun Mar 27, 2016 5:30 am

Re: Autolinking Bookends and DEVONthink records

Post by Dellu »

I found a way to do it now. The date formatting was the problem.

Adding the following line correctly imparted the creation date from date of the publication.

Code: Select all

set {theKey, thePath, theAuthor, theEditor, theTitle, theWords, theDate} to {citekey, path of attachment items, authors, editors, title, keywords, publication date string} of myItem

Code: Select all

set MyDay to (do shell script "date '+:%m:%d %H:%M:%S'")
		set MyDate to (theDate & MyDay)

Code: Select all

do shell script "/usr/local/bin/exiftool -title=" & quoted form of theTitle & " -author=" & quoted form of theAuthor & "  -keywords=" & quoted form of theWords & " -CreateDate=" & MyDate & " -overwrite_original " & quoted form of thisPath
zvh
Posts: 32
Joined: Sun Aug 27, 2017 12:47 am

Re: Autolinking Bookends and DEVONthink records

Post by zvh »

Glad to see you figured that out, Dellu.

Also, sorry to be late on replying again to the previous query regarding exporting from DTPO to currently existing Bookends records. Adding the record to Bookends, then, is pretty simple. What you'd do to move the item is either get the path from DEVONthink and then tell Finder to move the file to your Bookends library, or else move the record within DEVONthink to your indexed Bookends folder (if you have one) and then deconsolidate the record to add it to the external folder. You could also rename it in either case by retrieving the reference info from Bookends that corresponds to your naming format and then telling DEVONthink to rename the item.

However, for me at least, this is more effort (and more breakable) than it's worth, given that you can just drag and drop from DEVONthink into Bookends. I agree that it would be nice to select a reference to import a file to without having to have both apps open side-by-side with the record selected--and this is doable--but I, at least, have my files renamed (and special characters removed) through Bookends. Doing this guarantees consistency. So, while this could mostly be hacked together without the aid of Bookends import, for me personally it's something best left to Bookends.
mk7
Posts: 30
Joined: Tue Nov 27, 2018 11:08 am

Re: Autolinking Bookends and DEVONthink records

Post by mk7 »

Hi, you'll have to excuse my ignorance here, but I've been trying to work this out and just can't swing it. I want to be able to use the script that works on selected items, but instead of creating a linked record in DT with the PDF, I want to create a blank note. I modified part of it so that it reads:

Code: Select all

set theRecord to create record with {type:markdown, name:theAuthor & " " & "-" & theTitle} in theLocation
Except for some reason that I can't figure out it still only works if there's a PDF attached to the reference. Is there any way to disable that? I tried removing these lines, but it didn't help:

Code: Select all

set thePath to path of attachment items of myItem
if thePath is not {} then
Also, you mentioned in the original post that you use the wikilink feature to associate your notes with the item in DT--would you mind explaining how that works? Where do you put your notes? Are they automatically created somehow? Thanks!
zvh
Posts: 32
Joined: Sun Aug 27, 2017 12:47 am

Re: Autolinking Bookends and DEVONthink records

Post by zvh »

I think the section giving you problems now is

Code: Select all

repeat with i in thePath
or rather any remaining usage of the variable "thePath" insofar as you no longer create that variable when you remove this line:

Code: Select all

set thePath to path of attachment items of myItem
Those checks were created to make sure the script didn't try to process a reference without an attachment, so removing them should allow you to create items in DEVONthink rather than indexing pre-existing attachments.

As for the wikilink section of my comments, I was noting that DEVONthink will (if the option is turned on in preferences) automatically create links in your text notes to any item in that database whose alias is the same as the text in the note. So, for instance, if my BibKey for an item is "Smith2019" and I make this the alias of the reference I index in DEVONthink, then any time I want to link to this, all I have to do is put "Smith2019" in my note and it will link me to however many other files (notes or pdf) have that as an alias. How I actually create my notes is actually a bit too elaborate to write out at the moment--and also involves a couple of scripts that are a bit idiosyncratic. But I'll try to do so when I get some free time.

Hope this helps!
mk7
Posts: 30
Joined: Tue Nov 27, 2018 11:08 am

Re: Autolinking Bookends and DEVONthink records

Post by mk7 »

That did it! Thanks so much.

Trying to figure out the ideal writing/referencing workflow, so if you get a moment would love to hear how you swing it. Also trying to make this work on iOS, which is what I primarily work on. Thanks again.
Malcolm
Posts: 1
Joined: Wed Dec 08, 2021 11:31 am

Re: Autolinking Bookends and DEVONthink records

Post by Malcolm »

Very useful script for me as I store my documents in DevonThink while using Bookends to manage citations. I've managed to adapt the script to my own setup include the paths to the bookend attachment folder and created a Keyboard Maestro script to run it.

What seems to be working
  • USER20 is populated with the Devonthink link to the attachment
  • The finder comment contains the RIS info of the paper
What doesn't seem to work yet is setting the URL in Devonthink to the Bookends link. I'm not sure why. I've tried profiling the script in the AppleScript editor and looking at the events for the critical steps including setting the DT URL field all looks ok (see extract below). However the
bookends://sonnysoftware.com/92892
below never makes it into the DT URL. If I copy it by hand all works as expected.
tell application "DEVONthink 3"
open database "/Users/MalcolmDavidson/Documents/Database/Work.dtBase2"
create location "/Users/MalcolmDavidson/Documents/Database/BookendsAttachments"
indicate "/Users/MalcolmDavidson/Documents/Database/Bookends/BookendsAttachments/Ardhuin_F_etal_SKIM,_a_candidate_satellite_mission_exploring_global_ocean_currents_and_waves_Frontie.pdf" to parent id 4118023 of database id 2
set URL of content id 4146190 of database id 2 to "bookends://sonnysoftware.com/92892"
set aliases of content id 4146190 of database id 2 to ""
set comment of content id 4146190 of database id 2 to "TY - JOUR
AU - Ardhuin, Fabrice
AU - Brandt, Peter
AU - Gaultier, Lucile
AU - Donlon, Craig
etc...
Hoping I can solve this final hurdle with your help.
Post Reply