AppleScript:查找并设置 window
AppleScript : find and set window
我正在尝试创建一个小脚本来根据其 URL 查找 safari window。
这是我做的
property checkURL : "https://www.apple.com"
repeat with i in {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
try
tell application "Safari" to set theTab to (first tab of window i whose URL contains checkURL)
tell front window of application "Safari"
set current tab to theTab
end tell
on error
--
end try
end repeat
我确信我已经开始工作了,但它似乎不再工作了
return :
tell application "Safari"
get tab 1 of window 1 whose URL contains "https://www.apple.com"
--> tab 1 of window id 434
set current tab of window 1 to tab 1 of window id 434
get tab 1 of window 2 whose URL contains "https://www.apple.com"
--> error number -1719 from tab 1 of window 2 whose URL contains "https://acmp.corp.apple.com"
get tab 1 of window 3 whose URL contains "https://www.apple.com"
--> error number -1719 from window 3
get tab 1 of window 4 whose URL contains "https://www.apple.com"
--> error number -1719 from window 4
get tab 1 of window 5 whose URL contains "https://www.apple.com"
--> error number -1719 from window 5
get tab 1 of window 6 whose URL contains "https://www.apple.com"
--> error number -1719 from window 6
get tab 1 of window 7 whose URL contains "https://www.apple.com"
--> error number -1719 from window 7
get tab 1 of window 8 whose URL contains "https://www.apple.com"
--> error number -1719 from window 8
get tab 1 of window 9 whose URL contains "https://www.apple.com"
--> error number -1719 from window 9
get tab 1 of window 10 whose URL contains "https://www.apple.com"
--> error number -1719 from window 10
end tell
PS:我知道我应该将 1/10 列表替换为基于 window 数量的列表,例如
tell application "Safari"
set numberOfSafariWindows to length of (get current tab of windows)
end tell
set nlist to {}
repeat with n from 0 to numberOfSafariWindows
set nlist2 to {n + 1}
set nlist to nlist & nlist2
end repeat
我更改了代码,它似乎有效
tell application "Safari"
--Variables
set windowCount to number of windows
set docText to ""
--Repeat for Every Window
repeat with x from 1 to windowCount
set tabcount to number of tabs in window x
--Repeat for Every Tab in Current Window
repeat with y from 1 to tabcount
--Get Tab Name & URL
set tabName to name of tab y of window x
set tabURL to URL of tab y of window x
if tabURL contains "https://www.apple.com" then
tell application "Safari"
tell window x
set current tab to tab y
end tell
end tell
end if
end repeat
end repeat
end tell
避免在没有用的时候计数。
set theUrl to "https://www.apple.com"
tell application "Safari"
repeat with thisWindow in every window
repeat with thisTab in (every tab of thisWindow whose URL contains theUrl)
set current tab of thisWindow to thisTab
end repeat
end repeat
end tell
我正在尝试创建一个小脚本来根据其 URL 查找 safari window。
这是我做的
property checkURL : "https://www.apple.com"
repeat with i in {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
try
tell application "Safari" to set theTab to (first tab of window i whose URL contains checkURL)
tell front window of application "Safari"
set current tab to theTab
end tell
on error
--
end try
end repeat
我确信我已经开始工作了,但它似乎不再工作了
return :
tell application "Safari"
get tab 1 of window 1 whose URL contains "https://www.apple.com"
--> tab 1 of window id 434
set current tab of window 1 to tab 1 of window id 434
get tab 1 of window 2 whose URL contains "https://www.apple.com"
--> error number -1719 from tab 1 of window 2 whose URL contains "https://acmp.corp.apple.com"
get tab 1 of window 3 whose URL contains "https://www.apple.com"
--> error number -1719 from window 3
get tab 1 of window 4 whose URL contains "https://www.apple.com"
--> error number -1719 from window 4
get tab 1 of window 5 whose URL contains "https://www.apple.com"
--> error number -1719 from window 5
get tab 1 of window 6 whose URL contains "https://www.apple.com"
--> error number -1719 from window 6
get tab 1 of window 7 whose URL contains "https://www.apple.com"
--> error number -1719 from window 7
get tab 1 of window 8 whose URL contains "https://www.apple.com"
--> error number -1719 from window 8
get tab 1 of window 9 whose URL contains "https://www.apple.com"
--> error number -1719 from window 9
get tab 1 of window 10 whose URL contains "https://www.apple.com"
--> error number -1719 from window 10
end tell
PS:我知道我应该将 1/10 列表替换为基于 window 数量的列表,例如
tell application "Safari"
set numberOfSafariWindows to length of (get current tab of windows)
end tell
set nlist to {}
repeat with n from 0 to numberOfSafariWindows
set nlist2 to {n + 1}
set nlist to nlist & nlist2
end repeat
我更改了代码,它似乎有效
tell application "Safari"
--Variables
set windowCount to number of windows
set docText to ""
--Repeat for Every Window
repeat with x from 1 to windowCount
set tabcount to number of tabs in window x
--Repeat for Every Tab in Current Window
repeat with y from 1 to tabcount
--Get Tab Name & URL
set tabName to name of tab y of window x
set tabURL to URL of tab y of window x
if tabURL contains "https://www.apple.com" then
tell application "Safari"
tell window x
set current tab to tab y
end tell
end tell
end if
end repeat
end repeat
end tell
避免在没有用的时候计数。
set theUrl to "https://www.apple.com"
tell application "Safari"
repeat with thisWindow in every window
repeat with thisTab in (every tab of thisWindow whose URL contains theUrl)
set current tab of thisWindow to thisTab
end repeat
end repeat
end tell