Launching browsers in Private Browsing mode

Imagine the following task:

“Use a script to open https://www.example.com/ in Private Browsing mode.”

Well, this does not seem to be a overly sophisticated requirement and should be a rather easy task to implement. For most browsers, that’s absolutely true.

Microsoft Internet Explorer

1
iexplore.exe -private https://www.example.com/

Google Chrome

1
chrome -incognito https://www.example.com/

Mozilla Firefox

1
firefox -private-window https://www.example.com/

Apple Safari

  1. Go to “System Preferences -> Security & Privacy -> Privacy -> Accessibility”
  2. Click “Add”, select “Applications -> Utilities -> Terminal”
  3. Just execute this simple command…
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
osascript -e 'tell application "Safari" to activate
tell application "Safari"
close window 1
end tell

tell application "System Events"
tell process "Safari"
tell menu bar 1
tell menu bar item "File"
tell menu "File"
click menu item "New Private Window"
end tell
end tell
end tell
end tell
end tell

tell application "Safari" to set the URL of the front document to "https://www.example.com/"'

In a nutshell, this AppleScript launches Safari and closes the first browser window, which is non-private, right away. Then it uses OSX accessibility features to click through the menu bar to launch a Private Window. Finally it sets the address of the focused window to the desired URL.

Well… praise Apple for making intuitive software that just works. Magical. </rant>