Skip to content

Instantly share code, notes, and snippets.

@doublenns
Forked from edenwaith/CountOpenSafariTabs.scpt
Last active May 24, 2020 17:09
Show Gist options
  • Save doublenns/9fba6daa31a5c7a135e3971098e9389a to your computer and use it in GitHub Desktop.
Save doublenns/9fba6daa31a5c7a135e3971098e9389a to your computer and use it in GitHub Desktop.
AppleScript: Count the number of open windows and tabs in Safari
#!/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