Applescript以safari私有模式打开youtube

Applescript以safari私有模式打开youtube,applescript,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"

如何在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而不是safari私有模式打开youtube

如何修复此问题?

下面的示例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"
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
语句都放在自己的一行上


对于谷歌Chrome使用:

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"
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代码仅此而已,不包含任何适当的错误处理。用户有责任根据需要添加任何适当的错误处理。请查看中的语句和语句。另请参见,.

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

但是,您可能更喜欢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
我在
try
中使用属性{mode:“incognito”}包装了
创建新窗口,因为我得到了一个错误
“Opera得到了一个错误:AppleEvent处理程序失败。”数字-10000
,因为我使用了Opera浏览器


使用Google Chrome时不需要此选项。

RE:Safari代码:1。正如当前编写的,它不会执行请求的任务,因为URL尚未设置!2.如果要使用
击键
,则无需使用
。。。将其应用程序进程“Safari”
作为系统事件来告知,就是这样做的。使用上述选项时,
将frontmost设置为true
是多余的,因为您已经激活了Safari,它将其设置为frontmost!RE:Google Chrome代码:虽然它不需要使用系统事件,也不需要Google Chrome处于活动状态或最前端,但它将Google Chrome置于最前端,所以这是一种清洗。继续…继续。。。此外,在这种情况下,您不需要重复
循环,也不需要等待窗口完成加载,下面两行
告诉应用程序“Google Chrome”
块的内部操作:
使用属性创建新窗口{mode:“incognito”}
将前窗口的活动选项卡的URL设置为“http://youtube.com“
因为
创建新窗口…
使Google Chrome最前端和new wind最前端两个。不需要像你写的那样繁琐。天哪,这个!我已经找了好几年了<代码>使用属性{mode:“incognito”}创建新窗口
并将其与一个脚本相结合,以便在Chrome未运行时打开它,这意味着我可以随时轻松打开一个incognito窗口。