I have read the previous post on how to get Safari to load up into “Private Browsing”
But it leaves the question of how to handle the popup box
asking if you are sure you want to turn on Private Browsing or not.
Here is my script so far with the error it produces:
Can someone help?
tell application “Safari” to activate
tell application “System Events”
tell process “Safari” to click menu item “Private Browsing…” of menu “Safari” of menu bar 1
if exists button “OK” of window 1 of application process “Safari” then
click button “OK” of window 1 of application process “Safari”
end if
end tell
System Events got an error: NSReceiverEvaluationScriptError: 4
thanks
Bruce24
Model: 2 X 2 GHz Dual-Core Xeon
AppleScript: 1.10.7
Browser: Safari 522.12.1
Operating System: Mac OS X (10.4)
try this, the reference to the menu item is a bit different
activate application "Safari"
tell application "System Events"
tell process "Safari"
click menu item "Private Browsing" of menu 1 of menu bar item "Safari" of menu bar 1
if exists button "OK" of window 1 then
click button "OK" of window 1
end if
end tell
end tell
Thanks for getting back to me so fast.
You really cleaned up my butchered attempt to write script
but unfortunately it still does not get me running under the Private Browsing feature.
It simply starts Safari normally like nothing had changed.
Bruce24
Is your menu item really named “Private Browsing.” with an horizontal ellipsis at the end (not three dots!!)?
Then you have to change this (the US english menu item is only “Private Browsing”)
Stefank,
I also would like to do this.
I tried the script that you posted. I ran it in the script editor and got the following from the event log:
tell application “Safari”
activate
end tell
tell application “System Events”
click menu item “Private Browsing” of menu 1 of menu bar item “Safari” of menu bar 1 of process “Safari”
menu item “Private Browsing” of menu “Safari” of menu bar item “Safari” of menu bar 1 of application process “Safari”
exists button “OK” of window 1 of process “Safari”
false
end tell
This following script launches Safari 2 in Private Browsing mode.
tell application "Safari" to activate
tell application "System Events"
tell process "Safari" to click menu item "Private Browsing" of menu "Safari" of menu bar 1
keystroke return
end tell
The step that most people forget to do is not in Script Editor but in Finder Preferences. For this script to actually work, do the following:
Open System Preferences (Apple Menu)
Click Universal Access
Click “Enable access for assistive device”
Close System Prefernces
Run the Private Browsing Script
I have a question of my own, why did this script stop working when I updated to Safari 3?
Model: iMac G5
AppleScript: Script Editor 2.1.1
Browser: Safari 3.0.4
Operating System: Mac OS X (10.4)
Interestingly, the script in my previous post turns Private Browsing OFF when run whilst Safari 3 is open and Private Browsing is on. Therefore, the correct menu item is being selected. I don’t understand then why it want turn Private Browsing ON. Any suggestions to fix this script are very welcome.
On my 10.4.11 system (Safari 3), when Private Browsing is turned off (no check mark) the menu item has an ellipsis on the end: “Private Browsing.”. When it is turned on (check mark) the menu item has no ellipsis: “Private Browsing”. Try adding that ellipsis to the end of the menu item name in your script. On US keyboards/layouts you can type an ellipsis with Option Semicolon. Alternatively, you could extract it the name with AppleScript and copy and paste it back into your original script.
tell application "System Events" to name of every menu item of menu "Safari" of first menu bar of application process "Safari" whose name starts with "Private"
--> {"Private Browsing.", "Private Browsing"} -- on my system
Ahh, there are two menu items in the menu, one with and one without the ellipsis. In my testing if I tell System Events to click the one with the ellipsis, it works for both turning Private Browsing on and off (even though the displayed menu item does not have the ellipsis when the feature is active). My testing also indicated that telling System Events to click on the one without the ellipsis only worked to turn Private Browsing off (nothing happened if it was on; your next post indicates that you see the same behavior).
Since Safari shows a dialog box only when turning on Private Browsing, it makes HIG-sense that there is only an ellipsis when Private Browsing is currently off. I am not sure why only one of the menu items works for full toggling (off to on and on to off) though. Based on earlier posts in this thread it would seem that various versions/installations of Safari have shown the ellipsis and others have not. Maybe the two menu items are a hold-over from various versions that did and did not show ellipses. Or maybe it is just a coding slip.
Whatever the reason, clicking the one with the ellipsis seems to work for both turning Private Browsing on and off. Until Safari is changed yet again…
Model: iBook G4 933
AppleScript: 1.10.7
Browser: Safari 3.0.4 (523.12)
Operating System: Mac OS X (10.4)