Activate next instance of same app (Same as: Command+`)


Is there a way to switch between instances of the current app (Equivalent to CMD+` but I want to add some logic to that like conditions and such)

Thanks in advance!

By “instances of the current app” do you mean windows?

You can switch between windows, and get various windows names. It’s not very clear what you’re trying to do.

he seems to mean windows, but I’m actually interested in different instances of the same app (eg 2 Citrix Receiver environments)!

Mac OS X doesn’t really deal well with multiple instances of the same application in general, so off hand I don’t know what would work.

I've also got the same situation. In an effort to help come up with a solution, I'll provide a little more information:

About Me

I'm a self-employed consultant who works at home on several different clients' IT systems. Each of my clients maintain their own Windows Server environment and each use Citrix to deliver a user session to me to access the apps I need.

What I'm Doing

When I connect to any one client, an instance of Citrix Receiver launches on my Mac. For example: I connect to client A and get one instance of Citrix Receiver in the Dock. While I'm connected to client A, I will connect to client B, C, D, etc. Another separate instance of Citrix Receiver launches in the Dock for each client. Each instance of Citrix Receiver will have one-to-many windows (for each of the apps I'm using at that client), each window having a window title that is either the Windows folder or Windows app on the remote system. I usually have several clients connected simultaneously so I can service them throughout the day. Keep in mind, I typically use the same apps at all clients, so the window titles in Citrix Receiver aren't unique to any one client. And, it is the same Citrix Receiver app launching on my Mac, so the app name itself is replicated in Activity Monitor. The only thing that I can imagine that differs is the process_id and that's not a predictable value. How unfortunate!

What am I using KM for?

The Citrix server will not "remember" all the apps I had open from a prior session, so on every connection to the client I have to manually lay out all my windows and apps, and repeat for each client. This is very time consuming. So, I want to use KM to automatically move and resize windows, launch apps (in the Citrix Receiver session on Windows Server, not locally on my Mac), and automatically log me in. This should all be possible using keyboard inputs and mouse clicks at specific coordinates in the Citrix Receiver app. However, to make this reliable, I must manipulate the app windows to locations that will always be the same place on my screen.


The problem here is that I may have a folder in Citrix Receiver at client A called "C:\Windows" and I may have a folder of the same name at client B. If I run a KM macro that activates Citrix Receiver app, and runs an action based on a window title of that name, then KM doesn't know which instance of the Citrix Receiver app I'm referring to. I've included screenshots of what this looks like in my Dock, in Activity Monitor, and the right-click menu on each instance of Citrix Receiver (so you can see the names of the windows).

I'm new to writing macros in Keyboard Maestro so I haven't learned all the options just yet. But, I do have basic computer programming skills (variables, control flow, etc). And, this seems like an exceptional scenario so I thought I'd ask the forum first since @peternlewis you're absolutely right that Mac OS X doesn't usually have multiple instances of the same app and doesn't make it easy to deal with.

I appreciate any help others may have had with this and how I might work around it!

Screenshots w/ Captions

Activity Monitor showing all the Citrix apps.
"Citrix Viewer" is the app that presents the session windows, and there will be one for each client connection. All the other Citrix apps (Receiver Authentication, Receiver Helper, Service Record Application, ICAClientPlugin) are all helper apps and they exist only once, whether I have one or many connections to clients.

Dock in Mac OS X

Client #1's windows within Citrix Receiver
Note that I've right-clicked on the first instance of Citrix Receiver. I've redacted proprietary info. The app names and folders are the same in all clients. In this case, there are two Windows Explorer windows (the window title reflecting which folder I'm navigated to), and one Windows app ending with "Visual Developer" followed by the code filename it currently has open.

Client #2's windows within Citrix Receiver

I think I may have a scenario where I might be able to handle this, but it’s rather complex:

  1. Begin the macro by first querying for all Citrix Receiver instances and their respective pid’s. This is done using the ps command in Terminal. Store these numbers in an array (or, store the largest one if an array is not possible). You might also want to store the total number of pid’s you found (this is the number of active connections presently).
  2. Next, launch Safari to the client’s Citrix portal URL and automate the login process. The client’s URL is unique to that client’s name, so we now know which client this Citrix Receiver connection will be for. Store this value in a variable.
  3. Navigate in Citrix portal to the Citrix app I want to launch (i.e. the connection to the client). Wait for Citrix Receiver to launch and establish the connection.
  4. Once the connection is live, repeat step 1 and compare the largest pid to the pid in Step 1. If the new pid is larger, you have a new Citrix Receiver instance and live connection. If the largest pid in Step 3 is same as Step 1, you do not have a new Citrix connection (connection failed, did not authenticate, etc).
  5. Next, switch focus to the newest Citrix Receiver instance and perform the actions I want to do. Switching focus is accomplished with AppleScript using:
    tell application "System Events"
    set frontmost of the first process whose unix id is 84267 to true
    end tell
  6. Now the resize and position actions can be accomplished.

So long as the pid’s can be stored indefinitely in KM’s engine, then the known clients are identified by their Citrix Receiver pid. So long as the connection was established by KM then it will know who the client is. If KM did not establish the connection, then when querying for pid’s, the user could be prompted for the values (if that’s useful). The sort order would be pid, and then the user would have to know which connections were established in which order and label them accordingly.

If I can get this design working, then it won’t be necessary to identify the client’s by pid because I’ll be using KM to do all the connecting!

I’d welcome comments / thoughts on whether this would work. It’s complicated but I think it will do the trick.

That sounds like it would work, but it would be so much nicer if there were a way for Keyboard Maestro to address each instance of the same application individually. Here's my use-case:

macOS has a built-in vnc server and client, but it isn't optimized for connecting to multiple-screen setups. If I connect to my home computer, which has two screens, I can either display one screen or the other screen, or both in one window scaled way down. Urgh.

So instead I've written a keyboard maestro shortcut that launches two instances of Screensharing by running the following actions:

  • An 'Execute Shell Script' action that runs the command "open -n vnc://username@IP_address".
  • Then a pause
  • Select menu in 'Screen Sharing': 'Screen Sharing > View > Display 1'
  • Select menu in 'Screen Sharing': 'Screen Sharing > View > Enter Full Screen'.

After that, it repeats those same steps to open an additional instance of screen sharing and set it to View>Display 2.

Unfortunately, the "Select menu item" actions in this second set apply to the first instance of the Screen Sharing application and thus don't switch the second instance to a different display and to full screen.

Sorry, there is basically no way to differentiate the two instances of the identical application.

Maybe you could do it with process ids and AppleScript. I have no idea if AppleScript could even identify the application that way though.