I often collect PDFs off the web, and save far too many of them. But often they are tear sheets, product blurbs, or rocket science I don’t understand that I don’t want to have cluttering up the hard drive. When I have finished with a PDF that I want to trash, I use an AppleScript to immediately delete the file without leaving Acrobat. The script is accessed from the Script menu in the Menu Bar, and immediately deletes the file. The document is still open, but when you close it, it’s gone.
Here’s a simple script that may be useful It works with Acrobat Pro and Acrobat X, but will not work with Acrobat Reader, because Reader is not “scriptable”. It also can’t work with Preview, which is also not scriptable (!)
--this script will immediately delete the active Acrobat document --these "--" things are comments --use at your own risk, etc --attribute to JJ McClintock 20121101 --initialize variable set doc_path to "" tell application "Adobe Acrobat Pro" activate --get the name & path of the active document set doc_path to get file alias of active doc as string end tell --a dialog confirms display dialog "Are you sure you want to delete " & doc_path & " ?" tell application "Finder" move file doc_path to trash end tell tell application "Adobe Acrobat Pro" activate end tell
That’s the end of the script, not much to it. It asks Acrobat what and where the file is, and then tells the Finder to trash it.
How to make it work? Copy the script. Locate the AppleScript editor on your Mac – it’s in your Utilities folder, but it’s easiest to use Spotlight, CMD-Spacebar and type applescript editor. Open the editor and paste the script. Click the “Complile” button, which will format and check the script to make sure it doesn’t violate any obvious rules. Save the file: select “Application” in the “File Format” dropdown, name the file “Delete PDF” or whatever, and save it in a convenient location. It’s going to wind up in your script menu, but the actual folder for your script menu is buried in the bowels of your home directory. Here’s how to find it.
While in the editor, select Preferences from the AppleScript Editor menu and check the “Show Script menu in menu bar” box. Close the prefs window, you should see a scroll icon in the right part of the menu bar.
Click the Script icon and you will see a bunch of Apple supplied scripts, I think, but at the top select “Open Scripts Folder|Open User Scripts Folder. That’s a short way of getting to the scripts folder, which is located in Mac HD: Users:Your User name:Library:Scripts, Since Apple has invisibilized the Library folder, you have to horse around to get to it. Alternatively, from the Finder, select the Go menu while holding the Option key, and select Library. Anyway if you place your new Delete PDF script in the user scripts folder, it’s available at all times from the scripts folder in the menu.
Hopefully you will find this to be a handy little gadget. If you have several documents open, the script will delete the “front most” one. If you make a mistake, your file is still open and can be re-saved, and is in the Trash and can be recovered.
I have another handy script I use frequently to rename PDF documents. How often do you collect a PDF with the helpful name “aa3695-05.pdf”? If I want to keep it for reference I’d like to rename it something I can live with, like “Lucky Imaging-Hi Rez Imaging from the Ground.pdf” Using the handy “Rename PDF” script, it’s easy.
--saves pdf with new name, deletes original pdf --default save folder is Documents --won't work if file doesn't exist on disk. Why should it? --won't work on secure documents. They wouldn't be secure. --modified 20110526 to (awkwardly) handle long file names --since acrobat's applescript implementation doesn't permit file names longer than 32 char, --need to make temporary short file name for saving, which will be renamed with the chosen long file name --cleaned up 20121101 --initialize some variables set default_folder to (path to documents folder from user domain as string) set doc_path to default_folder set new_file to choose file name with prompt "Enter new file name" default location alias default_folder set short_name to default_folder & "short_name.pdf" set file_string to new_file as string set text item delimiters to ":" set file_string_list to every text item of file_string set last_word to text item -1 of file_string tell application "Adobe Acrobat Pro" set doc_path to get file alias of active doc as string set active_doc to name of active doc --try blocks help the script to fail gracefully if an error occurs try save active doc to file short_name with replacing --save active doc to file short_file with replacing on error --biggest source of error is trying to rename a secured document display dialog "Save failed. Is the document secured? You can still rename using Save As or in the Finder." buttons {"Cancel"} end try close active doc end tell set path_string to quoted form of POSIX path of short_name set shell_string to "cp " & path_string & " " & quoted form of POSIX path of new_file & "" --shell script uses unix commands to do file tasks, in this case copy do shell script shell_string tell application "Adobe Acrobat Pro" open file new_file end tell tell application "Finder" --trash original file and the temporary "short_name" file move file doc_path to trash move file short_name to trash end tell --restore text item delimiters to default setting set text item delimiters to ""
Prepare this script like you did the “Delete PDF” script before. From Acrobat Pro or Acrobat X, if you want to rename a PDF file to something human readable, go to the script menu, select the “Rename PDF” script. You’ll be prompted for a name and location for the file. If for some reason Acrobat won’t let you rename the document, for example, it is secured, then you get an alert. You should still be able to go to the Finder, and rename it the way you used to.
11 thoughts on “Use AppleScript to Manage PDFs”