Being an AppleScript noob, I’ve been liberally stealing pieces of code from google searches and pasting them together to come up with something, but I’ve run into problems. Here’s what I want to do in a nutshell:
- If a .ris file gets added to the Downloads folder
- Open that file with EndNote
- and import that file into the currently active library
- jump back to the application that was active before the script started doing stuff
- move the newly imported .ris file to the trash
To that end I’ve attached the script below as a folder action to the downloads folder. The problem is, that EndNote keeps asking me to select the library into which I wish to import. I’ve asked about this on the EndNote forums and there is no solution. The main body of the script was taken from a post there. The section of the script I’m having trouble with is making it select “HydrideLibrary.enlp” when the window “Select a Reference Library” pops up. I tried using system events/keystroke “hydridelibrary”/keystroke return, but that didn’t work either. ANY help would be greatly appreciated!
repeat until temp = 1
tell application "EndNote X3" --is this necessary?
if (name of front window contains "Select a Reference Library") then --waits for front window to be the "select a file" dialog window
set x to 1
end if
end tell
end repeat
delay 0.5
--The next block does not work! What I want it to do is select the .enlp file and hit the return key
tell application "System Events"
tell process "EndNote X3"
select item "HydrideLibrary.enlp" of table 1 of scroll area 2 of group 1 of tab group 1 of window "Select a Reference Library"
delay 1
keystroke return
end tell
end tell
--end unfunctional block
delay 5
tell application "Finder" to move this_item to trash --we're done with that file
Here is the full code, in case that will help. I’m open to any suggestions as to improve the efficiency of this script as a whole, since most of it is borrowed.
-- Direct Export.scpt
-- Thomson Reuters 2009
--
-- Installing:
-- 1) Copy this script to your ~/Library/Scripts/Folder Action Scripts
-- 2) Run the application /Applications/AppleScript/Folder Actions Setup
-- 3) Click on the + in the lower left corner
-- 4) Select the Downloads folder in your Home folder
-- 5) You will then be prompted to select the script, select "Direct Export.scpt" and click on Attach
-- Now when performing a direct export and the file is saved in Downloads, EndNote will will perform an import using that file if it's extension is ris, enw, or ovd
on adding folder items to this_folder after receiving these_items
if (count of these_items) is equal to 0 then --make sure script didn't fire by accident
quit --exit script
else
set front_app to (path to frontmost application as Unicode text) --stores which app was active, so I can get back to it
repeat with i from 1 to number of items in these_items
set this_item to item i of these_items --grabs name of first file to import
set the item_info to the info for this_item --getinfo on this item
set file_extension to the name extension of item_info --get extens ion of file to import
if file_extension = "enw" or file_extension = "ris" or file_extension = "ovd" or file_extension = "cgi" then --is extension of the valid type?
--this block I commented out because I don't use it...
-- Locate the newest version of EndNote. This is located here to prevent this code being run every time a file is downloaded.
-- You could hard code the app by changing the line:
-- set ENApp to ""
-- to
--set ENApp to ""
--set n to 9
--set bExists to false
--repeat while n > 0 and ENApp = ""
--set pathToTest to "/Applications/EndNote X" & n & "/EndNote X" & n & ".app"
--check to see if the App exists
--tell application "Finder" to if exists pathToTest as POSIX file then set bExists to true
--if bExists is true then
--set ENApp to POSIX path of pathToTest
--end if
--set n to n - 1
--end repeat
-- if we were unable to locate a copy of EndNote exit
--if ENApp = "" then
--return
--end if
set ENApp to POSIX path of "/Applications/EndNote X3/EndNote X3.app" --path to EndNote.app
set fileToOpen to POSIX path of this_item -- open the file with EndNote
try
do shell script "open -a \"" & ENApp & "\" \"" & fileToOpen & "\"" --command to open import file with EndNote, will generate a standard "select a file" dialog box asking for the target library where the new entry should go, this takes a while, so I wait for the window to pop up
set temp to 0
repeat until temp = 1
tell application "EndNote X3" --is this necessary?
if (name of front window contains "Select a Reference Library") then --waits for front window to be the "select a file" dialog window
set x to 1
end if
end tell
end repeat
delay 0.5
--The next block does not work! What I want it to do is select the .enlp file and hit the return key
tell application "System Events"
tell process "EndNote X3"
select item "HydrideLibrary.enlp" of table 1 of scroll area 2 of group 1 of tab group 1 of window "Select a Reference Library"
delay 1
keystroke return
end tell
end tell
--end unfunctional block
delay 5
tell application "Finder" to move this_item to trash --we're done with that file
--error handling
on error error_message number error_number
if the error_number is not -128 then
tell application "Finder"
activate
display dialog error_message buttons {"Cancel"} default button 1 giving up after 120
end tell
end if
end try
end if
end repeat
tell application front_app to activate --go back to where you came from!
tell application "Safari" to close window named "Downloads" --close that damn downloads window, should probably check to see if safari is even open, but 99% of the time I get the .ris file from safari
end if
end adding folder items to
Thanks!
Model: iMac
AppleScript: 2.1.2
Browser: Safari 531.22.7
Operating System: Mac OS X (10.6)