Last active
August 29, 2015 14:07
-
-
Save januz/e7631c5d8d423cbb657b to your computer and use it in GitHub Desktop.
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
# path to python which has pypdf(2) installed | |
set pythonExecutable to "/usr/bin/python" | |
# path to python script that extracts DOI | |
set doiExtract to "/Users/XXX/path/to/pypdf_doi.py" | |
# the PDF file that is going to be processed | |
set pdfPath to quoted form of (POSIX path of theFile) | |
set newPDF to theFile | |
tell application "Finder" to set pdfName to name of theFile | |
# start BibDesk if it is not running | |
tell application "System Events" to set runningApps to (name of processes) | |
if runningApps contains "BibDesk" then | |
# do nothing | |
else | |
tell application "BibDesk" to activate | |
delay 2 | |
end if | |
# try to extract DOI, if not found, ask to enter it manually | |
try | |
set theDOI to do shell script pythonExecutable & " " & doiExtract & " " & pdfPath | |
on error | |
tell application "System Events" | |
display dialog "No DOI found! | |
PDF: " & pdfName & " | |
- Press 'OK' to open the PDF and enter DOI manually | |
- Press 'Cancel' to delete the file" buttons ¬ | |
{"Cancel", "OK"} default button 2 | |
end tell | |
if button returned of result = "OK" then | |
tell application "Finder" to open file theFile | |
tell application "System Events" | |
display dialog "Enter DOI! | |
PDF: " & pdfName default answer "" | |
end tell | |
set theDOI to text returned of result | |
else if button returned of result = "Cancel" then | |
tell application "Finder" to delete theFile | |
return | |
end if | |
end try | |
# try to add publication from DOI, if not successful, ask to enter it manually | |
if theDOI is "" then | |
tell application "System Events" | |
display dialog "No DOI found or entered! | |
PDF: " & pdfName & " | |
- Pressing 'OK' or 'Cancel' will delete the file!" buttons ¬ | |
{"Cancel", "OK"} default button 2 | |
tell application "Finder" to delete theFile | |
return | |
end tell | |
else | |
tell front document of application "BibDesk" | |
repeat | |
set theImportPubs to import for search term theDOI | |
if theImportPubs is not {} then | |
exit repeat | |
else | |
tell application "System Events" | |
display dialog "Failed to import publication" & " | |
PDF: " & pdfName & " | |
DOI: " & theDOI & " | |
- Press 'OK' to open the PDF and enter DOI manually | |
- Press 'Cancel' to delete the file" buttons ¬ | |
{"Cancel", "OK"} default button 2 | |
end tell | |
if button returned of result = "OK" then | |
tell application "Finder" to open file theFile | |
tell application "System Events" | |
display dialog "Enter DOI! | |
PDF: " & pdfName default answer "" | |
end tell | |
set theDOI to text returned of result | |
else if button returned of result = "Cancel" then | |
tell application "Finder" to delete theFile | |
return | |
end if | |
end if | |
end repeat | |
set theImportPub to (get item 1 of theImportPubs) | |
add newPDF to theImportPub | |
select theImportPub | |
tell theImportPub | |
auto file | |
set pubAuthor to (value of field "Author") | |
set pubYear to (get publication year as string) | |
set pubTitle to (get title as string) | |
end tell | |
save | |
tell application "System Events" | |
display dialog "Successfully imported publication! | |
Author: " & pubAuthor & " | |
Year: " & pubYear & " | |
Title: " & pubTitle giving up after 5 | |
end tell | |
end tell | |
end if |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment