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)

  1. 1 Posted by Lars Lundqvist on 12 Jul, 2014 03:05 PM

    Lars Lundqvist's Avatar

    I Copy As - Notes, Link and Magic citation (i.e three copy-paste) to get the wanted info from Papers 3 to Scapple for further thought and analyses. Would it be possible to write an Apple script that does all of this in one move and not three?

  2. 2 Posted by matthias on 12 Jul, 2014 03:54 PM

    matthias's Avatar

    Hi Lars,

    in theory, this should be possible, yes. However, the scripting dictionary is still a bit in flux and a few objects & properties are still missing. Notes and cite key should be possible with the upcoming beta, link/URL still need to be added (so will require a subsequent beta release). That said, your use case is a perfect case for AppleScript and we should strive to make that possible.

    Matthias

  3. 3 Posted by Chris on 12 Jul, 2014 05:47 PM

    Chris's Avatar

    Hi Matthias,

    I am very excited about applescript support. I have been able to get betas in the past. Is 3.0.35 already available? If so, how do we download it? In the past I remember it being available via "Check of updates..."

    Thanks!

    • Chris
  4. 4 Posted by matthias on 12 Jul, 2014 06:30 PM

    matthias's Avatar

    Hi Chris,

    I'm glad to hear you're looking forward to it.

    The upcoming beta version with scripting support isn't yet available, sorry for the confusion! But it should go public during this weekend (I hope), definitely on Monday.

    Matthias

  5. 5 Posted by Hemmik on 14 Jul, 2014 11:27 AM

    Hemmik's Avatar

    Is there a direct download link available? It will not update under 10.10 (which is ironically fixed in the new version:))

  6. 6 Posted by matthias on 14 Jul, 2014 12:41 PM

    matthias's Avatar

    Hi Hemmik,

    Is there a direct download link available?

    Please try this download link: http://downloads.mekentosj.com/papers_3035_126.dmg

    Matthias

  7. 7 Posted by Jason on 14 Jul, 2014 06:59 PM

    Jason 's Avatar

    I am very excited about this opportunity and provide as much feedback as possible.

    Cheers,
    Jason

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Jason G. Craggs, Ph.D.
    Director of Human Neuroimaging
    The Center for Pain Research & Behavioral Health
    P: (352) 273-6151
    W: http://painlab.org/

  8. 8 Posted by matthias on 14 Jul, 2014 07:31 PM

    matthias's Avatar

    Hi Jason,

    I am very excited about this opportunity and provide as much feedback as possible.

    That's great to hear and much appreciated!

    As soon as I get a bit of time, I'd like to write a small sample script that exports Papers annotations (plus their related cite key/publication info) to individual files.

    It would be interesting for me to hear what kind of formats people would like to see. I mean, would a plaintext format work initially? (Papers notes are plaintext ATM)

    And how should the information inside an individual note file be organized? I guess some kind of customization/template options would be useful...

    Looking forward to your feedback.

    Matthias

  9. 9 Posted by Chris on 14 Jul, 2014 07:39 PM

    Chris's Avatar

    Matthias,

    That would be great! I've looked at the dictionary and have been playing with the applescript a bit, but I'm a novice at this point. Plain text would be great (markdown formatting even better), but rtf would be useful as well. The specific kind of output I'm personally hoping to get is the individual annotations within a folder titled "Author - Year - Title". Ideally this would go directly into a selected directory in Devonthink, but I'm honestly happy with anything.

    Thanks!

    • Chris
  10. 10 Posted by matthias on 14 Jul, 2014 07:43 PM

    matthias's Avatar

    Hi Chris,

    thanks for the info, that's useful! How should the note itself be formatted (preferably)?

    Ideally this would go directly into a selected directory in Devonthink, but I'm honestly happy with anything.

    Yes, I had thought about this too. This could be done in a more advanced follow-up version of a first simple script.

    Matthias

  11. 11 Posted by Jan Hendrik Leo... on 14 Jul, 2014 07:45 PM

    Jan Hendrik Leopold's Avatar

    Sent from Mailbox

  12. 12 Posted by Christopher Bir... on 14 Jul, 2014 07:56 PM

    Christopher Birdsall's Avatar

    I was thinking the first line could be the reference in the format of the user's favorite style. Followed by a papers link, then the highlight or note, then a blank field for the user to add a comment, then the cite key. If in the future it would be possible to add a comment to highlight during the annotation process, that would be great - but I know something like that would take time.

    Something like this (I guess the only markdown I'm using here is **bold**, so not a big a deal if it has it or not):

    Willems, Tom, and Wouter Van Dooren. 2012. “Coming to Terms with Accountability.” Public Management Review 14 (7): 1011–36. doi:10.1080/14719037.2012.662446.

    papers3://publication/doi/10.1080/14719037.2012.662446

    **Quotation:**

    p.1012: trends such as horizontal public governance may also have led to a displacement of accountability, away from the traditional chain of delegation. Maybe the picture of contemporary accountability does not look so bleak when taking displaced accountability into account. In order to study this proposition, we need broader conceptual lenses on accountability. This article develops a conceptual framework based on combinations of forums for accountability and functions of accountability. We do not want to redefine accountability. Rather, we build on existing concepts in order to develop a macro-perspective on the complex nature of accountability. We believe such a macro-perspective to be crucial to understand and evaluate how accountability works in the complex world of public governance. The main ambitions of this article are of a conceptual nature. Although actual empirical tests or results fall outside its scope, this article hopes to enrich future empirical research. -- Highlighted jul 14, 2014

    **Comment:**

    **CiteKey:**
    [@Willems:2012ej]

  13. 13 Posted by matthias on 14 Jul, 2014 11:02 PM

    matthias's Avatar

    Hi Chris,

    many thanks for the concrete example. Again, that's very useful!

    I'll try to implement something along these lines.

    Matthias

  14. 14 Posted by Lars Lundqvist on 14 Jul, 2014 11:12 PM

    Lars Lundqvist's Avatar

    If Chris' example include all annotations for a given publication, it's exactly what I would like to have. Format? Plain text, so that it's possible to paste it into any application. Or better still, invoke it from within Scapple. Any extra comments could then be added in Scapple (or whatever program you are using).

  15. 15 Posted by matthias on 14 Jul, 2014 11:20 PM

    matthias's Avatar

    Hi Lars,

    If Chris' example include all annotations for a given publication, it's exactly what I would like to have.

    Ok, many thanks for the feedback!

    This should be even easier to achieve. But it will require a script that's different from the script mentioned above since the above script's main feature is to export a single note/annotation per file (which Papers currently cannot do via its UI).

    Plain text, so that it's possible to paste it into any application.

    Ok, thanks for the input. If Scapple is scriptable (I haven't looked at it yet), then a more direct integration should be possible as well.

    Matthias

  16. 16 Posted by jeff on 15 Jul, 2014 01:49 PM

    jeff's Avatar

    Thanks for adding scripting support. I hope to use it to send pdfs to my ereader.

    I don't see a way to get collection information.

    Did I overlook it? If not, would you add it?

    Thanks,
    Jeff

  17. 17 Posted by drhmxie on 15 Jul, 2014 02:04 PM

    drhmxie's Avatar

    Dear
    I Cannot Pay for papers3’s License in China by some reasons, May you sell it in www.taobao.com? or I can buy it by other way?

    xiehongming

  18. 18 Posted by matthias on 15 Jul, 2014 03:53 PM

    matthias's Avatar

    Hi xiehongming, not sure if this is a valid request. If so, please email to support -at- mekentosj -dot- com. This thread is for scripting-related topics.

  19. 19 Posted by matthias on 15 Jul, 2014 03:59 PM

    matthias's Avatar

    Hi Jeff,

    I don't see a way to get collection information. Did I overlook it? If not, would you add it?

    You didn't overlook something, support for collections is still missing, unfortunately. It's one of the bigger items on my TODO list. I'll definitely try to add support for collections but (since it's a bit tricky) it may take a bit of time.

    Maybe, for now, you could workaround the issue by making use of keywords?

    Thanks for your patience,
    Matthias

  20. 20 Posted by jeff on 15 Jul, 2014 04:05 PM

    jeff's Avatar

    Hi Matthias,
    I remember that the Papers 2 also had this limitation. Because I'm curious, would you share what's tricky about it?

    Thanks for suggesting a workaround. I look forward to collection support.

  21. 21 Posted by matthias on 15 Jul, 2014 04:12 PM

    matthias's Avatar

    I remember that the Papers 2 also had this limitation.

    Papers 2 had no scripting support, can you elaborate a bit?

    Because I'm curious, would you share what's tricky about it?

    Collections have many different aspects/features so it's just a bigger thing to implement. There's manual and smart collections, and there's now also shared collections. Then, each mode (Library/Labels/Authors/Sources mode) has different collections. And some of these collections are user created, some are created by Papers and cannot be removed, etc. Also, it would be very nice if you could select certain collection(s) via AppleScript.

    So there's a lot to account for. Thus, I may start with a subset of simple things first.

    Matthias

  22. 22 Posted by jeff on 15 Jul, 2014 04:28 PM

    jeff's Avatar

    Thanks for the description!

    I'm sorry, it was Papers 1. I vaguely recall (in 2010), there was some
    access to Papers data, but I couldn't access collection info. (I think
    maybe we discussed it in the forum.) I used a workaround for a while,
    but ultimately moved to that horrible other reference management
    software, because I could access the data I needed via sqlite. It's good
    to be back.

  23. 23 Posted by matthias on 15 Jul, 2014 04:46 PM

    matthias's Avatar

    Hi Jeff,

    I'm sorry, it was Papers 1. I vaguely recall (in 2010), there was some access to Papers data

    Ah, I see. In Papers 1, you could write your own plugins, so maybe it was that. This predates my time at Papers, and as a user I haven't worked with custom Papers plugins, so I don't really know anything about it.

    Access to the user's own collections is probably the most important thing, so I'll try to start with that.

    Matthias

  24. 24 Posted by Michael Telch on 16 Jul, 2014 12:28 AM

    Michael Telch's Avatar

    Dear Mathias:

    Would you be so kind to email the link for the latest Papers 3 upgrade 3.035. My laptop version running the same Mac OS had the auto download work but my desktop running Papers 3 tells me I’m running the latest version which it is not. II’m not sure why the auto update feature is not working on this machine.

    Michael J. Telch, Ph.D.
    Professor of Psychology
    Director, Laboratory for the Study of Anxiety Disorders

    The University of Texas at Austin
    1 University Station, A8000
    Austin, TX 78712-0187

    (512) 475-8488 (Office)
    (512) 471-3393 (Secretary)
    (512) 404-9118 (Lab)
    (512) 590-8657 (FAX)

    [email blocked]<mailto:[email blocked]> (Email)
    www.Telchlab.com<http://www.telchlab.com/> (Websi
    The information transmitted in this email is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited. If you received this email in error, please contact the sender and delete the material from any computer.

  25. 25 Posted by matthias on 16 Jul, 2014 07:17 AM

    matthias's Avatar

    Hi Michael,

    Would you be so kind to email the link for the latest Papers 3 upgrade 3.035

    Please try this download URL:

    http://downloads.mekentosj.com/papers_3035_127.dmg

    my desktop running Papers 3 tells me I’m running the latest version which it is not. II’m not sure why the auto update feature is not working on this machine.

    Please make sure that you have marked the checkbox for download of beta releases: I.e., in the "General" tab of the Papers preferences, mark the checkbox that says "Inform me about available pre-release and beta versions of Papers".

    Best, Matthias

  26. 26 Posted by matthias on 17 Jul, 2014 04:51 PM

    matthias's Avatar

    Hi,

    FYI, the upcoming beta (due out soon) has these scripting-related additions & fixes:

    • The preferred name property of the publication item class was broken and caused an AppleScript error. It should work now again.
    • The citekey property of the publication item class is now writable. This allows you to set your own citekeys via AppleScript. One user has already written a sample script that shows how you could batch-update the citekeys of all publications in your library. And, IIRC, he was ok with publishing it here once the beta is out.
    • All library item subclasses (like, e.g., publication item) have now an item url property. This returns the item's papers3:// URL. This URL can be used to uniquely identify an item. For publications, you can also use it to open its corresponding publication in Papers (see below).

    Examples:

    Here's how to get a list of papers3:// URLs for all publications in your library:

    tell application "Papers" to set u to item url of publication items
    

    You can pass a publication's papers3:// URL to the Finder's open location command to open it in Papers (in Reader mode), e.g.:

    tell application "Finder" to open location "papers3://publication/doi/10.1371/journal.pone.0101513"
    

    Matthias

  27. 27 Posted by rjamiljonna on 18 Jul, 2014 10:52 AM

    rjamiljonna's Avatar

    Hello,

    Does this mean that you will soon start supporting Pages (iWork)? I did not see this mentioned above but it seems like the most obvious (and needed) use of the addition of scripting. The formatting process was much simpler with Pages when it worked in the last version and I cannot wait to ditch my 4 years (too old) version of office. Please help.

  28. 28 Posted by matthias on 18 Jul, 2014 11:09 AM

    matthias's Avatar

    Hi

    Does this mean that you will soon start supporting Pages (iWork)?

    This is unfortunately not under our control since Papers relies on the Pages AppleScript dictionary to provide the necessary functionality. And, as far as I know, while Pages has added some AppleScript functionality back, it's not sufficient to allow proper integration with Papers.

    If this is important to you, please let Apple know about it. We have also filed a general feature request with Apple to open up Pages' citation ability for other applications (see here; the radar number is rdar://8258643). I strongly encourage you to file the same bug request, and be sure to mention that it is a duplicate of bug number 8258643 (the more duplicates of these requests they get, the more likely it is to get implemented).

    Thanks, Matthias

  29. 29 Posted by Johannes Faehnd... on 21 Jul, 2014 06:47 AM

    Johannes Faehndrich's Avatar

    Hey There,

    wanted to post a script which tries to set the citekey of the paper to AuthorNameYear (e.g. Peters2009).
    If a author has multiple papers in one year we add a letter after the key (Peters2009a, Peters2009b) and we replace special characters form the authors name.
    Here the script so far:

    tell application "Papers"
        set pubList to every publication item
        set myList to {}
        repeat with i from 1 to count of pubList
            set aPub to item i of pubList
            if author names of aPub is not "" then
                copy aPub to end of myList
            end if
        end repeat
        set myCleanList to {}
        repeat with i from 1 to count of myList
            set aPub to item i of myList
            if publication year of aPub is not "" then
                copy aPub to end of myCleanList
            end if
        end repeat
        set refList to {}
        repeat with i from 1 to count of myCleanList
            set aPub to item i of myCleanList
            set authors to get author items of aPub
            set authorname to get family name of first item of authors
            set authorname to my clean_string(authorname)
            set pubyear to get publication year of aPub
            if pubyear is not "" then
                set newcitekey to authorname & pubyear
            end if
            set newcitekey to my create_CustomCiteKey(refList, newcitekey)
            set citekey of aPub to newcitekey
        end repeat
    end tell
    on clean_string(TheString)
        --Store the current TIDs. To be polite to other scripts.
        set previousDelimiter to AppleScript's text item delimiters
        set potentialName to removeSpecialChar(TheString)
        set legalName to {}
        set legalCharacters to {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}
        repeat with thisCharacter in the characters of potentialName
            set thisCharacter to thisCharacter as text
            if thisCharacter is in legalCharacters then
                set the end of legalName to thisCharacter
            end if
        end repeat
        set AppleScript's text item delimiters to ""
        if length of legalName is greater than 32 then
            set legalName to items 1 thru 32 of legalName as text
        else
            set legalName to legalName as text
        end if
        set AppleScript's text item delimiters to previousDelimiter
        return legalName
    end clean_string
    --lets hope no one has more then 24 papers a year.... 
    on create_CustomCiteKey(refList, citekey)
        --Store the current TIDs. To be polite to other scripts.
        set previousDelimiter to AppleScript's text item delimiters
        set legalCharacters to {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
        set orgCiteKey to citekey
        set counter to 1
        repeat until refList does not contain citekey
            set nextAppendant to item counter of legalCharacters
            set citekey to orgCiteKey & nextAppendant
            set counter to counter + 1
        end repeat
        set the end of refList to citekey
        return citekey
    end create_CustomCiteKey
    on removeSpecialChar(MyString)
        set MyString to my replaceText("Ä", "Ae", MyString)
        set MyString to my replaceText("À", "A", MyString)
        set MyString to my replaceText("Á", "A", MyString)
        set MyString to my replaceText("Â", "A", MyString)
        set MyString to my replaceText("Æ", "AE", MyString)
        set MyString to my replaceText("Ã", "A", MyString)
        set MyString to my replaceText("Å", "A", MyString)
        set MyString to my replaceText("Ā", "A", MyString)
        set MyString to my replaceText("ä", "ae", MyString)
        set MyString to my replaceText("à", "a", MyString)
        set MyString to my replaceText("á", "a", MyString)
        set MyString to my replaceText("â", "a", MyString)
        set MyString to my replaceText("ã", "a", MyString)
        set MyString to my replaceText("æ", "ae", MyString)
        set MyString to my replaceText("å", "a", MyString)
        set MyString to my replaceText("ā", "a", MyString)
        set MyString to my replaceText("Ö", "Oe", MyString)
        set MyString to my replaceText("Ô", "O", MyString)
        set MyString to my replaceText("Ò", "O", MyString)
        set MyString to my replaceText("Ó", "O", MyString)
        set MyString to my replaceText("Œ", "OE", MyString)
        set MyString to my replaceText("Ø", "O", MyString)
        set MyString to my replaceText("Ō", "O", MyString)
        set MyString to my replaceText("Õ", "O", MyString)
        set MyString to my replaceText("ö", "oe", MyString)
        set MyString to my replaceText("ò", "o", MyString)
        set MyString to my replaceText("ó", "o", MyString)
        set MyString to my replaceText("õ", "o", MyString)
        set MyString to my replaceText("ô", "o", MyString)
        set MyString to my replaceText("œ", "oe", MyString)
        set MyString to my replaceText("ø", "o", MyString)
        set MyString to my replaceText("ō", "o", MyString)
        set MyString to my replaceText("Ü", "Ue", MyString)
        set MyString to my replaceText("Û", "U", MyString)
        set MyString to my replaceText("Ù", "U", MyString)
        set MyString to my replaceText("Ú", "U", MyString)
        set MyString to my replaceText("Ū", "Ue", MyString)
        set MyString to my replaceText("ü", "ue", MyString)
        set MyString to my replaceText("ù", "u", MyString)
        set MyString to my replaceText("ú", "u", MyString)
        set MyString to my replaceText("ū", "u", MyString)
        set MyString to my replaceText("ß", "ss", MyString)
        set MyString to my replaceText("@", "AT", MyString)
        set MyString to my replaceText("Ç", "C", MyString)
        set MyString to my replaceText("Ć", "C", MyString)
        set MyString to my replaceText("Č", "C", MyString)
        set MyString to my replaceText("ç", "c", MyString)
        set MyString to my replaceText("ć", "c", MyString)
        set MyString to my replaceText("č", "c", MyString)
        set MyString to my replaceText("È", "E", MyString)
        set MyString to my replaceText("É", "E", MyString)
        set MyString to my replaceText("Ê", "E", MyString)
        set MyString to my replaceText("Ë", "E", MyString)
        set MyString to my replaceText("Ē", "E", MyString)
        set MyString to my replaceText("Ė", "E", MyString)
        set MyString to my replaceText("Ę", "E", MyString)
        set MyString to my replaceText("è", "e", MyString)
        set MyString to my replaceText("é", "e", MyString)
        set MyString to my replaceText("ê", "e", MyString)
        set MyString to my replaceText("ë", "e", MyString)
        set MyString to my replaceText("ē", "e", MyString)
        set MyString to my replaceText("ė", "e", MyString)
        set MyString to my replaceText("ę", "e", MyString)
        set MyString to my replaceText("Î", "I", MyString)
        set MyString to my replaceText("Ï", "I", MyString)
        set MyString to my replaceText("Í", "I", MyString)
        set MyString to my replaceText("Ī", "I", MyString)
        set MyString to my replaceText("Į", "I", MyString)
        set MyString to my replaceText("Ì", "I", MyString)
        set MyString to my replaceText("î", "i", MyString)
        set MyString to my replaceText("ï", "i", MyString)
        set MyString to my replaceText("í", "i", MyString)
        set MyString to my replaceText("ī", "i", MyString)
        set MyString to my replaceText("į", "i", MyString)
        set MyString to my replaceText("ì", "i", MyString)
        set MyString to my replaceText("Ł", "L", MyString)
        set MyString to my replaceText("Ś", "S", MyString)
        set MyString to my replaceText("Š", "S", MyString)
        set MyString to my replaceText("ś", "s", MyString)
        set MyString to my replaceText("š", "s", MyString)
        set MyString to my replaceText("Ñ", "N", MyString)
        set MyString to my replaceText("Ń", "N", MyString)
        set MyString to my replaceText("ñ", "n", MyString)
        set MyString to my replaceText("ń", "n", MyString)
        set MyString to my replaceText("ÿ", "y", MyString)
        set MyString to my replaceText("Ÿ", "Y", MyString)
        set MyString to my replaceText("Ž", "Z", MyString)
        set MyString to my replaceText("Ź", "Z", MyString)
        set MyString to my replaceText("Ż", "Z", MyString)
        set MyString to my replaceText("ž", "z", MyString)
        set MyString to my replaceText("ź", "z", MyString)
        set MyString to my replaceText("ż", "z", MyString)
        return MyString
    end removeSpecialChar
    -- find : Text to be found
    -- replace : Text to replace with
    -- someText : Text to be searched
    on replaceText(find, replace, someText)
        considering case
            set prevTIDs to text item delimiters of AppleScript
            set text item delimiters of AppleScript to find
            set someText to text items of someText
            set text item delimiters of AppleScript to replace
            set someText to "" & someText
            set text item delimiters of AppleScript to prevTIDs
        end considering
        return someText
    end replaceText
    

    I'm looking forward to your critique and comments.

    Best regards,
    Johannes

  30. 30 Posted by drhmxie on 21 Jul, 2014 11:46 PM

    drhmxie's Avatar

    Hi

    I sometimes add a paper to A collections mistakenly, then How can I remove from A collections in Papers and let the paper remain in B collections?

    Thanks

    xiehongming

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