如何在 Google Chrome 或 Microsoft Edge 上打开不存在的标签页 2?
How to open a tab 2 on Google Chrome or Microsoft Edge if it doesn't exist?
我是 AppleScript 的新手,尝试了一个 AppleScript 解决方案 :
tell application "Google Chrome" -- or "Microsoft Edge"
tell window 1
tell tab 2
set URL to "https://google.com/"
end tell
end tell
end tell
但是如果选项卡 2 不存在,它不会执行任何操作。
然后我尝试通过checking whether an URL exist in any tab解决它:
tell application "Google Chrome"
repeat with w in windows
set i to 1
repeat with t in tabs of w
if URL of t starts with "https://mail.google" then
set active tab index of w to i
set index of w to 1
return
end if
set i to i + 1
end repeat
end repeat
open location "https://mail.google.com/mail/u/0/#inbox"
end tell
但这有点矫枉过正,它打开了一个新选项卡,而不是打开一个选项卡 2。
然后我尝试了
tell application "Google Chrome" -- or "Microsoft Edge"
tell window 1
open tab 2 -- added this line
tell tab 2
set URL to "https://google.com/"
end tell
end tell
end tell
但是不管是open tab 2
还是activate tab 2
还是start tab 2
都不行。我们怎样才能让它发挥作用,一般来说,如何找出我们可以使用的词汇或动词?
open tab 2
是问题所在。如果浏览器 window 存在并且您想创建一个新的 选项卡 并设置其 URL ,然后:
tell application "Google Chrome" to ¬
if exists front window then ¬
make new tab at end of ¬
tabs of front window ¬
with properties ¬
{URL:"https://www.example.com"}
如果存在浏览器 window 并且您想更改 [=14= 的 URL ], 那么:
tell application "Google Chrome" to ¬
if exists front window then ¬
tell front window to ¬
if (exists tab 2) then ¬
tell its tab 2 to ¬
set its URL to ¬
"https://www.example.com"
备注:
- 示例 AppleScript code 适用于 Microsoft Edge也是。
- 这些示例可以扩展为正常的 块 格式,使用
if
语句 来确定是否 Google Chrome 和/或 Microsoft Edge 存在,或者已经 运行,如果 运行 是否window 存在,等等。你只需要根据你的 code 它 needs/wants.
- 看看AppleScript Language Guide
- 在 Script Editor 中查看任何 AppleScript 字典的 Library 您要为其编写 代码 的应用程序。从 Window 菜单... 库 ⇧⌘L
下面的 example AppleScript code 显示了一个编码示例Google Chrome 或 Microsoft Edge 以及它们是否 运行 然后你可以写 code 根据条件。如所写,它有利于 Google Chrome 但如果它确实存在,则 Microsoft Edge 运行其 代码。为了支持 Microsoft Edge,更改主要 if
语句 结构以适应。
tell application "System Events"
set existsGoogleChrome to exists file "/Applications/Google Chrome.app"
set existsMicrosoftEdge to exists file "/Applications/Microsoft Edge.app"
end tell
if existsGoogleChrome then
if running of application "Google Chrome" then
tell application "Google Chrome"
# Do Something
end tell
else
tell application "Google Chrome"
# Do Something Else
end tell
end if
else
if existsMicrosoftEdge then
if running of application "Microsoft Edge" then
tell application "Microsoft Edge"
# Do Something
end tell
else
tell application "Microsoft Edge"
# Do Something Else
end tell
end if
end if
end if
我是 AppleScript 的新手,尝试了一个 AppleScript 解决方案
tell application "Google Chrome" -- or "Microsoft Edge"
tell window 1
tell tab 2
set URL to "https://google.com/"
end tell
end tell
end tell
但是如果选项卡 2 不存在,它不会执行任何操作。
然后我尝试通过checking whether an URL exist in any tab解决它:
tell application "Google Chrome"
repeat with w in windows
set i to 1
repeat with t in tabs of w
if URL of t starts with "https://mail.google" then
set active tab index of w to i
set index of w to 1
return
end if
set i to i + 1
end repeat
end repeat
open location "https://mail.google.com/mail/u/0/#inbox"
end tell
但这有点矫枉过正,它打开了一个新选项卡,而不是打开一个选项卡 2。
然后我尝试了
tell application "Google Chrome" -- or "Microsoft Edge"
tell window 1
open tab 2 -- added this line
tell tab 2
set URL to "https://google.com/"
end tell
end tell
end tell
但是不管是open tab 2
还是activate tab 2
还是start tab 2
都不行。我们怎样才能让它发挥作用,一般来说,如何找出我们可以使用的词汇或动词?
open tab 2
是问题所在。如果浏览器 window 存在并且您想创建一个新的 选项卡 并设置其 URL ,然后:
tell application "Google Chrome" to ¬
if exists front window then ¬
make new tab at end of ¬
tabs of front window ¬
with properties ¬
{URL:"https://www.example.com"}
如果存在浏览器 window 并且您想更改 [=14= 的 URL ], 那么:
tell application "Google Chrome" to ¬
if exists front window then ¬
tell front window to ¬
if (exists tab 2) then ¬
tell its tab 2 to ¬
set its URL to ¬
"https://www.example.com"
备注:
- 示例 AppleScript code 适用于 Microsoft Edge也是。
- 这些示例可以扩展为正常的 块 格式,使用
if
语句 来确定是否 Google Chrome 和/或 Microsoft Edge 存在,或者已经 运行,如果 运行 是否window 存在,等等。你只需要根据你的 code 它 needs/wants. - 看看AppleScript Language Guide
- 在 Script Editor 中查看任何 AppleScript 字典的 Library 您要为其编写 代码 的应用程序。从 Window 菜单... 库 ⇧⌘L
下面的 example AppleScript code 显示了一个编码示例Google Chrome 或 Microsoft Edge 以及它们是否 运行 然后你可以写 code 根据条件。如所写,它有利于 Google Chrome 但如果它确实存在,则 Microsoft Edge 运行其 代码。为了支持 Microsoft Edge,更改主要 if
语句 结构以适应。
tell application "System Events"
set existsGoogleChrome to exists file "/Applications/Google Chrome.app"
set existsMicrosoftEdge to exists file "/Applications/Microsoft Edge.app"
end tell
if existsGoogleChrome then
if running of application "Google Chrome" then
tell application "Google Chrome"
# Do Something
end tell
else
tell application "Google Chrome"
# Do Something Else
end tell
end if
else
if existsMicrosoftEdge then
if running of application "Microsoft Edge" then
tell application "Microsoft Edge"
# Do Something
end tell
else
tell application "Microsoft Edge"
# Do Something Else
end tell
end if
end if
end if