Integrating Bookends, Scrivener and Devonthink

Users asking other users for AppleScripts that work with Bookends.
Post Reply
NilsMS
Posts: 56
Joined: Mon Sep 16, 2013 10:13 am

Integrating Bookends, Scrivener and Devonthink

Post by NilsMS »

Taking up my own suggestion (viewtopic.php?f=2&t=3444) I have written an applescript in order to integrate my wordprocessor of choice (Scrivener) with DevonthinkProOffice and Bookends more closely (borrowing from several other scripts, especially from this http://forum.devontechnologies.com/view ... 20&t=18047 by Korm). Often it is the case that I do not remember what the cited reference was exactly about. This script automatically links the references in Scrivener to Bookends and – when the respectivce link in Bookends is present (see my script here: viewtopic.php?f=6&t=3454) - also to the document in DTPO. Then only the reference has to be clicked to find the entry in Bookends (or DTPO).
It will copy the selected reference in Bookends, switch to Scrivener and paste the unformatted reference to the cursor position. In doing this it will link the Bookends-ID to Bookends and the group "Author Year" to DTPO (I store the DTPO-reference-URL in Bookends in userfield 20).
The unformatted link looks a little bit different than the normal output from Bookends, but Bookends does not seem to have a problem to still identify the correct reference when formatting the bibliography. However, before the final Scan-command can be invoked the document has to be stripped from all hyperlinks. The easiest way to do this seems to be in Microsoft Word (select all and then cmd-6) but I would be eager to hear about other solutions.
My only aesthethic problem with the script is that diacritic letters look awful. Somewhere along the script letters like "ä", "ö", "ø" etc. are changed in something like "ö", despite the fact that the shell script retains the RTF-format. Perhaps anyone knows of an easy way to remedy this?

Cheers,

Nils

Code: Select all

tell application "Bookends"
	set theIDString to «event DNtpRUID» "selection"
	set theauthor to «event ToySRFLD» theIDString given string:"authors"
	set theyear to «event ToySRFLD» theIDString given string:"thedate"
	set devonthinkReference to «event ToySRFLD» theIDString given string:"user20"
	set authorYear to theauthor & ", " & theyear as string
	if devonthinkReference is "" then
		set theReference to "{" & authorYear & ", #" & ("<font face=\"times new roman\"><a href=\"" & "bookends://sonnysoftware.com/" & theIDString & "\">" & theIDString & "</a></font>") & "}"
	else
		set theReference to "{" & ("<font face=\"times new roman\"><a href=\"" & devonthinkReference & "\">" & authorYear & "</a></font>") & ", #" & ("<font face=\"times new roman\"><a href=\"" & "bookends://sonnysoftware.com/" & theIDString & "\">" & theIDString & "</a></font>") & "}"
	end if
	set RTFtheReference to (do shell script "echo " & quoted form of theReference & " | textutil -stdin -stdout -format html -convert rtf | pbcopy -Prefer rtf")
	
end tell

tell application "Scrivener"
	activate
end tell

tell application "System Events"
	key code 9 using {command down} -- v key
end tell

Jon
Site Admin
Posts: 8672
Joined: Tue Jul 13, 2004 6:27 pm
Location: Bethesda, MD
Contact:

Re: Integrating Bookends, Scrivener and Devonthink

Post by Jon »

The accented characters are encoded as UTF-8. When rendered as ASCII, they appear as garbage (the 3 characters in your example are typical). RTF doesn't contain UTF-8 characters -- instead, it creates Unicode entities that represent the character (in RTF, they begin with \u and are followed by a number).

Jon
Sonny Software

NilsMS
Posts: 56
Joined: Mon Sep 16, 2013 10:13 am

Re: Integrating Bookends, Scrivener and Devonthink

Post by NilsMS »

Jon wrote:The accented characters are encoded as UTF-8. When rendered as ASCII, they appear as garbage (the 3 characters in your example are typical). RTF doesn't contain UTF-8 characters -- instead, it creates Unicode entities that represent the character (in RTF, they begin with \u and are followed by a number).
Thanks for the reply! I begin to see where the problem lies, but is there an easy solutions? I have played around with different formats, but so far nothing has worked.

Cheers,

Nils

Jon
Site Admin
Posts: 8672
Joined: Tue Jul 13, 2004 6:27 pm
Location: Bethesda, MD
Contact:

Re: Integrating Bookends, Scrivener and Devonthink

Post by Jon »

I don't understand exactly what you're doing well enough to say. All I can tell you is that the RTF is malformed if it contains UTF-8 characters. RTF is ASCII only (and even then, 7 bit ASCII). UTF-8 must be "encoded" in RTF so that RTF readers can render the characters properly.

Jon
Sonny Software

NilsMS
Posts: 56
Joined: Mon Sep 16, 2013 10:13 am

Re: Integrating Bookends, Scrivener and Devonthink

Post by NilsMS »

Jon wrote:I don't understand exactly what you're doing well enough to say. All I can tell you is that the RTF is malformed if it contains UTF-8 characters. RTF is ASCII only (and even then, 7 bit ASCII). UTF-8 must be "encoded" in RTF so that RTF readers can render the characters properly.
For those interested: I finally found a – in the end very obvious – solution for the encoding problem. The utility "textutil" used in the script simply needs to know the input encoding (provided by the string "-inputencoding utf-8"). The whole script - with some more refinements – is shown below.

Cheers,

Nils

Code: Select all

tell application "Bookends"
	set theIDString to «event DNtpRUID» "selection"
	set theauthor to «event ToySRFLD» theIDString given string:"authors"
	if theauthor is "" then
		set theauthor to «event ToySRFLD» theIDString given string:"editors"
		if theauthor is "" then
			set theauthor to "Anon."
		end if
	end if
	set theyear to «event ToySRFLD» theIDString given string:"thedate"
	set devonthinkReference to «event ToySRFLD» theIDString given string:"user20"
	
	set oldDelimiters to AppleScript's text item delimiters
	set AppleScript's text item delimiters to linefeed
	set theauthorList to every text item of theauthor
	set theauthorListCount to length of theauthorList
	set AppleScript's text item delimiters to ", "
	set ErsterAutor to (first item of theauthorList as string)
	set theauthorVollname to every text item of ErsterAutor
	if theauthorListCount = 1 then
		set theauthor to (first item of theauthorVollname as string)
	else if theauthorListCount > 2 then
		set theauthor to (first item of theauthorVollname as string) & " u. a."
	else if theauthorListCount = 2 then
		set ZweiterAutor to (second item of theauthorList as string)
		set theauthorVollname2 to every text item of ZweiterAutor
		set theauthor to (first item of theauthorVollname as string) & "/" & (first item of theauthorVollname2 as string)
	end if
	set AppleScript's text item delimiters to oldDelimiters
	
	set authorYear to theauthor & ", " & theyear
	if devonthinkReference is "" then
		set theReference to "{" & authorYear & ", #" & ("<font face=\"times new roman\"><a href=\"" & "bookends://sonnysoftware.com/" & theIDString & "\">" & theIDString & "</a></font>") & "}"
	else
		set theReference to "{" & ("<font face=\"times new roman\"><a href=\"" & devonthinkReference & "\">" & authorYear & "</a></font>") & ", #" & ("<font face=\"times new roman\"><a href=\"" & "bookends://sonnysoftware.com/" & theIDString & "\">" & theIDString & "</a></font>") & "}"
	end if
end tell

set RTFtheReference to (do shell script "echo " & quoted form of theReference & " | textutil -stdin -stdout -inputencoding utf-8 -format html -convert rtf | pbcopy")
tell application "Scrivener"
	activate
end tell

tell application "System Events"
	key code 9 using {command down} -- v key
end tell

Post Reply