Scripting support in Papers

matthias's Avatar

matthias

12 Jul, 2014 01:50 PM

Hi,

this is meant as a public space to gather feedback, questions and sample scripts for the AppleScript support in Papers 3.0.35 and above.

Matthias (from the Papers Mac team)

Showing page 7 out of 7. View the first page

  1. 181 Posted by Christian Werne... on 23 Mar, 2016 02:57 PM

    Christian Werner's Avatar

    Thank for getting back to me!

    Will check out yours ASAP. This looks much cleaner and more sophisticated :-)
    A good chance to also learn a bit of proper AppleScript!

    Thanks!
    Christian

  2. 182 Posted by Jason on 25 Mar, 2016 02:20 PM

    Jason 's Avatar

    Greetings Matthias at al.,

    I've spent the week playing with some code you recently sent me. After much frustration, I finally came up with the following, which is not great, but it gets 99% of the job done. As can be seen, there is a section that I'm not clear on what's happening. Any suggestions for how to understand that section and/or improve the code?

    As always, thanks for any help, which has always been stellar!

    Cheers,
    Jason

    
    (*
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %
    %
    %       CREATED BY:     JASON
    %       MODIFIED BY:    Matthias
    %
    %       CREATED ON:     2015-11-19
    %       MODIFIED ON:        2016_03_17
    %       MODIFIED ON:        2016_03_23
    %       MODIFIED ON:        2016_03_24
    %       MODIFIED ON:        2016_03_25
    %
    %
    %       USAGE:          EXPORT CRAGGS ABSTRACTS FROM PAPERS-3 TO A tex FILE FOR USE WITH BibTex
    %
    %
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    *)
    
    property authorName : "Craggs"
    
    set documentsFolderPath to path to documents folder as string
    set theDate to current date
    set theScript to "Papers_ExportCraggsAbstracts_forCV.scpt"
    set scriptLoc to "/Users/XXX/Library/Scripts/Applications/Papers/"
    
    tell application "Papers"
        -- …
        set allCiteKeys to citekey of every publication item whose author names contains "Craggs" and resource type is "Abstract"
        set the clipboard to my mergeTextItems(allCiteKeys, ",") & linefeed
        
        -- …
        -- Merges the given list of text items using the given separator string.
    end tell
    
    --      I AM UNCLEAR ABOUT WHAT THE SECTION BELOW ACTUALLY DOES
    
    on mergeTextItems(textItemList, aSeparator)
        considering case
            set prevTIDs to text item delimiters of AppleScript
            set text item delimiters of AppleScript to aSeparator
            set mergedText to textItemList as text
            set text item delimiters of AppleScript to prevTIDs
        end considering
        return mergedText
    end mergeTextItems
    
    
    --          COMMANDS TO CREATE, WRITE TEXT, AND PAST THE CLIPBOARD TO A TEXT FILE FOR LATEX/BIBDESK
    
    do shell script "echo %" & " " & " > /Users/XXX/Documents/MyCV/LaTeX/CV_Abstracts2.tex"
    do shell script "echo %" & " " & " >> /Users/XXX/Documents/MyCV/LaTeX/CV_Abstracts2.tex"
    do shell script "echo %" & " " & " >> /Users/XXX/Documents/MyCV/LaTeX/CV_Abstracts2.tex"
    do shell script "echo %        This file was created by: Jason Craggs >> /Users/XXX/Documents/MyCV/LaTeX/CV_Abstracts2.tex"
    do shell script "echo %        This file was created with the script: " & theScript & ">> /Users/XXX/Documents/MyCV/LaTeX/CV_Abstracts2.tex"
    do shell script "echo %        The script is located is file " & scriptLoc & ">> /Users/XXX/Documents/MyCV/LaTeX/CV_Abstracts2.tex"
    do shell script "echo %    The script was run " & theDate & ">> /Users/XXX/Documents/MyCV/LaTeX/CV_Abstracts2.tex"
    do shell script "echo %" & " " & " >> /Users/XXX/Documents/MyCV/LaTeX/CV_Abstracts2.tex"
    do shell script "echo %" & " " & " >> /Users/XXX/Documents/MyCV/LaTeX/CV_Abstracts2.tex"
    do shell script "echo %" & " " & " >> /Users/XXX/Documents/MyCV/LaTeX/CV_Abstracts2.tex"
    do shell script "echo " & quoted form of "\\\\" & "cite{" & ">> /Users/XXX/Documents/MyCV/LaTeX/CV_Abstracts2.tex"
    do shell script "pbpaste >> /Users/XXX/Documents/MyCV/LaTeX/CV_Abstracts2.tex"
    do shell script "echo " & "} " & " >> /Users/XXX/Documents/MyCV/LaTeX/CV_Abstracts2.tex"
    do shell script "echo %" & " " & " >> /Users/XXX/Documents/MyCV/LaTeX/CV_Abstracts2.tex"
    
  3. 183 Posted by Jason on 25 Mar, 2016 08:03 PM

    Jason 's Avatar

    For those of you who use the RSS feature in DEVONthink Pro Office to track PubMed queries, here is a script that will export the reference from DTP to Papers and then match the reference. As always, If anyone can improve on this little script, please do so and share.
    Cheers,
    Jason

    
    tell application id "DNtp"
        set theSelection to the selection
        set theList to "" --{}
        --set theCnt to 0 --added this section
        repeat with theRecord in theSelection
            set theText to plain text of theRecord
            if theText contains "PMID: " then
                set AppleScript's text item delimiters to "PMID: "
                set thePMID to (text item 2 of theText) as string
                if thePMID contains " [" then
                    set AppleScript's text item delimiters to " ["
                    set thePMID to (text item 1 of thePMID) as string
                    tell application "Papers"
                        open location ("http://www.ncbi.nlm.nih.gov/pubmed/?term=") & thePMID
                        set m to selected mode of front library window
                        set selected mode of front library window to Reader Mode
                        set p to displayed publications of front library window
                        set aPub to first item of (selected publications of front library window as list)
                        set pmid of aPub to thePMID
                        delay 2.5
                        match (selected publications of front library window as list) without replacing metadata
                    end tell
                    set theList to theList & thePMID & return
                    --set theCnt to theCnt + 1 -- added this section
                end if
            end if
        end repeat
    
  4. 184 Posted by montywaite on 05 Apr, 2016 07:53 PM

    montywaite's Avatar

    Dear All
    For some reason, I have now got duplicates of a large number of papers in my library
    More than is possible to merge with the current support in the software
    Is there a way I can use Applescript to do this?
    Thanks!
    Monty

  5. 185 Posted by matthias on 06 Apr, 2016 10:25 AM

    matthias's Avatar

    Hi Jason,

    sorry for the late response (only saw your post now). It's great that you've come up with a script that works for you!

    Any suggestions for how to understand that section and/or improve the code?

    The mergeTextItems() function merges all elements in a given list into a string (using the given delimiter). For example, the allCiteKeys variable in your script contains a list of citekeys, like this one:

    {"Down:2011ef", "Beall:2010ia", "Piipari:2010bu", "Piipari:2010kh", "Claret:2011fs"}
    

    By passing this list thru mergeTextItems(), it can be converted into a single string that's suitable for inclusion in a text file. E.g., your script would convert the above list into this string (i.e., concatenate all list elements using a comma as a delimiter):

    Down:2011ef,Beall:2010ia,Piipari:2010bu,Piipari:2010kh,Claret:2011fs
    

    W.r.t. the shell script commands: You could also use plain AppleScript for this (such as the functions below), but it's most important that you've found a working solution!

    --- Saves the given text to the given file. Note that this will replace any existing file content.
    on writeTextToFile(aFile, theText)
        set aFileRef to open for access aFile with write permission
        set eof aFileRef to 0
        write theText to aFileRef as «class utf8»
        close access aFileRef
    end writeTextToFile
    
    --- Appends the given text to the given file. If the file does not exist, a new file is created.
    on appendTextToFile(aFile, theText)
        set aFileRef to open for access aFile with write permission
        write theText to aFileRef as «class utf8» starting at eof
        close access aFileRef
    end appendTextToFile
    

    Best,
    Matthias
    (who doesn't work for Papers anymore but will try his best to respond to posts in this thread)

  6. 186 Posted by matthias on 06 Apr, 2016 10:33 AM

    matthias's Avatar

    Hi Monty,

    For some reason, I have now got duplicates of a large number of papers in my library More than is possible to merge with the current support in the software Is there a way I can use Applescript to do this?

    The Papers scripting API has no explicit commands to find or merge duplicates.

    That said, you could probably use a script to detect duplicates if the duplicate records have some field content in common. E.g., you could extract/select/delete all publications with an identical title (or DOI, ISBN, etc). More advanced duplicate checking (e.g., fuzzy matching) might be tricky, though.

    Merging of publications currently isn't possible via the scripting support, unfortunately.

    HTH, Matthias

  7. 187 Posted by alex.velasco on 07 Apr, 2016 07:35 AM

    alex.velasco's Avatar

    Hello,
    I have a list (excel or text file) of hundreds of publication titles (books / articles) that I would like to search for in my Papers 3 library. The found titles can simply be flagged. Not found titles should be recorded. Is this possible with AppleScript? Is it easy to do?
    Regards, Alex

  8. 188 Posted by Stephan Zellerh... on 29 May, 2016 06:29 AM

    Stephan Zellerhoff's Avatar

    Hi!

    Maybe this is of interest for those who use papers and devonthink: Screenshots done in papers using the build in mac osx tool (shift+command+4) are ocred and imported to devonthink. A paperslink points back to the pdf. By capturing also the figure´s legend, the see also feature of devonthink can be used to find related content. Screenshot done in other apps are simply saved, not imported to devonthink. The script is a folder action script for the screenshot folder. I used "defaults write com.apple.screencapture type pdf“, but this may not be necessary…

    Stephan

    on adding folder items to this_folder after receiving added_items
    try
    if (count of added_items) is greater than 0 then
    tell application id "DNtp" to launch


    repeat with theItem in added_items
    set lastFileSize to 0
    set currentFileSize to 1
    repeat while lastFileSize ≠ currentFileSize
    delay 0.5
    set lastFileSize to currentFileSize
    set currentFileSize to size of (info for theItem)
    end repeat

    try
    set thePath to theItem as text
    if thePath does not end with ".download:" then
    tell application "System Events"
    set activeApp to (name of first application process whose frontmost is true)
    end tell

    if activeApp contains "Papers" then
    tell application id "com.mekentosj.papers3"
    set selectedPubs to selected publications of front library window
    set screenshotLink to item url of first item of selectedPubs
    end tell
    tell application id "DNtp"
    set theGroup to get record with uuid "3A553272-6F9C-41B1-8159-E7AFA3DFC283"
    set theRecord to ocr file thePath to theGroup
    set tags of theRecord to "screenshot"
    set URL of theRecord to screenshotLink
    if exists theRecord then tell application "Finder" to delete theItem
    end tell
    end if
    end if
    end try
    end repeat
    end if
    end try
    end adding folder items to

  9. Ayie closed this discussion on 01 Jul, 2016 05:40 AM.

  10. Jason re-opened this discussion on 11 Nov, 2016 12:15 AM

  11. 189 Posted by Jason on 11 Nov, 2016 12:15 AM

    Jason 's Avatar

    Greetings,

    I am trying to write an AppleScript that will for "matching" articles with PubMed information. How can I perform a match using the PubMed search engine via scripting?

    Cheers,
    Jason

  12. 190 Posted by matthias on 06 Sep, 2017 07:49 PM

    matthias's Avatar

    Hi Jason,

    How can I perform a match using the PubMed search engine via scripting?

    I know it's ridiculous to reply to your post almost a year later, and you've likely figured this out yourself. But I just wanted to confirm that, using the scripting API, it's unfortunately not possible to use any other search engine for matching than CrossRef.

    Matthias

  13. 191 Posted by matthias on 06 Sep, 2017 07:55 PM

    matthias's Avatar

    Hi folks,

    I've written a "Papers to Bookends" script which exports all publications selected in your Papers 3 library to Bookends:

    https://github.com/extracts/mac-scripting/tree/master/Papers3/Paper...

    The script will also transfer the publication's primary PDF (if there's any) and can also transfer the publication's rating, color label, flagged status, language, edition, citekey, and "papers://..." link.

    The PDF file's annotations can be also transferred if you follow the steps given in the "Known Issues" section of the README.

    Hope this is useful to somebody.
    Cheers, Matthias

  14. 192 Posted by Jason on 07 Sep, 2017 06:52 PM

    Jason 's Avatar

    Hi Matthias,

    No worries! To be honest, after trying and failing for a while, I set the question aside to focus on other things. However, I am glad you responded, in general and happy to have a definite answer.

    Cheers,
    Jason

Discussions are closed to public comments.
If you need help with Papersapp please start a new discussion.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac