Reputation: 41
I am trying to write an applescript that hides all windows of all apps with a few exceptions. The following script works for the most part, but if an application has a window that is maximized, it does not get hidden.
tell application "System Events"
set showApps to {{name:"Finder"}, {name:"Chrome"}, {name:"Notion"}}
set openApps to name of (application processes whose visible is true)
repeat with x in openApps
if showApps does not contain x then
tell every window
set isFullScreen to get value of attribute "AXFullScreen"
if isFullScreen is true then
set value of attribute "AXFullScreen" to false
end if
end tell
set visible of application process x to false
end if
end repeat
end tell
Can anyone spot the error here? Is there anyway to do this?
Upvotes: 1
Views: 1988
Reputation: 1878
You tell to windows, but forgot of application process x. So, tell block correctly should be tell (every window of application process x). BUT No need checking windows state at all. Setting visible of process to false closes all its windows anyway. So, things is simpler:
set showApps to {"Finder", "Chrome", "Notion"}
tell application "System Events"
repeat with x in (get name of processes whose visible is true)
if showApps does not contain x then set visible of process x to false
end repeat
end tell
Upvotes: 1
Reputation: 1205
You need to get out of full-screen as I don't think that a 'window' is a window while in that mode. Try 'key code 53' — for esc.
That should knock you out of full-screen after which regular methods should work. By the way, most scriptable apps have a 'miniaturized' property for their windows. With Safari at least, it seems to behave poorly after yanking the app out of FS mode so you have pump the setting.
As I don't have Chrome, here is what it might look like with Safari — you could probably just substitute "Google Chrome" or "Chrome" for "Safari" and it would work with that app.
Open a page and then run the script. Once the browser activates, click the full screen button. After the delay period, it should exit FS mode and then minimize the window. You may have to play around with the last couple of lines.
tell application "Safari"
activate
delay 3
tell application "System Events"
tell application process "Safari"
key code 53 -- esc
delay 1
end tell
end tell
set miniaturized of window 1 to false
delay 0.4
set miniaturized of window 1 to true
end tell
Upvotes: 1