Close multiple Safari windows with applescript

After running some of my scripts, I get a bunch of Safari windows with Untitled windows.

I came up with the following code to close all windows with the name "Unitlted" as the name, but it does not close everything with the error message → "Safari got an error: I cannot get element 9 of each window. Invalid index." I had to run several times to close all 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 could be wrong?

+3
source share
3 answers

Use the AppleScript Description Template:

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

, , , , , , ( ).

, , .

. , . №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

. , (.. - close all windows whos name starts with "Untitled"), , , .

+2

:

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
0

Source: https://habr.com/ru/post/1760853/


All Articles