用于从 Safari 获取最前面 url 并在 Google Chrome 隐身模式下打开它的 AppleScript
AppleScript for getting the frontmost url from Safari and open it in Google Chrome Incognito
我正在尝试编写一个 applescript,它将从 Safari 获取当前最前面的 url 并在 Google Chrome 隐身模式中打开相同的 url。
到目前为止,我知道如何从 Safari 获取最前面的 url,但我不确定如何在 Chrome 隐身模式下打开那个 url。
MWE
tell application "Safari" to return URL of front document
tell application "Safari" to return name of front document
参考文献:
- Applescript to get the URL from Safari
以下 示例 AppleScript code 假定 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脚本 和 系统事件 而不是 do shell script
命令 打开 incognito window 在 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
更新地址评论:
... is there a way if there is an incongito window already open, then open new url in a new tab?
示例 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
更新以解决其他评论:
以下示例 AppleScript 代码 包含必要的错误处理 以适应 Google Chrome 状态的正常可能场景,以打开 URL Safari 前面 window 的当前选项卡 Google [=144] 的隐身 window/tab =]:
示例 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 code 就是这样,没有包含任何 错误处理 不包含任何额外的 错误处理 可能是适当的。用户有责任根据需要添加任何 错误处理 。查看 try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay 命令 在适当的事件之间可能是必要的,例如delay 0.5
,适当设置延迟的值。
如果您不介意使用JavaScript,只需几行代码:
const chrome = Application('Google Chrome')
const safari_url = Application('Safari').documents[0].url()
chrome.Window({ mode: 'incognito' }).make()
chrome.windows[0].activeTab.url = safari_url
我正在尝试编写一个 applescript,它将从 Safari 获取当前最前面的 url 并在 Google Chrome 隐身模式中打开相同的 url。
到目前为止,我知道如何从 Safari 获取最前面的 url,但我不确定如何在 Chrome 隐身模式下打开那个 url。
MWE
tell application "Safari" to return URL of front document
tell application "Safari" to return name of front document
参考文献:
- Applescript to get the URL from Safari
以下 示例 AppleScript code 假定 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脚本 和 系统事件 而不是 do shell script
命令 打开 incognito window 在 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
更新地址评论:
... is there a way if there is an incongito window already open, then open new url in a new tab?
示例 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
更新以解决其他评论:
以下示例 AppleScript 代码 包含必要的错误处理 以适应 Google Chrome 状态的正常可能场景,以打开 URL Safari 前面 window 的当前选项卡 Google [=144] 的隐身 window/tab =]:
示例 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 code 就是这样,没有包含任何 错误处理 不包含任何额外的 错误处理 可能是适当的。用户有责任根据需要添加任何 错误处理 。查看 try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay 命令 在适当的事件之间可能是必要的,例如delay 0.5
,适当设置延迟的值。
如果您不介意使用JavaScript,只需几行代码:
const chrome = Application('Google Chrome')
const safari_url = Application('Safari').documents[0].url()
chrome.Window({ mode: 'incognito' }).make()
chrome.windows[0].activeTab.url = safari_url