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

Autolinking Bookends and DEVONthink records

Post by zvh »

Hi all,

I thought I might contribute another script that I recently completed and that seems to work like a charm.

I've read some users of both Bookends and DEVONthink wish (on both forums) that you could automatically link records between the two softwares. The script located on the gist I've just posted, provided your files correspond to a certain naming convention, should allow you to do this. I've just run it on my own Bookends library of over 3500 records and can't find a single instance of a bad or incorrect link.

By link I mean that I automatically place the Bookends URL in the URL field of the record in DEVONthink and then the DEVONthink item link in the user20 field of the Bookends record. Not only that but I place the RIS data from Bookends into the comment field of DEVONthink and make the BibKey its alias. This means that, should you turn on Wikilinks in DEVONthink, you'll be able to use the BibKey as a link, whether to the original document or, for my purposes, to notes for the document. Both the RIS and BibKey records also serve as forms of redundancy that would allow you to get all the cite information (and essentially reconstitute your entire digital library with records) if need be.

Finally, the script passes any pdf through exiftool on the command line to place the author (or editor), title, and BibKey (as subject) records into the pdf itself.

Now, what the script does is loops through an entire Bookends SQL Smart Group, which I've set up using the following parameters:

Code: Select all

(attachments IS NOT NULL) AND (user20 IS NULL) AND (thedate IS NOT NULL) AND (attachments REGEX '^\S*\d{4}[a-z]?-\S*?(?=\.).(pdf|epub|mobi|md)')
So, what this says is that the record to be processed has to have attachments (or else there's nothing to link to), it can't already be linked (you don't want to do this to the same file multiple times but do want to know which ones you haven't yet processed, so user20 has to be empty), and also has to have a date (as the BibKey is the unique thing about my file naming system that guarantees a match when searching in DEVONthink). It also has to have an attachment that corresponds to the given RegEx, because I don't want to have to rename the pdf later and threaten breaking the links (although then I'd just reprocess, which would replenish all the lost metadata).

I've named this group "Unlinked Attachments," and you'd need to change this, as well as the location of your DEVONthink library location, if you decided to use a version of the script. Also, and this is essential: ensure your naming system produces unique names. Otherwise I don't think this script would be quite as effective and may produce false matches in the search function.

Caution: As this script does things to your files, please a) don't use it if you don't know enough AppleScript to know what the script is doing in order to be able to alter it to your own specifications; b) perform a backup of your Bookends library, your digital files, and your DEVONthink database prior to using the script; and c) consider using the second gist linked to below to start, which will just try this on a single file that you've selected in Bookends. This should allow you to test it with your own file, naming, and database structures. Your DEVONthink database does need to be open but you don't have to select anything inside of it.

As I said, this cycled through 3500 references, and even seems to have achieved a correct match in cases where file names were nearly identical or multiple files were attached to a single Bookends record. Again, this script will have different applications for anyone who chooses to use it. It's so individual to each user that I can't offer to customize it for anyone. I just wanted to show how something like this might be done.

All best,

Zach

Link all records in group: https://gist.github.com/zverhope/86c5f7 ... 1c0a3dc241

Link selected record: https://gist.github.com/zverhope/9e9e55 ... 01622fbf12
Dellu
Posts: 268
Joined: Sun Mar 27, 2016 5:30 am

Re: Autolinking Bookends and DEVONthink records

Post by Dellu »

This is amazing. Thank you for putting it out. I am going to use it.

Two comments/questions:
  • USER20: is not a clickable field in BE. Is there any way to make it clickable so that we can directly jump to Devonthink using the link?
  • How will these links react in the ios? Does clicking the link in Devonthink take to the BE library in the ios?
zvh
Posts: 32
Joined: Sun Aug 27, 2017 12:47 am

Re: Autolinking Bookends and DEVONthink records

Post by zvh »

Dellu wrote: Wed Mar 13, 2019 7:27 am This is amazing. Thank you for putting it out. I am going to use it.

