I’ve been annoyed for awhile now that Safari was auto-opening text files that I was trying to download in rapid succession via option-click on a series of links. The text documents kept popping up in front of my browser window and getting in the way. Safari used to have preferences that you could set to tell it what to do with specific file types, but not anymore – at least not since Safari 10 on MacOS Sierra (10.12.x). The only options were to make Safari auto-open “safe” files or not auto-open any files at all. I was about to give up again. I’ve tried to figure this out on multiple other occasions, but I’d finally had enough and decided to roll my own using Automator and some clever scripting…
This solution doesn’t control what Safari does, but rather attaches a folder action to your downloads folder. Here’s what you have to do:
- If you haven’t done so already, disable Safari’s Open “safe” files after downloading’ setting by unchecking it in the General preferences tab:
- Open /Applications/Automator.app
- Click the “New Document” button:
- Select “Folder Action” and click “Choose”:
- Select “Other…” folder and find your downloads folder:
- Find the “Run Shell Script” action and drag it into your workflow:
- Next to “Shell:” select “/bin/tcsh”:
- Next to “Pass input:” select “as arguments”:
- Replace the contents of the text window with the following code linked on pastebin:
- [OPTIONAL] You can edit the OPENTHESE list of comma-delimited file extensions at the top to decide which file types you would like to auto-open and omit any file types you do not want to auto-open.
- Save your workflow (e.g. type command-s or select File->Save)
I also have my folder action maintain a log of things it has auto-opened. Doing this can also be handy for debugging to see why any files may not be opening. Here’s an example of the entire script:
Just replace “yourname” with your username in the LOG file path and make sure the path to your downloads folder is correct in the first line. Note, don’t name your log file with an extension that gets auto-opened.
A couple notes on the perl script that evaluates and opens the files. It assumes that the files passed from the folder action have absolute paths. It needs to do this to be able to join portions of file names that have spaces in them as a single file name. There is one drawback of the script. It cannot handle folders containing spaces in the folder name anywhere in the folder path.
And that’s it. You can test out your new folder action by dropping a file you expect to open into the downloads folder. Try a PDF. Note, the Finder needs to know how to open a file type you allow to be opened. You can test this by double-clicking a file you want to be auto-opened. If the Finder prompts you about what application to use when opening it, you will have to do the following for the folder action to be able to open the file in the desired application:
- Select the file in the Finder
- Select File->Get Info
- Expand the “Open with:” section if it’s not already expanded, using the little triangle.
- Select the desired application to open the file in the drop-down list.
- Click the “Change All…” button.
- Click “Continue” in the resulting dialog window.