AppleScript以Safari私人模式打开youtube

时间:2019-05-23 22:11:51

标签: applescript

如何在Safari私人模式下打开youtube?

我已经尝试过了,但是没有用:

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

我的默认浏览器是Chrome,它以chrome格式打开youtube,而不是使用Safari私有模式。

该如何解决?

3 个答案:

答案 0 :(得分:1)

以下示例 AppleScript 代码对我有用:

对于 Safari ,请使用:

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"

注意:如果愿意,可以通过删除tell 行来将两个¬ 声明中的每一个都放在一行上连续字符和随后的不可见的换行符字符。


对于 Google Chrome ,请使用:

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"

注意:如果愿意,可以通过删除tell 行来将两个¬ 声明中的每一个都放在一行上连续字符和随后的不可见的换行符字符。



注意:示例 AppleScript 代码就是这样,并且不包含任何适当的错误处理。用户有责任添加适当,需要或想要的任何错误处理。看看try中的error 声明AppleScript Language Guide 声明。另请参见Working with Errors

答案 1 :(得分:1)

对于Safari,我确实没有比@ user3439894提供的解决方案更好的解决方案。我唯一会做不同的事情就是使用此代码创建新的私有窗口(可能是“ 6之1或其他1/2”)

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

但是,您可能更喜欢针对Google Chrome浏览器的此解决方案,因为它不需要使用系统事件,也不需要Google Chrome浏览器处于活动状态或最前端。

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

答案 2 :(得分:1)

tell application "Opera"
    try
        make new window with properties {mode:"incognito"}
    end try
        set URL of active tab of front window to "https://yoururl.com"
end tell

我将 make new window with properties {mode:"incognito"} 包裹在 try 中,因为我收到错误 "Opera got an error: AppleEvent handler failed." number -10000,因为我使用的是 Opera 浏览器。

使用谷歌浏览器时不需要。