以下示例 AppleScript 代码假设Safari打开到一个带有URL 并且系统上存在 Google Chrome,但是,如果存在,将按照您的要求执行。
注意:不包含错误处理以确保Safari打开到具有URL的页面或者系统上是否存在 Google Chrome。那是你添加的。
tell application "Safari" to set theURL to the URL of the front document
do shell script "open -na 'Google Chrome' --args -incognito " & theURL's quoted form
代码的Safari行也可以写成:
tell application "Safari" to set theURL to the URL of the current tab of the front window
这是一个 示例,其中包含一些 错误处理,它使用 UI 脚本 和 系统事件 而不是 @ 987654328@ command 在 Google Chrome 中打开隐身窗口:
示例 AppleScript 代码:
if running of application "Safari" then
tell application "Safari"
if the URL of ¬
the current tab of ¬
the front window ¬
does not contain missing value then
set theURL to ¬
the URL of ¬
the current tab of ¬
the front window
else
return
end if
end tell
tell application "Google Chrome" to activate
delay 1
tell application "System Events" to ¬
keystroke "n" using ¬
{shift down, command down}
delay 1
tell application "Google Chrome" to ¬
set the URL of ¬
the active tab of ¬
the front window to theURL
end if
更新地址评论:
...如果已经打开了一个 incongito 窗口,有没有办法在新标签页中打开新网址?
示例 AppleScript 代码:
if running of application "Safari" then
tell application "Safari"
if the URL of ¬
the current tab of ¬
the front window ¬
does not contain missing value then
set theURL to ¬
the URL of ¬
the current tab of ¬
the front window
else
return
end if
end tell
if running of application "Google Chrome" then
tell application "Google Chrome"
if mode of front window is "incognito" then
make new tab at end of ¬
front window with properties {URL:theURL}
end if
end tell
end if
end if
更新以解决其他 cmets:
以下示例 AppleScript 代码包含必要的错误处理以适应正常可能的状态场景Google Chrome 关于在 Google 的隐身窗口/标签中打开 Safari 前窗口的当前标签的 URL铬:
示例 AppleScript 代码:
if running of application "Safari" then
tell application "Safari"
if exists front window then
if the URL of ¬
the current tab of ¬
the front window ¬
does not contain missing value then
set theURL to ¬
the URL of ¬
the current tab of ¬
the front window
else
return
end if
else
return
end if
end tell
if running of application "Google Chrome" then
tell application "Google Chrome"
set incognitoWindowIDs to ¬
the id of every window ¬
whose mode is "incognito"
if incognitoWindowIDs is {} then
activate
my openNewIncognitoWindow()
delay 1
set URL of ¬
the active tab of ¬
the front window to theURL
else
make new tab at end of ¬
window id (first item of incognitoWindowIDs) ¬
with properties {URL:theURL}
end if
end tell
else
tell application "Google Chrome"
activate
delay 1
my openNewIncognitoWindow()
delay 1
set URL of ¬
the active tab of ¬
the front window to theURL
end tell
end if
end if
on openNewIncognitoWindow()
tell application "System Events" to ¬
keystroke "n" using ¬
{shift down, command down}
end openNewIncognitoWindow
注意:示例 AppleScript 代码 就是这样,并且没有任何包含的错误处理不包含任何适当的额外错误处理。用户有责任根据需要或需要添加任何错误处理。查看AppleScript Language Guide 中的try statement 和error statement。另请参阅Working with Errors。此外,在适当的情况下,可能需要在事件之间使用delay 命令,例如delay 0.5,延迟的值设置得当。