1

How to address an active window of an active app by AppleScript when two instances of the same app are running?

The script below addresses the other app. Probably because the name and id of the app is the same and it picks just the first one from the list by name.

tell application (path to frontmost application as text) to tell front window...

I use that script to maximize the current window but it does not work for the two instances of the same app:

tell application "Finder" to set {0, 0, dtw, dth} to bounds of window of desktop
try
    tell application (path to frontmost application as text) to tell front window
        set bounds to {0, 0, dtw, dth}
    end tell
on error
    tell application "System Events" to tell (process 1 where it is frontmost)
        try
            click (button 1 of window 1 where subrole is "AXZoomButton")
        end try
    end tell
end try
  • What app is running twice? Why not name each app differently? IF you have some more details / constraints we might be able to offer something other than parsing the process list to pick the app that started first or started last... – bmike Feb 12 '16 at 20:20
  • It's a Chrome instance created as a standalone app to access Google Music as described here: apple.stackexchange.com/questions/226833/… – myneur Feb 12 '16 at 21:47
  • Cool - I've plugged the tool I've seen other people use when they need web apps to get different dock names and such. It's fluidapp.com – bmike Feb 14 '16 at 15:41
2

It's not possible to get the right instance of the frontmost application because these two instances have the same path and the same bundle identifier, the application will always be the first open instance.

You must use the frontmost process, like this:

tell application "System Events"
    tell (first process whose frontmost is true) to tell front window to if exists then
        set position to {0, 0}
        set size to {dtw, dth}
    end if
end tell
  • Great. It seems to work except for one detail: the former script maximizes the window to the full screen including the unused area over the hidden dock. The new script with the position/size keeps padding on the side of the screen where the dock is hidden. This script does not seem to accept the set bounds to {0, 0, dtw, dth}. Any idea how to improve it to use the real full screen size without the padding? – myneur Feb 15 '16 at 20:02
  • I don't have this behavior on my iMac (El Capitan), dock on the side or bottom, so I have no idea how to fix it. – jackjr300 Feb 16 '16 at 14:40
  • If you have a hidden dock, there is a 5px gap over it. It is on all OS X versions. If you don't see it, you probably have just a dark background. The gap is not there if the dock is shown. It is not a major issue, but it is just annoying to see the wild background through that gap or the windows below might be peeking through it, which is ugly. – myneur Feb 16 '16 at 20:59
  • Yes, my background is dark, I tested with the yellow background and I see the gap. So, I have no solution to this issue, except to launch Chrome in full screen, put the --start-fullscreen option at the end of the exec command in the SH script – jackjr300 Feb 17 '16 at 16:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .