使用 applescript 关闭多个 Safari 窗口
运行一些脚本后,我碰巧有一堆带有“无标题”窗口的 Safari 窗口。
我想出了以下代码来关闭所有名称为“Unitlted”的窗口,但它不会关闭所有带有错误消息的窗口 -> “Safari 出现错误:无法获取每个窗口的第 9 项。索引无效。”我不得不运行多次才能关闭所有窗口。
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
可能出了什么问题?
After running some of my script, I happen to have a bunch of Safari window that has the "Untitled" windows.
I came up with the following code to close all the windows that have "Unitlted" as name, but it doesn't close everything with an error message -> "Safari got an error: Can’t get item 9 of every window. Invalid index." I had to run multiple times to close all the windows.
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
What might be wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 AppleScript 过滤参考表单:
Use an AppleScript filter reference form:
问题是,当您关闭一个窗口时,窗口的数量会发生变化并且循环会中断,因为最终您开始循环的窗口之一不再存在(因为您正在循环中间修改循环变量) )。
如果您打开事件和回复日志,您可以更清楚地看到发生的情况。
这是修复的尝试。这个循环的次数与窗口的数量一样多。如果窗口 #1 没有标题,则它会被关闭。如果没有,那么我们继续窗口#2,依此类推。
我的苹果脚本真的生锈了。我确信有一种更简单的方法可以做到这一点(即某种
关闭名称以“Untitled”语法开头的所有窗口),但这似乎可行。
The problem is that when you close a window, the number of windows changes and your loop breaks because eventually one of the windows that you started looping over isn't there anymore (because you're modifying the loop variable in the middle of the loop).
If you turn on the Events and Replies logs, you can see what's happening a little more clearly.
Here's a try at a fix. This loops as many times as there are windows. If window #1 is untitled, it is closed. If not, then we proceed with window #2, and on.
My applescript is really rusty. I'm certain there is a simpler way to do it (i.e., some kind of
close all windows whos name starts with "Untitled"
syntax), but this seems to work.这对我有用:
或:
This works for me:
or: