Skip to content

Instantly share code, notes, and snippets.

@bhavyaw
Last active September 15, 2016 07:06
Show Gist options
  • Save bhavyaw/13df8a7c53485e74362570aaedc2c911 to your computer and use it in GitHub Desktop.
Save bhavyaw/13df8a7c53485e74362570aaedc2c911 to your computer and use it in GitHub Desktop.
set windowTitle to ""
set currentTabTitle to ""
set appID to ""
set appName to ""
set comments to ""
set visWins to totalVisibleWindows()
tell application "System Events"
set frontApp to first application process whose frontmost is true
set appName to name of frontApp
set appID to unix id of frontApp
tell frontApp
try
tell (1st window whose value of attribute "AXMain" is true)
set windowTitle to value of attribute "AXTitle"
set appName to name of frontApp
set appID to unix id of frontApp
end tell
on error
if exists ( 1st window of frontApp ) then
set firstWindow to 1st window
tell (firstWindow whose value of attribute "miniaturized" is true)
if visWins is greater than 1 then
set visible of frontApp to false
else
set appID to -1
set appName to ""
set windowTitle to ""
end if
end tell
else
-- app closed case
if visWins is greater than 1 then
set visible of frontApp to false
else
set appID to -1
set appName to ""
set windowTitle to ""
end if
end if
end try
end tell
end tell
return {comments,windowTitle,appName,appID}
on totalVisibleWindows()
set visibleWindowIds to {}
set message to ""
tell application "System Events"
set listOfProcesses to (name of every process where background only is false)
end tell
repeat with visibleProcess in listOfProcesses
try
tell application visibleProcess to set visibleWindowIds to visibleWindowIds & (id of windows whose visible is true)
on error someError
set message to message & "Some error occurred: " & someError & "; "
end try
end repeat
set visibleWindowsCount to count of visibleWindowIds
return visibleWindowsCount
end totalVisibleWindows
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment