How to determine the last app one was in?

How does one determine the last app one was in?

Things I’ve played with that raise questions:

  1. tell application “System Events” to get name of (2nd process whose visible is true)

Executing this from an AppleScript applet always names the applet itself, no matter whether I use “2nd,” “3rd” or “10th process.” What’s up with that?

  1. tell application “System Events” to get the name of every process

Executing this gives me a list that I can’t figure out what order it’s in.
So I can’t usefully do something like this:

tell application “System Events” to get name of 1st process

  1. tell application “System Events” to set visible of first application process whose frontmost is true to false

Executing this from an AppleScript applet usually but not always will return to the original application from which the applet was launched. Why not always?

It may not be AppleScript per se: I’ve noticed that after quitting an app I don’t always return to the last app I was in. Sometimes the pattern seems to be that quitting returns me to the last Cocoa app I was in. Anybody know?

The last app one was in has got to be something a lot of people need to determine. It’s driving me up the wall! Is it something that simply cannot reliably be done?

Thanks for any help!

There was a thread about this a while back…

http://bbs.applescript.net/viewtopic.php?t=4892&start=0&postdays=0&postorder=asc&highlight=last+application

Thanks greggo!

I had seen that thread a while back and had forgotten that the list result of this command is in launch order:

tell application “System Events” to get the name of every process

But launch order doesn’t really accomplish the goal.

So far it’s increasingly looking like there really is no direct procedure to determine the last app one actually was in.
(That thread’s stay-open applet to maintain a list of frontmost apps was frought with pitfalls.)

Possibly someone else has a clever idea on how to do it. Let’s keep this going. As I said, it’s more than AppleScript; it’s general Mac OS X behavior: Simply quitting an app doesn’t take one back to the last app one was in, but somewhere seemingly unpredictable. (Perhaps to the last app in reverse launch order.)

This is as close as I can come.

http://scriptbuilders.net

[This script was automatically tagged for color coded syntax by Script to Markup Code]

It’s still not dependable because if you quit the last app it will no longer be in the list. So you would have to have a list generated of the running apps and it would need to generated at a set time interval. – A Stay Open app – Then you have to be able to get the list from the Stay Open app and compare to a current list to reveal the missing app.

First, why are you trying to do this? What are your trying to accomplish? If a stay-open script is viable, this works for me in 10.3.1:

First, save the following script as a stay-open application named “C&P Processes”:

Launch this new app from the Finder and then to reference its properties from any other script, you can use the following code:

Jon


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]