Cite-as-you-write

A place for users to ask each other questions, make suggestions, and discuss Bookends.
Post Reply
kseggleton
Posts: 43
Joined: Fri Jan 01, 2016 6:47 am

Cite-as-you-write

Post by kseggleton »

I have created a cite-as-you-write Alfred workflow that may interest some people. The link to the Alfred workflow is here http://www.alfredforum.com/topic/8817-b ... citations/

The workflow enables you to search Bookends, for an author, with a keyboard shortcut and then paste a Bookends citation into your text editor (without having to go back and forward between text editor and Bookends). I tend to use Multimarkdown or Pandoc so the workflow also gives you the option of pasting a Multimarkdown or Pandoc citation.

A screenshot of the workflow is below
Image
taja
Posts: 55
Joined: Sun Feb 12, 2012 10:39 pm

Re: Cite-as-you-write

Post by taja »

This looks fantastic. Does anyone know whether it could be made into a Launchbar action?
kseggleton
Posts: 43
Joined: Fri Jan 01, 2016 6:47 am

Re: Cite-as-you-write

Post by kseggleton »

Does anyone know whether it could be made into a Launchbar action?
The workflow that I created has a different structure to a Launchbar action. The basic code languages used are Applescript and Bash, which Launchbar could also use. Someone would have to re-write the code for Launchbar (which I don't use sorry).
iandol
Posts: 465
Joined: Fri Jan 25, 2008 2:31 pm

Re: Cite-as-you-write

Post by iandol »

This sounds great! Do you have the source code for this somewhere for those of us that use Quicksilver/Launchbar etc. (this would almost make me switch from QS!!!)
jhbarker
Posts: 28
Joined: Tue Aug 09, 2005 12:07 pm

Re: Cite-as-you-write

Post by jhbarker »

Hi, I use Launchbar and would also be very grateful if you could post the script so I could modify it for Launchbar.
It looks great!
jason
kseggleton
Posts: 43
Joined: Fri Jan 01, 2016 6:47 am

Re: Cite-as-you-write

Post by kseggleton »

I don't know how this would translate to Launchbar but I have attached the underlying code and workflow below.

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 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 bash script, 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 bash script, 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 bash 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
Last edited by kseggleton on Fri May 20, 2016 7:18 am, edited 1 time in total.
Jon
Site Admin
Posts: 10060
Joined: Tue Jul 13, 2004 6:27 pm
Location: Bethesda, MD
Contact:

Re: Cite-as-you-write

Post by Jon »

This looks great, thank you for posting your work. Would you mind also posting this in the AppleScripts forum? It gets a lot less traffic and will remain on the main page much longer, plus it's probably the place I would look if I were looking for such scripting assistance.

Jon
Sonny Software
jhbarker
Posts: 28
Joined: Tue Aug 09, 2005 12:07 pm

Re: Cite-as-you-write

Post by jhbarker »

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

Re: Cite-as-you-write

Post by NilsMS »

This workflow looks really fantastic, many thanks for posting this!
Generally, it works as advertised. Unfortunately, it takes very long to load all the references of my research library (+13500), which at first made me think it does not work at all. Very reluctantly, I have to admit that in the present form it is not usable for me.
Anyway, thanks for the effort!

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

Re: Cite-as-you-write

Post by NilsMS »

NilsMS wrote:Generally, it works as advertised. Unfortunately, it takes very long to load all the references of my research library (+13500), which at first made me think it does not work at all. Very reluctantly, I have to admit that in the present form it is not usable for me.
Nils
I found a very simple solution for my problem: In the ".cite"-script the setting of "Run Behavior" has to be set to "Automatic delay after last character typed", then the references found pop up shortly after stopping typing.
There is one severe problem, though, with diacritic letters (has has already been noted on the Alfred site). Authors with such letters are not found; however, if they co-authored a paper, they funnily appear in the list correctly spelled. I will start a related query over on the Alfred site.

Cheers,

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

Re: Cite-as-you-write

Post by kseggleton »

Thanks Nils for this. I have updated the script on the Alfred forum to allow searching for diacritic characters and changed the script behaviour to an automatic delay after last character typed.
Dellu
Posts: 268
Joined: Sun Mar 27, 2016 5:30 am

Re: Cite-as-you-write

Post by Dellu »

Thanks; this is great--I was trying to do the same before.
Post Reply