Cite-as-you-write

Users asking other users for AppleScripts that work with Bookends.
Post Reply
kseggleton
Posts: 39
Joined: Fri Jan 01, 2016 6:47 am

Cite-as-you-write

Post by kseggleton »

As suggested by Jon I am posting my Bookends Alfred cite-as-you-write workflow here. The workflow can be downloaded at: http://www.alfredforum.com/topic/8817-b ... citations/

The overview of the workflow is:

Image

The first step is a script filter that sends stdout to some bash scripts. The code for the script filter is:

Code: Select all

--------------------------------------------------------------------
-- SCRIPT FOR EXTRACTING REFERENCES FROM BOOKENDS
--------------------------------------------------------------------

on run argv
	set query to argv as text
	tell application "Bookends"
		
		-- Extract UUID from Bookends
		
		set AppleScript's text item delimiters to {return}
		set refList to text items of («event ToySSQLS» "authors REGEX '" & query & "'")
		set AppleScript's text item delimiters to {","}
		
		
		
		set xml to "<?xml version=\"1.0\"?>" & linefeed
		set xml to xml & "<items>" & linefeed
		
		
		
		repeat with refItem in refList
			set refItem to contents of refItem
			
			-- Extract first Author
			
			set refAuthorList to («event ToySRFLD» refItem given string:"authors")
			set AppleScript's text item delimiters to {","}
			set refAuthors to text items of refAuthorList
			set AppleScript's text item delimiters to {"'"}
			set refAuthor to first item of refAuthors
			set AppleScript's text item delimiters to {""}
			
			
			-- Extract and clean title to HTML friendly title
			
			set refTitle to («event ToySRFLD» refItem given string:"title")
			set findChars to {"&", "\"", "'", "<", ">"}
			set replaceChars to {"&", """, "&apos;", "<", ">"}
			repeat with i from 1 to 5
				if (item i of findChars) is in refTitle then
					set AppleScript's text item delimiters to {item i of findChars}
					set refTitle to text items of refTitle
					set AppleScript's text item delimiters to {item i of replaceChars}
					set refTitle to refTitle as text
					set AppleScript's text item delimiters to {""}
				end if
			end repeat
			
			
			-- Extract date
			
			set refDateList to («event ToySRFLD» refItem given string:"thedate")
			set regexDef to "([12][0-9][0-9][0-9])"
			set refDate to (do shell script "if [[ \"" & refDateList & "\" =~ " & regexDef & " ]]
then echo \"${BASH_REMATCH[1]}\"
fi")
			
	
			
			-- XML formatting
			-- Set XML header
			
			
			set xml to xml & linefeed & "<item uid=\"" & refItem & "\" arg=\"" & refItem & "\" valid=\"yes\">" & linefeed
			set xml to xml & "<title>"
			set xml to xml & refAuthor & " " & refDate
			set xml to xml & "</title>" & linefeed
			set xml to xml & "<subtitle>"
			set xml to xml & refTitle
			set xml to xml & "</subtitle>" & linefeed
			set xml to xml & "<icon>file.png</icon>" & linefeed
			set xml to xml & "</item>" & linefeed
		end repeat
		return xml
		
		
	end tell
end run

The first script, following on from the script filter, copies the citation to the clipboard in {Author Date UUID} format

Code: Select all

-------------------------------------------------------------------------
-- SCRIPT FOR SETTING CITATION USING {AUTHOR, DATE #UID}
-------------------------------------------------------------------------
on run argv
	set refItem to argv as text
	
	tell application "Bookends"
		
		-- Extract first Author
		
		set refAuthorList to («event ToySRFLD» refItem given string:"authors")
		set AppleScript's text item delimiters to {","}
		set refAuthors to text items of refAuthorList
		set AppleScript's text item delimiters to {"'"}
		set refAuthor to first item of refAuthors
		set AppleScript's text item delimiters to {""}
		
		-- Extract date
		
		set refDateList to («event ToySRFLD» refItem given string:"thedate")
		set regexDef to "([12][0-9][0-9][0-9])"
		set refDate to (do shell script "if [[ \"" & refDateList & "\" =~ " & regexDef & " ]]
then echo \"${BASH_REMATCH[1]}\"
fi")
		
		
		-- Set citation
		
		set refCitation to "{" & refAuthor & ", " & refDate & ", #" & refItem & "}"
		set the clipboard to {Unicode text:refCitation}
		-- return refCitation
		
		
	end tell
	
	
end run
The second script, a bash script and accessed by the 'command' key copies a MultiMarkdown formatted citation to the clipboard

Code: Select all

UUID="{query}"
BIBKEY=$(/usr/bin/osascript << EOT
tell application "Bookends"
    return «event ToySRFLD» "$UUID" given string:"user1"
end tell
EOT)
echo "[#$BIBKEY]" | tr -d '\n'
The third script, also a bash script and accessed by the 'option' key, copies a Pandoc formatted citation to the clipboard

Code: Select all

UUID="{query}"
BIBKEY=$(/usr/bin/osascript << EOT
tell application "Bookends"
    return «event ToySRFLD» "$UUID" given string:"user1"
end tell
EOT)
echo "@$BIBKEY" | tr -d '\n'
The fourth script (not in the Alfred script that I have posted and accessed through the 'control' key) opens a DevonThink pdf that is linked to Bookends by a URL that I keep in 'user4'

Code: Select all

UUID="{query}"
BIBKEY=$(/usr/bin/osascript << EOT
tell application "Bookends"
    return «event ToySRFLD» "$UUID" given string:"user4"
end tell
EOT)
open $BIBKEY
In the workflow there is a second script filter. This is the same as the first script filter but just accessed through a separate Alfred shortcut. It sends stdout to two bash scripts. The first copies a formatted bibliography to the clipboard:

Code: Select all

UUID="{query}"
REFNAME=$(/usr/bin/osascript << EOT
tell application "Bookends"
    return «event ToySGUID» "$UUID" given «class RRTF»:"true"
end tell
EOT)
echo "$REFNAME" | pbcopy
The second bash script accessed through the 'command' key (copies a MultiMarkdown formatted bibliography (Using the Vancouver style) to the clipboard as in [Reference]: bibliography

Code: Select all

UUID="{query}"
BIBKEY=$(/usr/bin/osascript << EOT
tell application "Bookends"
    return «event ToySRFLD» "$UUID" given string:"user1"
end tell
EOT)
REFNAME=$(/usr/bin/osascript << EOT
tell application "Bookends"
    return «event ToySGUID» "$UUID" given «class RRTF»:"false", string:"Vancouver"
end tell
EOT)
echo "[#$BIBKEY]: $REFNAME"
Hope this helps. As I said I have no idea how Launch bar works but you might be able to reconfigure it for your use

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

Re: Cite-as-you-write - diacritic signs

Post by NilsMS »

After some trouble-shooting, I found the solution for the problem with the diacritic signs ("Umlaute").

Code: Select all

set query to argv as text
has to be replaced with

Code: Select all

set query to (do shell script "echo " & argv & " | iconv -s -f UTF-8-Mac -t UTF-8") as Unicode text
This works at least for me.

This workflow really makes placing references a treat!

Cheers,

Nils

kseggleton
Posts: 39
Joined: Fri Jan 01, 2016 6:47 am

Re: Cite-as-you-write

Post by kseggleton »

Thanks for that Nils. I will update my Alfred script with your changes

Post Reply