Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Set Safari's download folder via script for private browsing Web Browsers
Safari's Private Browsing feature (Apple: "Keep cookies, history and caches private when using Safari on a public access Mac") is handy if you want to ensure there's no trace left behind when accessing sensitive sites, such as online banking services. However, Safari will still download files to its usual download folder, as specified in its preferences. If you're downloading private data (such as banking statements), this could be a concern.

This AppleScript will automate the process of enabling Private Browsing, mounting a disk image (which should be created as an Encrypted image), and setting Safari's download location to the mounted image.

Copy and paste the script into Script Editor and take a look at the code. You'll need to modify the DownloadFolder, PrivateImage, and PrivateVolume values to match your setup. See this page for a guide to creating an encrypted disk image. Note in the example code that I've pointed to a .sparseimage file, which is a disk image able to grow in size. If you create a regular image, it'll have a .dmg extension.

With everything in place, you can save the script (I've called mine Set Private Browsing) into ~/Library -> Scripts -> Applications -> Safari, and have it always available from the Script Menu when Safari's active. Running the script will bring up a prompt from which you can Enable or Disable Private Browsing. Chooseing Enable will mount your encrypted image (you'll be prompted by Disk Utility for the password), enable Safari's Private Browsing feature, and tell Safari to use the encrypted disk to store downloads. Choosing Disable will un-mount the encrypted disk, set Safari back to using your regular download location, and turn off Private Browsing.

The script tries to keep things in a consistent state, so you won't have to tidy up after it in case something goes awry. For example, if the encrypted disk can't be mounted, the script will still enable Private Browsing, but won't mess with Safari's download folder. The code's fully commented, so you should be able to get a good idea of how it behaves.

[robg adds: I haven't tested this one.]
  • Currently 2.75 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (4 votes cast)

Set Safari's download folder via script for private browsing | 6 comments | Create New Account
Click here to return to the 'Set Safari's download folder via script for private browsing' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Set Safari's download folder via script for private browsing
Authored by: displaced on Aug 04, '06 11:08:46AM
Hi everyone... the hint's author here :)

You might find that the part of the script which checks that GUI Scripting is enabled causes an error on your machine. I've noticed I get it intermittently on mine.

If you experience this, make sure you've got GUI Scripting enabled (open /Applications/AppleScript/AppleScript Utility and check the Enable GUI Scripting box), then delete the my check_requirements line (line 13, funnily enough!). You should be good to go after that.

[ Reply to This | # ]
Authored by: faranv on Aug 05, '06 01:01:29PM

Hi, I like the idea of this script, but I'm having some trouble getting it to work. It mounts the image and sets my Private Browsing on, but it won't change my download location to the encrypted disk image when I enable it. Also, if I manually change my download location to the disk image, when I disable the 'Private Browsing' via that script, my download location is not reverted to the original one and the disk is not ejected. Am I doing something wrong? I have put the path for the disk image correctly because it is mounted, and I have called the image 'encrypted' and that's what I set at the beginning of the scripts. Any help would be greatly appreciated!

[ Reply to This | # ]
Set Safari's download folder via script for private browsing
Authored by: maubrowncow on Aug 08, '06 02:06:13PM

looks like i'm getting a "NSReceiverEvaluationScriptError: 4" when the script sets Safari's download location. Specifically at:
click menu item "Preferences?" of menu 1 of menu bar item "Safari" of menu bar 1

any ideas? thanks.

[ Reply to This | # ]
Set Safari's download folder via script for private browsing
Authored by: pokaroo on Aug 09, '06 02:58:09PM

In answer to the question above regarding the "NSReceiverEvaluationScriptError: 4" error, since the script is a plain text file, the special characters required for the script to work are changed. You need to replace the '?' character at the end of 'Preferences' with the elipses character you get when pressing Option-;. I think you need to make this change in at least one other place as well - replace Other? with Other...

You should then be able to recompile and run the script properly.

[ Reply to This | # ]
Possible Fix for Scripting Error
Authored by: JKrahn on Aug 13, '06 04:25:21PM
I was having the same issue. It seems the the GUI scripting doesn't work as expected. I modified the script to use keystrokes rather than GUI scripting and it works just fine. (This may also extend it's ability to older versions of OS X). Let me know how it works for you. I've only posted the changed area, replace it into the existing script.

-- Sets Safari's download location
-- See comment below if the script fails during the setting of the download folder
on set_download_folder(dlFolder)
activate application "Safari"
tell application "System Events" to tell application process "Safari"
keystroke "," using {command down}
tell pop up button 3 of group 1 of group 1 of window "General"
keystroke (ASCII character 31) -- down arrow key
keystroke return -- return key
end tell
keystroke "g" using {shift down, command down}
delay 2 -- NOTE: This value should be increased if the new download location doesn't appear in the Go To Folder dialog
tell window "Go To Folder"
set value of text field 1 to dlFolder
click button "Go"
end tell
click button "Select" of sheet 1 of window "General"
click button 1 of window "General"
end tell
end set_download_folder

- JKrahn

[ Reply to This | # ]

Script for private browsing
Authored by: ojohns on Aug 08, '06 09:08:22PM

Thanks for the neat script. Here's a variant. I don't care much about the download folder, but I got tired of amazon greeting me warmly by name on return visits. Even in "Private Browsing", Safari keeps Cookies and (contrary to documentation) does not reliably zap them when you quit "Private Browsing". So I use this apple script to launch Safari. I pasted it into Script Editor, and then saved it as an app which I put in my dock.

-- Zaps the cookies, history, downloads, and cache files, then launches Safari
-- Sets Private Browsing
-- Also automatically dismisses the pop-up information window
on run {}
do shell script
" pre=/Users/$(whoami)/Library;
rm -rf $pre/Cookies/Cookies.plist $pre/Safari/History.plist
$pre/Safari/Downloads.plist $pre/Cache/Safari >/dev/null 2>&1;
exit 0 "
activate application "Safari"
tell application "System Events" to tell application process "Safari"
click menu item "Private Browsing" of menu 1 of menu bar item "Safari" of menu bar 1
click button "OK" of window 1
end tell
end run


[ Reply to This | # ]