harrythehead

Q: applescript to open safari in private browsing

Hello,

I've read an old locked discussion from year 2010 but the script provided there does not work. Could anyone help me please ? I'm not familiar with scripting. Thanks.

OS X El Capitan (10.11.1)

Posted on Oct 29, 2015 7:32 PM

Close

Q: applescript to open safari in private browsing

  • All replies
  • Helpful answers

  • by harrythehead,

    harrythehead harrythehead Oct 29, 2015 7:34 PM in response to harrythehead
    Level 1 (14 points)
    Mac OS X
    Oct 29, 2015 7:34 PM in response to harrythehead

    the non-working script is :

     

    tell application "Safari" to activate

    tell application "System Events" to tell process "Safari"

    if menu item "Private Browsing…" of menu 1 of menu bar item 2 of menu bar 1 exists then

    click menu item "Private Browsing…" of menu 1 of menu bar item 2 of menu bar 1

    keystroke return

    else

    click menu item "Private Browsing" of menu 1 of menu bar item 2 of menu bar 1

    end if

    end tell

  • by VikingOSX,

    VikingOSX VikingOSX Oct 29, 2015 8:11 PM in response to harrythehead
    Level 7 (20,606 points)
    Mac OS X
    Oct 29, 2015 8:11 PM in response to harrythehead

    If your goal is to just open a Private Browsing window in Safari 9, then the terminology has changed to New Private Window, and that old script is broke and pointless, when all you have to do in Safari is press shift+command+N to launch that New Private Window.

  • by VikingOSX,Solvedanswer

    VikingOSX VikingOSX Oct 30, 2015 7:34 PM in response to VikingOSX
    Level 7 (20,606 points)
    Mac OS X
    Oct 30, 2015 7:34 PM in response to VikingOSX

    This code just opens a New Private Window in Safari 9, something that the majority of us that need that functionality could do with shift+command+N.

     

    tell application "Safari"

        activate

        tell application "System Events"

            tell process "Safari"

                keystroke "N" using {shift down, command down}

            end tell

       end tell

    end tell

  • by harrythehead,

    harrythehead harrythehead Oct 30, 2015 7:36 PM in response to VikingOSX
    Level 1 (14 points)
    Mac OS X
    Oct 30, 2015 7:36 PM in response to VikingOSX

    Thank you.

  • by jacver57,

    jacver57 jacver57 Aug 1, 2016 10:41 AM in response to VikingOSX
    Level 1 (4 points)
    Aug 1, 2016 10:41 AM in response to VikingOSX

    Very good, working fine!! Thanks very much.

    By the way, do you know by chance how to open it in a small window, rather than in full screen mode?

    And do you know how I can figure out the resolution of default icons for application so I can have an icon same resolution for the new app? I didn't find it.

    Thanks.

  • by VikingOSX,

    VikingOSX VikingOSX Aug 1, 2016 11:08 AM in response to jacver57
    Level 7 (20,606 points)
    Mac OS X
    Aug 1, 2016 11:08 AM in response to jacver57

    When I run this script as written on OS X 10.11.6 with Safari 9.1.2, the new private window opens at a normal Safari window size — not full screen.

     

    If I quit Safari that has been opened full screen, and run this script again, paragraph 1 is still the result.

     

    Here is a script that creates a new private window of user configurable proportions:

     

    use scripting additions

     

    set position to {0, 0, 800, 800} as bounding rectangle

     

    tell application "Safari"

    activate

           tell application "System Events"

                tell process "Safari"

                     keystroke "N" using {shift down, command down}

                end tell

           end tell

           set bounds of front window to position

    end tell

  • by jacver57,

    jacver57 jacver57 Aug 1, 2016 4:29 PM in response to VikingOSX
    Level 1 (4 points)
    Aug 1, 2016 4:29 PM in response to VikingOSX

    Hi,

    Thanks for the quick reply but the script's behaviour is weird!

    In fact, Safari opens in full screen mode but the display part takes the size you define in the full screen, and the remaining is black, like a small window in the big one!!!

    Really Weird!!

  • by VikingOSX,

    VikingOSX VikingOSX Aug 1, 2016 4:33 PM in response to jacver57
    Level 7 (20,606 points)
    Mac OS X
    Aug 1, 2016 4:33 PM in response to jacver57

    This is intended for Safari 9.* on Mavericks 10.9.5 or later. It works fine on my system as mentioned in the previous post. I should have added:

     

    use AppleScript version "2.3" to the script which would have restricted it to Mavericks or later.

  • by jacver57,

    jacver57 jacver57 Aug 2, 2016 5:36 PM in response to VikingOSX
    Level 1 (4 points)
    Aug 2, 2016 5:36 PM in response to VikingOSX

    Yes I have latest OSX 10.11.6 (15G31) and Safari 9.1.2 (11601.7.7)

    But I have Script Editor Version 2.8.1 (183.1) and AppleScript 2.5, is it a problem?

    Thx

  • by VikingOSX,

    VikingOSX VikingOSX Aug 2, 2016 7:29 PM in response to jacver57
    Level 7 (20,606 points)
    Mac OS X
    Aug 2, 2016 7:29 PM in response to jacver57

    You have what I have. There is no problem, as this is what I tested my script against.

     

    Had I specified use AppleScript version "2.3", that means it would be the minimum allowable version for the script, and restrict its running to Mavericks 10.9.5 or later — where Safari 9.* exists.