Skip to content

Instantly share code, notes, and snippets.

@melangue
Forked from edenwaith/CountOpenSafariTabs.scpt
Last active February 8, 2021 13:45
Show Gist options
  • Save melangue/fbc805a3f0ee213382bfd1dfe6dc8910 to your computer and use it in GitHub Desktop.
Save melangue/fbc805a3f0ee213382bfd1dfe6dc8910 to your computer and use it in GitHub Desktop.
AppleScript: Count the number of open tabs in Safari and show a notification
-- 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
--
-- Modified by V on 4 February 2017 to show a system notification with the number
tell application "Safari"
--Variables
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
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
display notification "There are " & totaltabcount & " Safari tabs 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