-
-
Save doublenns/9fba6daa31a5c7a135e3971098e9389a to your computer and use it in GitHub Desktop.
AppleScript: Count the number of open windows and tabs in Safari
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env osascript | |
-- Original Author: Chad Armstrong | |
-- Source: https://gist.github.com/edenwaith/2213a764ccb091d6a03989f238efb63f | |
-- Description: Count the number of open windows and tabs in Safari | |
tell application "Safari" | |
--Variables | |
set windowCount to count of windows | |
set windowList to every window | |
set totalTabCount to 0 | |
-- Loop through each window to count the number of open tabs | |
repeat with win in windowList | |
try | |
set tabcount to number of tabs in win | |
set totalTabCount to totalTabCount + tabcount | |
-- log "tab count: " & tabcount & " totalTabCount: " & totalTabCount | |
on error errmsg | |
-- Often getting error message like this: | |
-- "Safari got an error: AppleEvent handler failed." | |
-- log "error message: " & errmsg | |
end try | |
end repeat | |
log "There are " & windowCount & " Safari windows open." | |
log "There are " & totalTabCount & " Safari tabs open." | |
end tell |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment