Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
AppleScript: Count the number of open tabs in Safari
-- CountOpenSafariTabs.scpt
-- Author: Chad Armstrong
-- Date: 13 May 2016
-- Description: Count the number of open tabs in Safari
-- To run from CLI: osascript CountOpenSafariTabs.scpt
tell application "Safari"
set winlist to every window
set totaltabcount to 0
-- Loop through each window to count the number of open tabs
repeat with win in winlist
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 " & totaltabcount & " Safari tabs open."
end tell

@edenwaith great stuff, really appreciate it, been wrestling with getting a tab count script to work correctly. Your's works really well!

sebma commented Aug 1, 2017

@edenwaith Hi,

How can I run this from Safari ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment