-- This does not work
--
set StubbornAppsToToggle to {"Music", "TV", "Messages"}
repeat with i from 1 to (length of StubbornAppsToToggle)
set theProgramName to item i in StubbornAppsToToggle -- debugging
tell application "System Events"
set isRunning to (exists (processes where name is theProgramName))
if isRunning then
activate application theProgramName
tell application theProgramName
set theProps to (get properties of window 1) -- debugging
tell (window 1 of process whose name is theProgramName)
if (exists miniaturized) then
set miniaturized to (not miniaturized)
else
if (exists collapsed) then
set collapsed to (not collapsed)
end if
end if
end tell
end tell -- window 1
end if --running
end tell -- system events
end repeat
You have several problems going on. One is the statement “tell (window 1 of process whose name is theProgramName)” which is inside a tell application… group. You are telling the application to use the parameter “process”, which is not a parameter of the application but of System Events. Also, not every application’s miniaturized parameter is settable, although perhaps you’ve checked the three of interest. Also, I don’t see a need to check if the process exists because if not, you wouldn’t be toggling its miniaturization.
In any event, a running application’s frontmost window’s minimized state can be toggled with a single statement:
tell application "System Events" to tell process "Music"'s window 1's attribute "AXMinimized" to set value to (not value)
I hope that is helpful.
Thank you.
(1) I have checked that the process exists earlier. I know it is before I run it.
(2) When I hard code the app’s name, your solution works, but when I try to iterate through a list of apps, I cannot get it to work. Cannot get the syntax right. It cannot find window 1.
Here is’ what I tried:
tell application "System Events"
set sa2t to {"Music", "TV", "Messages"}
repeat with i from 1 to (length of sa2t)
set theapp to item i of sa2t
tell process theapp's window 1's attribute "AXMinimized" to set value to (not value)
end repeat
end tell
Another problem: If the apps are running in different desktops, your solution doesn’t work. I posted a working solution at forum.keyboardmaestro.com that works when the apps are running in different desktops.
ikenassi - the (second) script you posted works on my computer. I even tried the three specific applications you are using. You can try introducing a very short delay into the repeat loop; it is not uncommon that with GUI scripting the script gets ahead of the interface and needs strategically placed delays to allow the interface to catch up. Your script doesn’t seem vulnerable in that way, but its worth a try.
But based on your followup post it seems as through the issues is using the script across Desktops; I don’t use Spaces so I didn’t consider that configuration. Because you have a solution using Keyboard Maestro I won’t take this any further.
Thanks for your help. The only solution that works for me is the one I posted.
I read the discussion on KM forum. It’s obvious you’re making a confusion:
You believe you are using System Events (SE) while your script addresses the application.
collapsed
does not exist in SE. It’s part of the Music.app dictionary.
Another issue is that process names are localized in the system language and they can be totally different than the displayed application name.
Finally, you have to test if there is an open window to avoid errors.
Here is how to do:
set theIDs to {"com.apple.MobileSMS", "com.apple.Music", "com.apple.TV"}
repeat with anID in theIDs
if application id anID is running then
activate application id anID
tell application "System Events"
set anApp to (application process 1 whose bundle identifier = anID)
tell anApp to if exists window 1 then set value of attribute "AXMinimized" of window 1 to false
end tell -- system events
end if --running
end repeat
Thank you, but respectively, I tried this solution verbatim, and the windows were not minimized. I had Music running on one display, and messages and TV running in another display. I even put in a delay after minimized the window. When I have a chance, I’ll try a similar solution for each window in each application, and see if that works on the theory that each app has more than a single window.
Try this. Not extensively tested, but in a quick test, it works:
set theIDs to {"com.apple.MobileSMS", "com.apple.Music", "com.apple.TV"}
repeat with anID in theIDs
if application id anID is running then
activate application id anID
tell application "System Events"
set anApp to (application process 1 whose bundle identifier = anID)
tell anApp
set windowslist to every window
repeat with thewindow in windowslist
set value of attribute "AXMinimized" of thewindow to true
end repeat
end tell
end tell -- system events
end if --running
end repeat
Did some more testing, and it really does seem to consistently work across all spaces and screens (well, I only tried 2 screens…). Thanks to all. I don’t know what the other windows are in these apps however, but since it works, I don’t plan on investigating further.
just curious, but if you want to minimize all the windows of an app. you can do it without System Events and no loop required.
example like this:
tell application "Safari"
tell every window
set miniaturized to true
end tell
end tell
tell application "TV"
tell every window
set collapsed to true
end tell
end tell
so the TV has a different property => collapsed This appears to be SwiftUI thing. updated script above to reflect the correction.