Two comments/questions:
  • USER20: is not a clickable field in BE. Is there any way to make it clickable so that we can directly jump to Devonthink using the link?
  • How will these links react in the ios? Does clicking the link in Devonthink take to the BE library in the ios?
Hi Dellu (and any other interested party),

I've simplified the script above so that it should work for any file naming scheme, regardless of whether there is a unique identifier included in the file name. This version will actually do the indexing for you, so instead of linking to already indexed files by search, this creates the index and makes links between the files in the process. This should be much less fallible than the original version would be. It will also, of course, work just fine with a folder hierarchy, so you no longer need a flat structure. The only thing you should really have to customize here is the path to your database and the location within the database you would like all of the files to be indexed to.

Also, the new version utilizes Bookends new scripting library, which makes everything so much cleaner.

As for your first question, I use a script in Bookends (activated through Keyboard Maestro) to automatically open the link in the user20 field. The script is:

Code: Select all

tell application "Bookends"
	set theID to «event ToySRUID» "Selection"
	set theLink to «event ToySRFLD» theID given string:"user20"
end tell

set theLink to paragraphs of theLink
if (count of theLink) is greater than 1 then
	set theLink to choose from list theLink with multiple selections allowed
end if

repeat with i in theLink
	do shell script "open " & i as string
end repeat
I can't really answer your second question, unfortunately, although it should be pretty easy to find out. I've purchased both the iOS versions of DTPO and Bookends, but don't quite find the latter to be fully featured enough yet to get any use from it. Granted, that's more than likely because I haven't quite put the time in to figuring out how to make it work. Hope that helps.
Dellu
Posts: 268
Joined: Sun Mar 27, 2016 5:30 am

Re: Autolinking Bookends and DEVONthink records

Post by Dellu »

zvh wrote: Fri Mar 15, 2019 1:30 am As for your first question, I use a script in Bookends (activated through Keyboard Maestro) to automatically open the link in the user20 field. The script is:
are you reading the files in Devonthink?
I am wondering why you want to jump from BE to Devonthink.

This is a very useful article. I love fact that you incorporated the XML writing into it. I used to do it separately.

Thank you so much.
Dellu
Posts: 268
Joined: Sun Mar 27, 2016 5:30 am

Re: Autolinking Bookends and DEVONthink records

Post by Dellu »

i am getting error :
Can't get item 1 of {}
zvh
Posts: 32
Joined: Sun Aug 27, 2017 12:47 am

Re: Autolinking Bookends and DEVONthink records

Post by zvh »

Dellu wrote: Mon Mar 18, 2019 4:37 am i am getting error :
Can't get item 1 of {}
Hmmm, are you using the new script that indexes the "Unlinked Attachments" group? If so, what line does that error message relate to if you run it from Script Editor? This indicates you have an empty list and could indicate that either the script isn't picking up anything from the group or that there are items in the group that have no attachments (and therefore no items to index). Those are the only two instances where the script calls for iterating through items in list, so it's likely one of those two parts of the script that are causing you problems.
Dellu
Posts: 268
Joined: Sun Mar 27, 2016 5:30 am

Re: Autolinking Bookends and DEVONthink records

Post by Dellu »

I am getting this when I use the "selection" script.
Selecting one item doesn't trigger the error. Selecting two or more items in the library and running the script triggers the error.

Yes, I am running it in the Script editor.
and, they have attachmentsImage


On the positive side, the script is working fine on the group. I am using that one now.
zvh
Posts: 32
Joined: Sun Aug 27, 2017 12:47 am

Re: Autolinking Bookends and DEVONthink records

Post by zvh »

Dellu wrote: Wed Mar 20, 2019 4:05 am I am getting this when I use the "selection" script.
Selecting one item doesn't trigger the error. Selecting two or more items in the library and running the script triggers the error.

Yes, I am running it in the Script editor.


On the positive side, the script is working fine on the group. I am using that one now.
Thanks for this! I hadn't tested the selection version extensively as I use the group version exclusively. The selection script should now work with multiple selections. It also now knows to bypass items without attachments that might be in your selection.
Dellu
Posts: 268
Joined: Sun Mar 27, 2016 5:30 am

Re: Autolinking Bookends and DEVONthink records

Post by Dellu »

That is great.
Thanks for these cool scripts man.
fmeres
Posts: 12
Joined: Sun Apr 14, 2019 1:23 pm

Re: Autolinking Bookends and DEVONthink records

Post by fmeres »

This script is awesome! I'm wondering if there is a way to add the functionality to import keywords as tags to DT as well. That would be amazing!
fmeres
Posts: 12
Joined: Sun Apr 14, 2019 1:23 pm

Re: Autolinking Bookends and DEVONthink records

Post by fmeres »

I think I was able to figure it out, you need to modify the following lines of the script under "tell application "Bookends" to include the tag/keyword info:

Code: Select all

set {theKey, theAuthor, theEditor, theTitle, theKeywords} to {citekey, authors, editors, title, keyword names}
And add the following line to the "tell application id "DNtp""part:

Code: Select all

set tags of theRecord to theKeywords
zvh
Posts: 32
Joined: Sun Aug 27, 2017 12:47 am

Re: Autolinking Bookends and DEVONthink records

Post by zvh »

I'm so glad you're finding the script useful and were able to modify it to your specifications. You can do a lot of extra things with the script, including sharing tags and labels. I have a Bookends label, for instance, that is intended to specify items I want on hand but don't necessarily want cluttering my DEVONthink search results. Stuff, for instance, like the complete works of Aristotle. I could never use that for my research, but I still want it in my library. At the same time, I don't need it slowing down my searches or cluttering up any magic hat results. So, with this script, I can have DEVONthink turn off the search and see also results for any item that has that label in Bookends. Stuff like that is quite useful.

You can also then write scripts that use the link kept in the user20 field to update various portions of the metadata between apps. It opens a whole bunch of possibilities, depending on your particular preferences.
fmeres
Posts: 12
Joined: Sun Apr 14, 2019 1:23 pm

Re: Autolinking Bookends and DEVONthink records

Post by fmeres »

That's a great idea!

I've been actually wondering about further possibilities of syncing the script could offer. The main one that would be really productive would some form of cross-syncing of PDFs from DT to Bookends. For instance, I have many pdfs of articles and books in DT that I could like to use in references in Bookends. If the script could somehow import these PDFs from DT to Bookends it would be fantastic. It would also be great if when I run the script in Bookends on a reference that has a PDF that is already in my DT library, that it would recognize it somehow and not duplicate it but just become linked to it or import it to the Bookends reference. I don't know if these actions are feaseble but thought I'd put them out there!

Thanks again for such a great script, it's really helping my research!
zvh
Posts: 32
Joined: Sun Aug 27, 2017 12:47 am

Re: Autolinking Bookends and DEVONthink records

Post by zvh »

There is a "Send to Bookends" script in DEVONthink that can easily be modified to serve the purpose of getting your items from DTPO to Bookends. The one provided is quite basic, but extending it is mostly trivial, especially with Bookends' new scripting library.

As for checking whether a reference is already in DTPO, you might look at this script from the DTPO forums: https://discourse.devontechnologies.com ... es/24989/8

As the script I've posted puts the Bookends URL in the URL field of the DTPO item, you should just be able to do something like the above, but check for the presence of the Bookends URL in DTPO rather than a Safari link. You could also do the same the other way around by checking for the presence of the DEVONthink URL in Bookends' user20 field.
fmeres
Posts: 12
Joined: Sun Apr 14, 2019 1:23 pm

Re: Autolinking Bookends and DEVONthink records

Post by fmeres »

Thank you for the tips. I'm finding the Export to Bookends script very handy. I'm just having an issue with one thing: how can I modify the script to have DT export the PDF to a pre-existing citation selected in Bookends? I spent a couple of hours trying to edit the script but no luck so far. Any tips would be greatly appreciated!
Post Reply