使用 applescript 关闭多个 Safari 窗口

发布于 2024-09-14 23:41:43 字数 362 浏览 3 评论 0原文

运行一些脚本后,我碰巧有一堆带有“无标题”窗口的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤单情人 2024-09-21 23:41:43

使用 AppleScript 过滤参考表单

tell application "Safari"
    close (every window whose name is "Untitled")
end tell

Use an AppleScript filter reference form:

tell application "Safari"
    close (every window whose name is "Untitled")
end tell
何其悲哀 2024-09-21 23:41:43

问题是,当您关闭一个窗口时,窗口的数量会发生变化并且循环会中断,因为最终您开始循环的窗口之一不再存在(因为您正在循环中间修改循环变量) )。

如果您打开事件和回复日志,您可以更清楚地看到发生的情况。

这是修复的尝试。这个循环的次数与窗口的数量一样多。如果窗口 #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”语法开头的所有窗口),但这似乎可行。

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.

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

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.

一杆小烟枪 2024-09-21 23:41:43

这对我有用:

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

This works for me:

tell application "Safari"
    close (every tab of every window whose name starts with "some_text")
end tell

or:

tell application "Safari"
    close (every tab of every window whose URL contains "some_text")
end tell
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文