Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applescript to open youtube in safari private mode

Tags:

applescript

How can the youtube be opened in safari private mode?

I have tried this, but it is not working:

tell application "Safari" to activate
tell application "System Events"
tell process "Safari"
      click menu item "New Private Window" of menu "File" of menu bar 1
      open location "https://www.youtube.com"  -- this will open default browser
end tell
end tell

My default browser is Chrome, and it opens youtube in chrome not in safari private mode.

How to fix this?

like image 363
BhishanPoudel Avatar asked Dec 13 '22 11:12

BhishanPoudel


2 Answers

The following example AppleScript code works for me:

For Safari use:

activate application "Safari"

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

tell application "Safari" to ¬
    set URL of current tab of ¬
        front window to "https://www.youtube.com"

Note: If one prefers, each of the two tell statements can be all on a line of its own by removing the ¬ line continuation character and the invisible linefeed character that follows.


For Google Chrome use:

activate application "Google Chrome"

tell application "System Events" to ¬
    click menu item "New Incognito Window" of ¬
        menu "File" of menu bar 1 of ¬
        application process "Google Chrome"

tell application "Google Chrome" to ¬
    set URL of active tab of ¬
        front window to "https://www.youtube.com"

Note: If one prefers, each of the two tell statements can be all on a line of its own by removing the ¬ line continuation character and the invisible linefeed character that follows.



Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors.

like image 199
user3439894 Avatar answered Mar 05 '23 11:03

user3439894


I don't really have a better solution for Safari than the one offered by @user3439894. The only thing I would have done differently would be to make the new private window using this code (which is probably "6 of 1 or 1/2 a dozen of the other")

tell application "Safari" to activate
tell application "System Events" to tell its application process "Safari"
    set frontmost to true
    keystroke "n" using {shift down, command down}
end tell

However, you may prefer this solution for Google Chrome because it does not require the use of System Events and it does not require Google Chrome to be active or frontmost.

tell application "Google Chrome"
    set incognitoWindow to (make new window with properties {mode:"incognito"})
    repeat while loading of active tab of incognitoWindow
        delay 0.1
    end repeat
    set URL of active tab of incognitoWindow to "http://youtube.com"
end tell
like image 21
wch1zpink Avatar answered Mar 05 '23 09:03

wch1zpink