Applescript以safari私人模式打开youtube



如何在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"

注意:如果愿意,通过删除后面的¬行继续符和不可见的换行符,两个tell语句中的每一个都可以在自己的一行上。


对于谷歌浏览器使用:

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代码就是这样,不包含任何适当的错误处理。用户有责任添加任何适当、需要或想要的错误处理。请查看AppleScript 语言指南中的try语句和错误语句另请参阅处理错误

我真的没有比@user3439894提供的更好的Safari解决方案了。我唯一会做的不同的事情是使用此代码制作新的私有窗口(可能是"6 of 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

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

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
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

我用try包装了make new window with properties {mode:"incognito"},因为我使用 Opera 浏览器"Opera got an error: AppleEvent handler failed." number -10000出现错误。

使用谷歌浏览器时没有必要。

最新更新