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?