0

I have a function where I pass in the name of an application. Within the function, one of the things I'd like to do is restore the windows of the application:

on test(applicationName)
    -- do some work
    -- restore all windows
    -- do some more work
end test

I've found references on how to restore the windows of an application by setting the miniaturized property, ala:

tell application "Maps"
   set miniaturized of windows to false
end tell

(see Un-minimizing an app with Applescript)

But this requires one to specify the name of the app at compile time - I have to hard code the name of the app into the code - I can't use "tell application applicationName" even though applicationName is a string:

on test(applicationName)
    -- do some work

    -- restore all windows
    tell application applicationName
        set miniaturized of windows to false
    end tell

    --- do some more work
end test

(see tell application - string vs. string?)

Is it possible to restore the windows of an application, when I reference the name of the application as a variable?

There must be another way to do this, but the only examples I've found to do this is the "tell application/set miniaturized of windows" approach.

Jeff
  • 169
  • 9

2 Answers2

1

You might have more success using System Events to access the attributes of application process windows to control their miniaturised states.

Unlike trying to do it via the application objects themselves, the applications in question do not need to be AppleScript-able. I believe all processes running under System Events that contain windows have a set of attributes that are accessible via AppleScript, including one called AXMiniaturized, whose value is either true or false.

Although I didn't attempt to diagnose the problem with your method, I did draft this method (albeit on MacOS 10.13), which appears to corroborate what I've said. Hopefully the script is pretty self-explanatory:

use application id "com.apple.systemevents"

to getProcessesWithMiniaturizedWindows()
    return the name of (every process whose value of ¬
        attribute "AXMinimized" of every window ¬
        contains true)
end getProcessesWithMiniaturizedWindows

to restoreAllWindowsForProcess:(procName as text)
    local procName

    set value of attribute "AXMinimized" of (every window ¬
        of the process named procName whose value of ¬
        attribute "AXMinimized" = true) to false
end restoreAllWindowsForProcess:

on run
    repeat with processName in getProcessesWithMiniaturizedWindows()
        restoreAllWindowsForProcess_(processName)
    end repeat
end run

NB. You may need to grant Assistive Access Rights for System Events in System Preferences > Security & Privacy > Privacy > Accessibility (High Sierra).

CJK
  • 5,732
  • 1
  • 8
  • 26
0

The simple and straightforward solution would be this:

on test(applicationName)
    -- do some work

    -- restore all windows
    tell application "System Events"
        tell process applicationName
            tell every window
                set value of attribute "AXMinimized" to false
            end tell
        end tell
    end tell

    --- do some more work
end test
Ted Wrigley
  • 2,921
  • 2
  • 7
  • 17