How to make Safari 10 auto-open specific file types

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:

  1. If you haven’t done so already, disable Safari’s Open “safe” files after downloading’ setting by unchecking it in the General preferences tab:safariprefs.png
  2. Open /Applications/Automator.appautomator.png
  3. Click the “New Document” button:newdoc.png
  4. Select “Folder Action” and click “Choose”:folderaction.png
  5. Select “Other…” folder and find your downloads folder:fa-other.png
  6. Find the “Run Shell Script” action and drag it into your workflow:wnxq7g
  7. Next to “Shell:” select “/bin/tcsh”:shell-tcsh.png
  8. Next to “Pass input:” select “as arguments”:inp-asargs.png
  9. Replace the contents of the text window with the following code linked on pastebin:autoopencode2.png
  10. [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.
  11. 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:

autoopencode-full3.png

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:

  1. Select the file in the Finder
  2. Select File->Get Info
  3. Expand the “Open with:” section if it’s not already expanded, using the little triangle.
  4. Select the desired application to open the file in the drop-down list.
  5. Click the “Change All…” button.
  6. Click “Continue” in the resulting dialog window.

Happy downloading!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s