Multiple instances of App Designer windows of MATLAB can not be reached with AppleScript in MacOS

88 Views Asked by At

I run two .app files (m_app1.app and m_app2.app) prepared in "App Designer" utility of MATLAB R2022b-academic use packaged as "standalone Desktop app" using MacOS Catalina 10.15.7 with a late 2012 Macbook Pro. But, using Apple Script, I can "fully" access only to the first instance of these running .app files, that is to the one whichever is started/activated before the other. Similar attempt for the second app triggers error in AppleScript:

"System Events encountered an error: cannot retrieve window "app_b" of application process "MATLABWindow"."

My aim is being able to minimize and maximize one of these MATLAB apps, or make one of them the front window via AppleScript whenever required- while both of them are simultaneously running.

I can obtain process [id, unix id] of both and tried to access the "second" window via these PID's but it did not help either (I can get/set the attributes of the "first" window via PID's, though- that code is also given below).

Could you please suggest a solution to overcome this issue,

Thank you in advance, With kind regards, Eddie

-0-

m_app1.app's window title is "app_a". Using "Accessibility Inspector" for MAC, the following is obtained: <AXApplication: "MATLABWindow"> <AXWindow: "app_a"> Attributes: <AXTitle: "app_a">

m_app2.app's window title is "app_b". Using "Accessibility Inspector" for MAC, the following is obtained: <AXApplication: "MATLABWindow"> <AXWindow: "app_b"> Attributes: <AXTitle: "app_b">

Using Applescript:

**

tell application "System Events"    
     set windowsList to every window of (every process whose visible is true)   
     set windowSelect1 to item 1 of item 16 of aa -- app_a is item 16 among many other windows like Safari, Finder etc.     
     set windowSelect2 to item 1 of item 20 of aa -- app_b is item 20 among many other windows like Safari, Finder etc. 
 
     return windowSelect1
     --return attributes of windowSelect1
     --return windowSelect2
     --return attributes of windowSelect1 
end tell

Preserving the comment sign "--" in only "three lines including 'return' " accordingly (after 4 attempts for 4 lines including "return") the following is obtained:

returned value for "windowSelect1" is --> window "app_a" of application process "MATLABWindow" of application "System Events"

returned value for "windowSelect2" is --> window "app_b" of application process "MATLABWindow" of application "System Events"

returned value for "attributes of windowSelect1" is (a long list of attributes)

returned value for "attributes of windowSelect2" triggers an error message saying: "System Events encountered an error: cannot retrieve window "app_b" of application process "MATLABWindow"."

That is window for "app_b" is recognized but its attributes can not be reached. After a fresh restart of MACOS, if app_b.app is started first and then app_a.app is started, this time attributes of app_b is reachable while similar error as declared above arises for attributes of app_a.app.

Last remark, starting/activating app_a.app produces TWO elements in the dock (I mean at the bottom of the screen presenting list of icons for running programs). First one is named as "app_a". This one is always in minimized form, i.e., left clicking on it does not maximize/minimize any window. 2nd element is named as "MATLABWindow" which can be maximized and minimized. This second window's title is "app_a". In the same manner, activating "app_b.app" produces two more elements in the dock: window named as "app_b" (nothing to minimize or maximize) and "MATLABWindow" (a minimizable/maximizable window with title "app_b")

Using PID's, among app_a and app_b, whichever is started/activated first can be maximized (for the app which is started secondly can not be reached (i.e., setting "nn" to 2 below triggers the error message mention above-"...can not retrieve...")), and I also have kept the various comment lines for learners who would like to give a try to see the corresponding output:

tell application "System Events"
    --set aa to get every window --whose name contains "app_b"
    --set aa to get every window --whose name contains "app_a"      
    --set aa to get windows whose name contains "app_b"
    --return aa
    set aa to windows of (processes whose name contains "MATLABWindow")
    --return aa
    --return name of (item 1 of item 2 of aa)
    
    set pidList to the [id, unix id] of (processes whose name contains "MATLABWindow")
    -- set pidList to the [id] of (processes whose name contains "MATLABWindow")
    
    set nn to 1 -- can be 1 or 2  for app_a and app_b respectively, assuming "MATLAB R2022b-academic use" and "App Designer" are closed and only standalone apps (app_a.app and app_b.app) are running
    --set myPID to item nn of (item 2 of pidList) --using UNIX ID's
    --set myPID to item nn of (item 1 of pidList) --using ID's
    
    
    --set myProcess to first process whose unix id is myPID
    --set myProcess to first process whose id is myPID ---using ID's
    --return myProcess
    
    set wnn to get windows of process id (item nn of item 1 of pidList) --using ID
    --return wnn
    set wn to (item 1 of (wnn))
    --return wn
    set value of attribute "AXFocused" of wn to true
    set value of attribute "AXFullScreen" of wn to true     
end tell
0

There are 0 best solutions below