The overview of the workflow is:

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 {"&", """, "'", "<", ">"}
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
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
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'
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'
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
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
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"