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 5 out of 7. View the first page

  1. 121 Posted by Jason on 04 Mar, 2015 05:23 PM

    Jason 's Avatar
    Below is a little script that takes the first article selected in the Vienna RSS feed reader and imports it into your Papers 3 library.

    Greetings,
    I'm trying to do the same/similar thing with an RSS feed reader in DevonThink Pro. Working with them, I have a working script that will extract the PMID of each of the selected articles from the feed. However, I'm struggling with how to use that information in a way that will allow me to import the references into Papers. I tried tweaking script referenced above without success. Any help or suggestions that can provide would be greatly appreciated. As an aside, the script for extracting PMID information is below.

    -- ############################################
    --
    --      ORIGINALLY CREATED BY:  Created by Christian Grunenberg 
    --      ORIGIANNLY CREATED ON:  FEB 26, 2015
    --                                              
    --              COMMENTS BY:    JASON CRAGGS    
    --      EDITED ON:      2015_02_26
    --      MODIFIED ON:    2015_03_03
    --      MODIFIED ON:    2015_03_04      
    --                                      
    --      COMMENTS:       USED TO EXTRACT PMID INFORMATION FROM RSS FEEDS IN DTpro            
    --      COMMENTS:       TRYING TO IMPORT RESULTS INTO PAPERS3                               
    --                                                          
    -- ############################################
    -- 
    -- 
    -- 
    (*
                        THIS SECTION WORKS
    *)
    
    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
                    set theList to theList & thePMID
                    set theCnt to theCnt + 1 -- added this section
                end if
            end if
        end repeat
        return theList
        
    end tell
    
    
    (* 
    BELOW ARE FAILED ATTEMPTS TO EXPORT THE RESULTS FOR USE WITH PAPERS 3
    *)
    
    
    (*
    
        tell application "System Events"
            keystroke "c" using {command down}
            delay 0.5
        end tell
    *)
    
    
    (*
        set this_app to (path to frontmost application as string)
        
        try
            if this_app does not contain "DEVONthink Pro" then
                tell application this_app to activate
                tell application "System Events"
                    keystroke "c" using {command down}
                    delay 0.5
                    tell application id "DNtp" to paste clipboard
                end tell
            end if
        on error error_message number error_number
            if error_number is not -128 then display alert "DEVONthink Pro" message error_message as warning
        end try
        *)
    
    
    
    
    
    (*
        
        
        
        
        set clipboard to theList
        tell application "TextEdit" to activate --make sure TextEdit is running
        tell application "System Events"
            keystroke "c" using {command down}
            delay 0.5
            tell application id DNtp to paste clipboard
        end tell
            
        --  make new document
        --  make new paragraph at end of document 1 with theList
            --  make new paragraph at end of document 1 with data (return & (the clipboard))
        end tell
        end tell
        
        *)
    
    --return theCnt
    -- item 1 of {theList}
    --return theList & theCnt
    -- set the clipboard to {theList}
    
    (*
        -- added the whole next if-then statement
        if (theCnt > 0) then
            create record with {name:(theCnt as string) & " comments", type:txt, plain text:theList} in current group
            return name
            return theList
        end if
        
        *)
    
    
    
    
    (*
    
    -- set the clipboard to "Add this sentence at the end."
    set the clipboard to {theList}
    tell application "TextEdit"
        activate --make sure TextEdit is running
        make new document
        -- make new paragraph at end of document 1 with data (return & (theList))
        --  make new paragraph at end of document 1 with data (return & (the clipboard))
    end tell
    
    
    *)
    
    (*
    tell application "TextEdit"
        activate --make sure TextEdit is running
        make new document
        item 1 of {theList}
    end tell
    *)
    
    (*
    tell application "TextWrangler"
        activate
        make new text document
        paste theList
        
    end tell
    *)
    
  2. 122 Posted by matthias on 05 Mar, 2015 09:07 AM

    matthias's Avatar

    Hi Jason,

    I'm trying to do the same/similar thing with an RSS feed reader in DevonThink Pro.

    Please try the script below. At the moment it's probably best to import an RSS feed entry via its webpage URL into your Papers 3 library, thus the script takes this approach instead. Note that the automatic download of metadata and (if available) the fulltext PDF may work best if you import a single paper at a time.

    Hope this helps,
    Matthias

    -- This script takes all selected records (e.g., from an RSS feed) in DEVONthink
    -- and imports them (via their webpage URL) into your Papers 3 library
    
    tell application id "DNtp"
        set theSelection to the selection
        set urlList to {}
        repeat with theRecord in theSelection
            set recordURL to URL of theRecord
            if recordURL starts with "http://" or recordURL starts with "https://" then
                copy recordURL to end of urlList
            end if
        end repeat
    end tell
    
    tell application id "com.mekentosj.papers3"
        activate -- if you don't want Papers 3 to become the frontmost application, comment out this line (i.e., add two hyphens in the front)
        repeat with anURL in urlList
            open location anURL -- opens the publication in a new Reader tab
        end repeat
        display dialog "Did import " & (count of urlList) & " publication(s)." buttons {"OK"} default button "OK" with icon 1
    end tell
    
  3. 123 Posted by Jason on 10 Mar, 2015 02:22 PM

    Jason 's Avatar

    Thank you Matthias,

    I will test this out later today (March 10, 2015) and report back to you and the community how it works. As always, your help and guidance is greatly appreciated as is the input from the community.

    Cheers,
    Jason

  4. 124 Posted by Stephan on 31 Mar, 2015 08:46 PM

    Stephan's Avatar

    Dear Matthias!

    Is it possible to create a popclip extension for papers 3 to automatically create a reference from the clipboard via popclip?

    Thanks!

    Stephan

  5. 125 Posted by matthias on 01 Apr, 2015 04:42 PM

    matthias's Avatar

    Hi Stephan,

    Is it possible to create a popclip extension for papers 3 to automatically create a reference from the clipboard via popclip?

    You mean if it is possible to create a popclip extension that creates a new entry in your Papers library based on the selection?

    This should be possible, at least if the selection contains a web URL or DOI. The script inside the popclip extension would then call the open location command from the Papers scripting dictionary. Future updates to the Papers scripting API should also allow to create a new paper from a selected PMCID or PMID.

    To create a new entry in your Papers library from a selected formatted reference, the necessary scripting command is unfortunately missing, so this wouldn't yet be possible.

    Best,
    Matthias

  6. 126 Posted by Stephan.Zellerh... on 01 Apr, 2015 07:03 PM

    Stephan.Zellerhoff's Avatar

    Hi Matthias!

    What I have in mind is unfortunately the latter… Nevertheless, thank you very much for your help!

    Stephan

    Am 01.04.2015 um 18:42 schrieb matthias <[email blocked]<mailto:[email blocked]>>:

  7. 127 Posted by matthias on 01 Apr, 2015 09:07 PM

    matthias's Avatar

    Hi Stephan,

    What I have in mind is unfortunately the latter…

    Ok, I see. What could be done even with the current release version is to simply create a new paper with the selected text and then use the matching functionality. Depending on your input text this may not always work, though. But if you've selected (parts of) a reference that's indexed by CrossRef (i.e., it has a DOI) then chances are high that the matching will work.

    This approach is described in the "Getting started guide" (at the end of the "Matching publications" section):

    http://support.mekentosj.com/kb/papers-3-for-mac-how-to/scripting-p...

    Here's an example script (imagine that the text stored in variable theReference comes from the {popclip text}):

    set theReference to "Biomechanical evaluation of combined short segment fixation and augmentation of incomplete osteoporotic burst fractures"
    
    tell application "Papers"
        set newPub to make new publication item with properties {title:theReference}
        delay 0.5
        set selected publications of front library window to (newPub as list)
        set matchedPubs to match (newPub as list) with replacing metadata
    end tell
    

    Best regards,
    Matthias

  8. 128 Posted by matthias on 07 Apr, 2015 11:01 AM

    matthias's Avatar

    Hi Stephan,

    I've made two PopClip extensions for Papers 3. To install them, install PopClip, then download the two attachments below and double click them.

    The first one (called "Papers (match)") takes the selected text and creates a new publication entry in your Papers 3 library from it, then uses CrossRef to match the entry. If it is indexed by CrossRef, Papers will fetch all the basic metadata and (if available) will download the PDF.

    This usually works well if you just select a paper's title, but for short titles that aren't distinct enough, selecting a fully formatted reference may work better. Usually, though, it's sufficient to select just a few distinct words from the paper's reference. E.g., try to select these four words and use the "Papers (match)" PopClip extension:

    Beall Loss of AMP

    The second Papers PopClip extension (called "Papers (URLs, IDs)") extracts any URLs or common article identifiers from the selected text and imports all of them. These identifiers are supported: DOI, PMID, PMCID and arXiv ID. To avoid false positives, PMID identifiers must be either prefixed by "pmid:" or "pubmed/", or provided in a text that consists solely of PMIDs separated by any non-alphanumerical characters. As an example, try to select the below text block and choose the "Papers (URLs, IDs)" PopClip extension:

    http://www.biomedcentral.com/1471-2482/15/18
    doi:10.1186/s13046-015-0129-6
    pmid:23031599
    PMC4333888
    arXiv:1503.01696
    

    Let me know if this doesn't work for you, or if you have any suggestions for improvement.

    HTH, Matthias

  9. 129 Posted by Stephan Zellerh... on 07 Apr, 2015 11:31 AM

    Stephan Zellerhoff's Avatar

    Hi Matthias!

    I tested both extensions quickly, and both seem to work perfectly. Thanks so much! Is it possible to leave papers in the background, e.g. not changing the active window?

    Thanks!

    Stephan

  10. 130 Posted by matthias on 07 Apr, 2015 11:44 AM

    matthias's Avatar

    Hi Stephan,

    both seem to work perfectly.

    That's good news, thanks for testing!

    Is it possible to leave papers in the background, e.g. not changing the active window?

    Yes, that's an easy change:

    1. Change the file name extension from ".popclipextz" to ".zip", then double click to unzip it.
    2. Select the resulting ".popclipext" extension (which is really a package), right click and select "Show Package Contents"
    3. Open the contained "papers.applescript" file in a text editor and remove or comment out the line(s) that say "activate". In case of the "Papers (URLs, IDs)" extension, you may also want to comment out the line that starts with "display dialog …". You can comment out a line in an AppleScript by prefixing the line with two hyphens ("--").
    4. Save the "papers.applescript" file again.
    5. Double click the ".popclipext" extension again to re-add it to PopClip.

    Let me know if you get stuck.
    Matthias

  11. 131 Posted by Rik on 13 Apr, 2015 02:59 PM

    Rik's Avatar

    Would you be able to put those extensions up on github or something so we could maybe fiddle with them?

  12. 132 Posted by MartinN on 24 May, 2015 09:28 PM

    MartinN's Avatar

    Hi, how can I add a primary file item of an application item or how can I replace it?

    Thanks a lot for the Apple Scripting support

  13. 133 Posted by weixie4 on 29 Jun, 2015 01:51 PM

    weixie4's Avatar

    Hi Matthias,

    How can I refer to all "publication item" of a certain "collection" of Papers in AppleScript?

    I ask this because I want to do some operations on all items in the "Reading List" collection. Specifically, I want to write an AppleScript to
    1) export the "primary file item" of all the "publication item" in the "Reading List" collection to an external location on my hard drive--PDF Exert app's folder located within my local iCoud Drive directory). These files will then be synced via iCloud to my iPad and I can read them there with PDF Expert--my preferred pdf annotation app on iPad.
    2) pull the annotated PDF file back and replace the old one in Papers library when I finished reading it. This action is triggered when the "publication item" is deleted from "Reading List".
    3) and finally, calls a third party Javascript (forked from Zotefile) to extract the annotations in the PDF file and put it in "General Note" field of the "publication item" in Papers library. It is unfortunate that currently "General Note" field in Papers seems to only support plain text, so putting links in the note that trace the annotations back to the specific pages on which they are made is not possible, but I am fine with this.

    You may think all the above are already supported by Papers 3's iOS app, but I like to read with PDF expert because it offers better hand-written annotation, faster browsing and more functionalities; also annotations made by PDF Expert are visible to other PDF readers like Preview and Adobe Acrobat/Reader, while it is not the case for those made by Papers iOS app.

    I think as long as that I can refer and manipulate items the Reading List with AppleScript, I can easily realize my goals 1)-3).

    Thank you and kind regards,
    Wei Xie

  14. 134 Posted by Chris on 30 Jun, 2015 03:04 AM

    Chris's Avatar

    Something discussed earlier in this thread was the possibility of papers including a comment field attached to specific highlights. This, combined with the ability to edit notes created by highlights would be very helpful. I know this does not directly relate to applescript, but it was something mentioned in this thread. Anyway, I would love to hear whether the papers team is working on this kind of functionality.

  15. 135 Posted by Stephan Zellerh... on 12 Jul, 2015 01:18 PM

    Stephan Zellerhoff's Avatar

    Hi!

    With the current version of papers it now works. Thanks!

    Stephan

  16. 136 Posted by Mendel on 17 Aug, 2015 05:21 PM

    Mendel's Avatar

    Although not exactly applescript, I thought these might be helpful. Here are two Keyboard Maestro macros that I created for Papers and have found extremely useful. The problem I had was that if I highlighted anything in Papers and then wanted to copy it to a document I was working on, I always got the page number (usually incorrect) and the highlighted date included. Also I wanted an easy way to immediately paste in the citekey without having to invoke the citations search.

    Accordingly, these KM macros copy highlighted text in Papers, search through the clipboard and replace the page numbers and highlighted dates with simple quotation marks, then simulate the keystroke for copying the citekey, and finally copy it all to the clipboard. Changing to my text editor of choice, I then simply press command V and paste in a perfectly formatted quote.

    A few notes:

    • As an example this quote: p.176: This is an example. -- Highlighted Aug 17, 2015 becomes: "This is an example"[@yourcitekey]
      • I prefer pandoc citekeys as I write everything in plain text so I added another feature into the macros that replaces the papers citekey: {citekey} with the pandoc one: [@citekey]. If you'd rather the papers version though, just delete those few steps in the macros.
    • There are two macros, one is used for shorter quotes and thus adds quotation marks on either end (as in the example above). The other though, is for when I want a block quote. For this one, instead of adding quotation marks, the quote is preceded by > (as per markdown syntax) and the quotation marks are left out.
  17. 137 Posted by Stephan Zellerh... on 20 Aug, 2015 08:13 PM

    Stephan Zellerhoff's Avatar

    Hi!

    Thanks for the macro! I modified it a bit: The papers citekey is formatted as a markdown link using the papers link, so I can go back to the original paper easily. Moreover, the complete clipboard content is automatically appended to a file in my nvalt folder for editing.

    Best,

    Stephan

  18. 138 Posted by Blagoy on 14 Sep, 2015 08:49 AM

    Blagoy's Avatar

    Hi!

    Is there a script that can automatically generate notes in Evernote for highlights in Papers? Or at least an easy way to export highlights from a text, so that each highlight has its own note?

    Thanks!
    Blagoy

  19. 139 Posted by matthias on 14 Sep, 2015 10:09 AM

    matthias's Avatar

    Hi Blagoy,

    s there a script that can automatically generate notes in Evernote for highlights in Papers?

    below is a script that exports all note & highlight annotations of all selected publications to Evernote. Note that running the script for the same publications several times will currently cause duplicate note entries in Evernote. And long notes may cause Evernote to fail creating a note title. That said, I hope this can serve as a starting point for you.

    Or at least an easy way to export highlights from a text, so that each highlight has its own note?

    See the link below for a script that exports note & highlight annotations to individual text files:

    http://support.mekentosj.com/discussions/questions/105370-scripting...

    Hope this helps,
    Matthias

    -- Papers annotations to Evernote, v0.1
    -- by Matthias Steffens, papersapp.com
    
    -- Exports all notes & highlight annotations of all selected publications to Evernote.
    -- For each publication, the formatted reference and the cite key are included above the
    -- note/annotation text. The publication's Papers link and keywords are also transferred.
    
    -- TODO: Instead of duplicating notes that already exist in Evernote, update them appropriately.
    -- TODO: Limit title to x number of words.
    
    tell application id "com.mekentosj.papers3"
        set selectedPubs to selected publications of front library window
        set allPubsWithPDF to {}
        repeat with aPub in selectedPubs
            set pdfFile to primary file item of aPub
            if pdfFile is not missing value then
                -- get all notes & highlight annotations for this publication
                set pubNotes to every annotation item of primary file item of aPub
                
                if pubNotes is not {} then
                    copy aPub to end of allPubsWithPDF
                    
                    -- gather info for this publication
                    set {pubRef, pubKey, pubTitle, pubLink} to {formatted reference, citekey, title, item url} of aPub
                    set pubKeywords to name of every keyword item of aPub
                    
                    repeat with aPubNote in pubNotes
                        -- individual records have titles like "<CITEKEY> - <NOTE SUMMARY>"
                        set noteSummary to content summary of aPubNote
                        set noteTitle to pubKey & " - " & noteSummary
                        
                        -- assemble formatted text for this note
                        set noteContents to pubRef & linefeed & linefeed ¬
                            & "{" & pubKey & "}" & linefeed & linefeed ¬
                            & noteSummary & linefeed & linefeed
                        
                        -- create a record for this note in Evernote
                        my createEvernoteNote(noteTitle, pubLink, noteContents, pubKeywords)
                    end repeat
                end if
            end if
        end repeat
        if allPubsWithPDF is {} then
            display dialog "Couldn't export any notes to Evernote since the selection didn't contain any PDFs with notes." buttons {"OK"} default button "OK" with icon 1
        else
            display dialog "Exported notes of " & (count of allPubsWithPDF) & " publication(s) to Evernote." buttons {"OK"} default button "OK" with icon 1
        end if
        
    end tell
    
    -- Creates a new (plain text) note from the given text in the default Evernote notebook.
    on createEvernoteNote(noteTitle, noteURL, noteText, noteTags)
        if noteTitle is "" or noteTitle is missing value then return
        if noteText is "" or noteText is missing value then return
        
        set newNote to missing value
        tell application "Evernote"
            if noteTags is not {} or noteTags is not missing value then
                set newNote to create note with text noteText title noteTitle tags noteTags
            else
                set newNote to create note with text noteText title noteTitle
            end if
            
            if newNote is not missing value then
                set source URL of newNote to noteURL
            end if
        end tell
        return newNote
    end createEvernoteNote
    
  20. 140 Posted by matthias on 23 Sep, 2015 10:03 AM

    matthias's Avatar

    Hi,

    similar to the previously posted script, I've made another script that exports all selected publications to Evernote. For each publication, a new note is created in the default Evernote notebook, and the primary PDF is attached to the note. The publication's papers3:// link and keywords are also transferred. You can run the script again to update existing notes.

    As always, to run this script, open a new ScriptEditor.app document and paste the script into it. Then click the "Run" button in ScriptEditor. You could also save the script as a self-contained application or run it from the system's script menu (which can be enabled in the ScriptEditor prefs).

    Matthias

    -- Papers publications to Evernote, v0.1
    -- by Matthias Steffens, papersapp.com
    
    -- Exports all selected publications to Evernote. For each publication, a new
    -- note is created in the default Evernote notebook. The formatted reference
    -- and the cite key are included in the note, and the primary PDF is attached
    -- to the note. The publication's Papers link and keywords are also transferred.
    
    -- You can run this script more than once for the same publication, in which case the existing
    -- note will get updated. However, note that, for safety reasons, attachments that have been
    -- added to an Evernote note won't get removed again if the corresponding file is removed
    -- from its publication in your Papers library.
    
    -- TODO: Limit title to x number of words.
    -- TODO: when updating existing notes, generate proper HTML content
    
    tell application id "com.mekentosj.papers3"
        set selectedPubs to selected publications of front library window
        if selectedPubs is {} then
            my displayHelp()
            error number -128
        end if
    
        set exportedPubs to {}
        set updatedPubs to {}
        repeat with aPub in selectedPubs
            -- gather info for this publication
            set {pubRef, pubKey, pubTitle, pubLink} to {formatted reference, citekey, title, item url} of aPub
            set pubKeywords to name of every keyword item of aPub
            set noteTitle to pubKey & " - " & pubTitle
    
            -- assemble formatted text for this publication
            set noteContents to my noteContentsAsText(pubRef, pubKey, pubLink)
    
            set foundNotes to my noteWithSourceURL(pubLink)
            if foundNotes is {} then
                -- create a record for this note in Evernote
                set evernoteNote to my createEvernoteNote(noteTitle, pubLink, noteContents, pubKeywords)
                copy aPub to end of exportedPubs
            else
                -- update existing note in Evernote
                set evernoteNote to first item of foundNotes
                my setEvernoteNoteTitle(evernoteNote, noteTitle)
                my setEvernoteNoteURL(evernoteNote, pubLink)
                my setEvernoteNoteTags(evernoteNote, pubKeywords)
                my setEvernoteNoteText(evernoteNote, pubRef, pubKey, pubLink)
                copy aPub to end of updatedPubs
            end if
    
            -- add the publication's primary PDF as a note attachment
            set pubPrimaryFile to primary file item of aPub
            if pubPrimaryFile is not missing value then
                set pdfFilePath to full path of pubPrimaryFile
                if pdfFilePath is not missing value then
                    try
                        set pdfFile to (POSIX file pdfFilePath) as alias
                        my appendEvernoteNoteAttachment(evernoteNote, pdfFile)
                    end try
                end if
            end if
        end repeat
    
        if exportedPubs is {} and updatedPubs is {} then
            display dialog "Couldn't export any publications." buttons {"OK"} default button "OK" with icon 1
        else
            if exportedPubs is {} then
                display dialog "Updated " & (count of updatedPubs) & " existing Evernote note(s)." buttons {"OK"} default button "OK" with icon 1
            else if updatedPubs is {} then
                display dialog "Exported " & (count of exportedPubs) & " publication(s) to Evernote." buttons {"OK"} default button "OK" with icon 1
            else
                display dialog "Exported " & (count of exportedPubs) & " publication(s) to Evernote. Updated existing notes: " & (count of updatedPubs) buttons {"OK"} default button "OK" with icon 1
            end if
        end if
    end tell
    
    on displayHelp()
        tell application id "com.mekentosj.papers3"
            display dialog "Please select the publications that you'd like to export to Evernote and run this script again." with title "Nothing selected" buttons {"OK"} default button "OK" with icon 1
        end tell
    end displayHelp
    
    -- Creates a new (plain text) note from the given text in the default Evernote notebook.
    on createEvernoteNote(noteTitle, noteURL, noteText, noteTags)
        if noteTitle is "" or noteTitle is missing value then return missing value
        if noteText is "" or noteText is missing value then return missing value
    
        set newNote to missing value
        tell application "Evernote"
            if noteTags is not {} or noteTags is not missing value then
                set newNote to create note with text noteText title noteTitle tags noteTags
            else
                set newNote to create note with text noteText title noteTitle
            end if
    
            if newNote is not missing value then
                if noteURL is not missing value then set source URL of newNote to noteURL
            end if
        end tell
        return newNote
    end createEvernoteNote
    
    on setEvernoteNoteTitle(evernoteNote, noteTitle)
        if evernoteNote is missing value or noteTitle is missing value then return missing value
        tell application "Evernote"
            if title of evernoteNote is not equal to noteTitle then
                set title of evernoteNote to noteTitle
            end if
        end tell
        return evernoteNote
    end setEvernoteNoteTitle
    
    on setEvernoteNoteURL(evernoteNote, noteURL)
        if evernoteNote is missing value or noteURL is missing value then return missing value
        tell application "Evernote"
            if source URL of evernoteNote is not equal to noteURL then
                set source URL of evernoteNote to noteURL
            end if
        end tell
        return evernoteNote
    end setEvernoteNoteURL
    
    on setEvernoteNoteTags(evernoteNote, noteTagNames)
        if evernoteNote is missing value or noteTagNames is missing value then return missing value
        tell application "Evernote"
            set existingNoteTagNames to {}
            set existingNoteTagObjects to tags of evernoteNote
            repeat with existingNoteTagObject in existingNoteTagObjects
                set existingNoteTagName to name of existingNoteTagObject
                copy existingNoteTagName to end of existingNoteTagNames
                if existingNoteTagName is not in noteTagNames then
                    unassign existingNoteTagObject from evernoteNote
                end if
            end repeat
            repeat with aTagName in noteTagNames
                if aTagName is not in existingNoteTagNames then
                    set existingTagObjects to (every tag whose name is aTagName)
                    if existingTagObjects is {} then
                        set aTagObject to make new tag with properties {name:aTagName}
                    else
                        set aTagObject to first item of existingTagObjects
                    end if
                    assign aTagObject to evernoteNote
                end if
            end repeat
        end tell
        return evernoteNote
    end setEvernoteNoteTags
    
    on setEvernoteNoteText(evernoteNote, formattedReference, citeKey, aLink)
        if evernoteNote is missing value then return missing value
        tell application "Evernote"
            set HTML content of evernoteNote to my noteContentsAsHTML(formattedReference, citeKey, aLink)
        end tell
        return evernoteNote
    end setEvernoteNoteText
    
    on appendEvernoteNoteAttachment(evernoteNote, aFile)
        if evernoteNote is missing value or aFile is missing value then return missing value
        try
            set aFileName to name of (info for aFile)
        on error errorMessage number errorNumber
            return missing value
        end try
        tell application "Evernote"
            -- check if an attachment with that name already exists
            set noteAttachments to attachments of evernoteNote
            repeat with anAttachment in noteAttachments
                if filename of anAttachment is equal to aFileName then return evernoteNote
            end repeat
            append evernoteNote attachment aFile
        end tell
        return evernoteNote
    end appendEvernoteNoteAttachment
    
    -- Returns a list of existing notes matching the given source URL. Returns an empty list
    -- if nothing was found, or `missing value` in case of error.
    on noteWithSourceURL(sourceURL)
        if sourceURL is "" or sourceURL is missing value then return missing value
        tell application "Evernote"
            try
                set defaultNoteBook to first item of (every notebook whose default is true)
            on error errorMessage number errorNumber
                set defaultNoteBook to missing value
            end try
    
            if defaultNoteBook is missing value then return missing value
    
            set notesList to notes of defaultNoteBook whose source URL is sourceURL
            return notesList
        end tell
    end noteWithSourceURL
    
    on noteContentsAsText(formattedReference, citeKey, aLink)
        set noteContents to ""
        if formattedReference is not missing value then set noteContents to formattedReference & linefeed & linefeed
        if citeKey is not missing value then set noteContents to noteContents & "{" & citeKey & "}" & linefeed
        if aLink is not missing value then set noteContents to noteContents & aLink & linefeed & linefeed
        return noteContents
    end noteContentsAsText
    
    on noteContentsAsHTML(formattedReference, citeKey, aLink)
        set htmlNoteContents to ""
    
        -- NOTE: The Papers scripting API currently doesn't offer the formatted reference in HTML format so it's tricky to generate the note's HTML
        -- content properly. Thus we're currently using a hack and create a temporary note to have Evernote generate the HTML content for us.
        set plainNoteContents to my noteContentsAsText(formattedReference, citeKey, aLink)
        set tempEvernoteNote to my createEvernoteNote("(temp note)", aLink, plainNoteContents, {})
        if tempEvernoteNote is not missing value then
            tell application "Evernote"
                set htmlNoteContents to HTML content of tempEvernoteNote
                delete tempEvernoteNote
            end tell
        end if
    
        -- alternatively, we could create some simple HTML content ourselves, but that would cause any existing link formatting to get lost
        --if formattedReference is not missing value then set htmlNoteContents to "<div>" & formattedReference & "</div><div><br /></div>"
        --if citeKey is not missing value then set htmlNoteContents to htmlNoteContents & "<div>{" & citeKey & "}</div>"
        --if aLink is not missing value then set htmlNoteContents to htmlNoteContents & "<div>" & aLink & "</div><div><br /></div>"
    
        return htmlNoteContents
    end noteContentsAsHTML
    
  21. 141 Posted by dperlman on 14 Oct, 2015 02:59 PM

    dperlman's Avatar

    Hi Mathias, I love it that you are supportive of Applescript! I have a question about AS integration in Papers. Is Papers "attachable" at all? I'm trying to come up with a system for supporting humanities research where it is possible to create highlight annotations in a PDF which are hierarchically labeled and stored in a database. If it were possible to script Papers with something like "On create new highlight item" that would be perfect...

    Thanks!

  22. 142 Posted by matthias on 14 Oct, 2015 03:38 PM

    matthias's Avatar

    Hi Dave,

    Is Papers "attachable" at all? […] If it were possible to script Papers with something like "On create new highlight item" that would be perfect...

    I see. We've discussed script attachability among team members and we have a feature request for it in our ticket database. I.e., it would be nice if Papers could trigger scripts at certain application events. That said, nothing has been done about it yet and I cannot promise anything at this stage. But I agree something along these lines would be super useful!

    Matthias

  23. 143 Posted by Jason on 12 Nov, 2015 03:52 PM

    Jason 's Avatar

    Greetings,

    In an attempt to help automate the update procedure for my CV, I’ve created two “smart groups” to track my published articles and the other tracks abstracts presented at conferences. I am trying to create a separate .bib file for each one so that I can reference each one independently in my .txt file. My goal is to have these automatically updated, how might this be accomplished?

    Cheers,
    Jason

  24. 144 Posted by Christian on 11 Dec, 2015 12:21 PM

    Christian's Avatar

    Hi.

    I was wondering if I can prevent papers from creating these webarchive supplements in any way? If not, is there a way to write a script that goes through my library and remove all of them?

    Cheers,
    Christian

  25. 145 Posted by matthias on 19 Dec, 2015 08:37 AM

    matthias's Avatar

    Hi Jason,

    apologies for the very late reply!

    I’ve created two “smart groups” to track my published articles and the other tracks abstracts presented at conferences. I am trying to create a separate .bib file for each one

    Papers offers a "virtual file system" (aka "virtual disk") feature which automatically exports BibTeX files for all of your collections, labels, etc. More information about this feature is here:

    http://support.mekentosj.com/kb/tutorials/how-to-access-your-librar...

    Note that the paths to the BibTeX files in the "virtual file system" contain spaces. These may cause trouble for LaTeX upon compilation. However, you could create a symlink to the BibTeX file in the virtual disk and reference that symlink in your LaTeX file. If BibTeX compilation still has trouble, simply try to repeat the "Compile BibTeX" command one or two times more.

    Alternatively, you could use the Papers scripting API to export certain papers to a BibTeX file. Unfortunately, the scripting API doesn't yet provide access to your collections. But if you can assign unique tags to your papers & abstracts (e.g., "my papers" & "my abstracts"), a script similar to the one below could be used to export all of these tagged publications to a specific BibTeX file.

    -- Exports all publications with the specified keyword to a BibTeX file on your desktop.
    
    -- Name of the keyword for which matching publications should be exported.
    property keywordName : "my papers"
    
    -- Name of the exported BibTeX file.
    property outFileName : "Bibliography.bib"
    
    tell application "Papers"
        set outFilePath to ((path to desktop from user domain) as string) & outFileName -- saves the exported BibTeX file to your desktop
        --set outFilePath to POSIX file "/Users/msteffens/Dev/Projects/Papers/Test/Bibliography.bib" -- alternatively, specify a full POSIX path
    
        set pubList to every publication item whose keyword names contains keywordName
        export pubList to outFilePath
    end tell
    

    Hope this helps.
    Best regards,
    Matthias

  26. 146 Posted by matthias on 21 Dec, 2015 02:58 PM

    matthias's Avatar

    Hi Christian,

    I was wondering if I can prevent papers from creating these webarchive supplements in any way?

    ATM, there's no preference setting to prevent the creation of .webarchive files. We have a feature request for this.

    If not, is there a way to write a script that goes through my library and remove all of them?

    You could probably remove them via a script like the one below. But please note that this isn't recommended (e.g., since this may also remove other .webarchive files which you've added yourself). As always, you should make a full backup of your shared & local library(*) before performing any actions like that.

    tell application id "com.mekentosj.papers3"
        delete (every supplemental file item whose full path ends with ".webarchive" and mime type is "application/x-webarchive")
    end tell
    

    (*) The locations of your shared & local library can be retrieved via a script like the one below (which copies the paths of the shared library & local database to the clipboard).

    tell application id "com.mekentosj.papers3"
        tell front library window to set the clipboard to "Shared Library: " & library path & linefeed & "Local Database: " & database path
    end tell
    

    Best, Matthias

  27. 147 Posted by matthias on 21 Dec, 2015 03:18 PM

    matthias's Avatar

    Hi everybody, the script below was inspired by a blog post by Jeffrey Taekman about how he makes use of papers3://… links: http://wippp.com/blog/papers-links/

    The script below copies citation info (i.e., citekey, Papers link & the formatted reference) of all selected papers to the clipboard. I.e., you don't need to assemble this info manually. You can then paste this string into your note taking app or wherever. The format of the generated string can be adopted within the script. Note that this script requires the Papers Scripting Lib (v0.8 or greater) which is bundled within the attached script application. It should be also possible to run this script from LaunchBar or similar tools.

    Best, Matthias

    -- Copies citation info (i.e., citekey, Papers link & the formatted reference) of all selected papers to the clipboard.
    -- This script requires the Papers Scripting Lib, v0.8 or greater
    use PapersLib : script "PapersScriptingLib"
    use scripting additions
    
    -- Template string that defines how a paper's citation info gets merged. Supported placeholders:
    --    Citekey: <CITEKEY> (Example: "Down:2011ef")
    --    Papers link: <PAPERSLINK> (Example: "papers3://publication/doi/10.1093/bioinformatics/btr020")
    --    Formatted reference: <REFERENCE> (formatted according to your "Citations" preferences)
    --    Author info: <AUTHORINFO> (formatted according to your "Sources" preferences; Example: "Thomas A Down et al.")
    --    Bundle info: <BUNDLEINFO> (Example: "Bioinformatics, 2011 vol. 27 (6) pp. 889-890")
    --    Short bundle info: <SHORTBUNDLEINFO> (Example: "2011 vol. 27 (6) pp. 889-890")
    --    Citation: <CITATION> (Example: "Down et al. (2011)")
    property citationInfoTemplate : "{<CITEKEY>} <PAPERSLINK> <REFERENCE>"
    
    property seperatorBetweenPapers : linefeed & linefeed
    
    on run
        tell application id "com.mekentosj.papers3"
            -- if you edit searchPatterns, make sure that you edit replacePatterns as well
            set searchPatterns to {"<CITEKEY>", "<PAPERSLINK>", "<REFERENCE>", "<AUTHORINFO>", "<BUNDLEINFO>", "<SHORTBUNDLEINFO>", "<CITATION>"}
    
            set selectedPubs to selected publications of front library window
            set citationInfoList to {}
            repeat with aPub in selectedPubs
                set replacePatterns to {citekey of aPub, item url of aPub, formatted reference of aPub, author info of aPub, bundle info of aPub, short bundle info of aPub, short publication info of aPub}
                set citationInfo to citationInfoTemplate
                repeat with i from 1 to count of searchPatterns
                    set aSearchPattern to item i of searchPatterns
                    set aReplacePattern to item i of replacePatterns
                    set citationInfo to PapersLib's textReplace(citationInfo, aSearchPattern, aReplacePattern)
                end repeat
                copy citationInfo to end of citationInfoList
            end repeat
    
            if citationInfoList is {} then
                PapersLib's displayError("Nothing selected!", "Please select some publications in your Papers library.", 10, true)
                return ""
            else
                set mergedCitationInfo to PapersLib's mergeTextItems(citationInfoList, seperatorBetweenPapers)
                set the clipboard to mergedCitationInfo
                return mergedCitationInfo
            end if
        end tell
    end run
    
  28. 148 Posted by Christian Werne... on 21 Dec, 2015 05:10 PM

    Christian Werner's Avatar

    Thanks for this!

    Actually I never ever created a webarchive on purpose! Don't see the point. If I have any info but not the PDF yet it's in the metadata... Is there another use instead of a simple placeholder for PDFs one has no access to? I try to keep my library as complete as possible... This means if it's the full source it goes in, otherwise it might get in but I'll try to hunt the PDF in the next days...
    Are there other common uses for the webarchive I don't get? Or is this for a different use scenario of Papers?

    Cheers anyways,
    Christian

  29. 149 Posted by matthias on 21 Dec, 2015 05:38 PM

    matthias's Avatar

    Hi Christian,

    Is there another use instead of a simple placeholder for PDFs one has no access to?

    The .webarchive files are indeed useful if you don't have the PDF version. Webpage content and location can change, so Webarchive files are useful since they preserve the content and appearance of the web resource at a certain time. A local copy of an article's web resource is also useful when you're working offline or when you don't have access to that specific resource (e.g., because it's only accessible from your institute's network). Also, some resources are only available as a web resource and not as a PDF.

    Personally, I think that the automatically saved Webarchive files are more useful than annoying. They aren't that big, and having worked in polar research (with several months on research vessels w/o internet access), I can definitely see their value. That said, I realize that these files aren't useful to everyone and we should probably make this optional.

    Best, Matthias

  30. 150 Posted by Christian Werne... on 21 Dec, 2015 08:29 PM

    Christian Werner's Avatar

    Fair enough.

     A preference setting would be nice. If I recall correctly I was mostly annoyed since due to papers always pulling in webarchives the lower rigth panel by default has the webarchive / supplement field activated and if then manually drop a pdf file it would end up as a supplement more often than not without me noticing... Not a biggy but annoying since it took me a while to clean this up again by saven the pdf to desktop and dropping it again, this time on the left spot (yes, I noticed that pretth late ;-))...
    Not sure if you know what I'm trying to describe here?

    Thanks for improving Papers!

    Christian

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