Forked from fractaledmind/Insert Markdown Link of BibDesk Item into TextMate
Last active
October 17, 2021 19:56
-
-
Save derickfay/6386807 to your computer and use it in GitHub Desktop.
Copies to clipboard to remove dependency on a particular editor. Displays Cite Key as well as Title in selection dialog.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- original by Stephen Margheim | |
-- edited by Derick Fay, 29 Aug 2013 | |
-- copies to clipboard | |
-- uses Cite Key rather than title as link text | |
-- Displays Cite Key as well as Title in selection dialog | |
-- (I use Author-Date cite keys, & tend to think of publications as Author Date references rather than titles…) | |
display dialog "Get Link of selected item or choose other item?" buttons {"Get Selected", "Choose Other"} default button {"Get Selected"} | |
--Get Markdown formatted info for selected item | |
if result = {button returned:"Get Selected"} then | |
tell application "BibDesk" | |
set bibdesk_selection to selection of first document | |
if bibdesk_selection is "" then display dialog "Please select an item." | |
set Markdown_link to "" | |
repeat with i from 1 to the count of bibdesk_selection | |
set CiteKey to cite key of item i of bibdesk_selection | |
set ItemURL to "x-bdsk://" & CiteKey | |
-- set itemName to title of item i of bibdesk_selection | |
set theWikiLink to "[" & CiteKey & "]" & "(" & ItemURL & ")" | |
set Markdown_link to (Markdown_link) & theWikiLink | |
end repeat | |
end tell | |
--insert whichever link type you selected into WriteRoom | |
set the clipboard to Markdown_link | |
else if result = {button returned:"Choose Other"} then | |
tell application "BibDesk" | |
set allDocs to every document of application "BibDesk" | |
set listofDocs to {} | |
repeat with theDoc in allDocs | |
set thename to the name of theDoc | |
copy thename to the end of listofDocs | |
end repeat | |
if (not ((count of listofDocs) is 1)) then | |
set SelDoc to choose from list of listofDocs with title "Select BibDesk Database" with prompt ¬ | |
"Current BibDesk Databases" OK button name "OK" | |
set theDoc to item 1 of SelDoc | |
else | |
set theDoc to document 1 of application "BibDesk" | |
end if | |
set allGroups to every static group of theDoc | |
set listofGroups to {} | |
repeat with theGroup in allGroups | |
set thename to the name of theGroup | |
copy thename to the end of listofGroups | |
end repeat | |
set SelGroup to choose from list of listofGroups with title "Select BibDesk Group" with prompt ¬ | |
"Current BibDesk Groups" OK button name "OK" | |
set theGroup to item 1 of SelGroup | |
set allPubs to every publication of group theGroup of theDoc | |
set listofPubs to {} | |
set listofLinks to {} | |
set listofCiteKeys to {} | |
set DisplayList to {} | |
repeat with thePub in allPubs | |
set PubName to title of thePub | |
copy PubName to the end of listofPubs | |
set CiteKey to cite key of thePub | |
set PubURl to "x-bdsk://" & CiteKey | |
copy PubURl to the end of listofLinks | |
copy CiteKey to the end of listofCiteKeys | |
copy (CiteKey & " " & PubName) to the end of DisplayList | |
end repeat | |
set SelPub to choose from list of DisplayList with title "Select BibDesk Publication" with prompt ¬ | |
"Current BibDesk Publications" OK button name "OK" | |
set thePub to SelPub as string | |
set PubPlacement to my findAll(DisplayList, thePub) as number | |
set PubURl to item PubPlacement of listofLinks | |
set PubCiteKey to item PubPlacement of listofCiteKeys | |
set theWikiLink to "[" & PubCiteKey & "]" & "(" & PubURl & ")" | |
end tell | |
--insert whichever link type you selected into WriteRoom | |
set the clipboard to theWikiLink | |
end if | |
(* SUBROUTINES *) | |
on findAll(lst, val) | |
-- HAS (http://applemods.sourceforge.net/mods/Data/List.php) | |
local lst, val, res | |
try | |
if lst's class is not list then error "not a list." number -1704 | |
if {val} is not in lst then return {} | |
set res to {} | |
script k | |
property l : lst | |
end script | |
repeat with i from 1 to count of k's l | |
if k's l's item i is val then set res's end to i | |
end repeat | |
return res | |
on error eMsg number eNum | |
error "Can't find All: " & eMsg number eNum | |
end try | |
end findAll |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment