使用applescript关闭多个Safari窗口
在运行了我的一些脚本之后,我碰巧有一堆Safari窗口,其中有“无标题”窗口 我想出了以下代码来关闭所有以“Unitlted”为名称的窗口,但它不会关闭所有窗口,并显示错误消息->“Safari出错:无法获取每个窗口的项目9。索引无效。”我必须运行多次才能关闭所有窗口使用applescript关闭多个Safari窗口,applescript,Applescript,在运行了我的一些脚本之后,我碰巧有一堆Safari窗口,其中有“无标题”窗口 我想出了以下代码来关闭所有以“Unitlted”为名称的窗口,但它不会关闭所有窗口,并显示错误消息->“Safari出错:无法获取每个窗口的项目9。索引无效。”我必须运行多次才能关闭所有窗口 tell application "Safari" repeat with w in windows if name of w is "Untitled" then tell w t
tell application "Safari"
repeat with w in windows
if name of w is "Untitled" then
tell w to close
end if
end repeat
end tell
问题可能是什么? < P>问题是,当你关闭一个窗口时,窗口的数量会改变,而你的循环会中断,因为最终你开始循环的一个窗口就不再存在了(因为你正在修改循环中间的循环变量)。p> 如果打开事件和回复日志,您可以更清楚地看到发生了什么 这里有一个修复的尝试。这个循环的次数和窗口的次数一样多。如果窗口#1未命名,则它将关闭。如果没有,则继续执行窗口2,然后继续
tell application "Safari"
set windowNumber to 1
repeat the number of windows times
if name of window windowNumber starts with "Untitled" then
close window windowNumber
else
set windowNumber to windowNumber + 1
end if
end repeat
end tell
我的苹果书真的生锈了。我确信有一种更简单的方法可以做到这一点(例如,某种关闭所有名称以“Untitled”
语法开头的窗口),但这似乎是可行的 使用AppleScript:
这对我很有用:
tell application "Safari"
close (every tab of every window whose name starts with "some_text")
end tell
或:
tell application "Safari"
close (every tab of every window whose name starts with "some_text")
end tell
tell application "Safari"
close (every tab of every window whose URL contains "some_text")
end tell