Activating previous app

Does anyone know how to activate the previous active app by using applescript. HotApp has a system action that does this and this is what I’m looking for. I can’t seem to find the original post on this forum where I saw how this was done.

Hi denns,

You probably know this, but the system doesn’t keep track of the order of fronmostedness. That’s why you need an open app to keep track of process. AppleScript is a bit slow in getting the frontmost process when it changes. The script needs to account for this.

It has been a while since I worked on this. Here’s the stay open script I came up with now:

global last_proc, front_proc

on run
tell application “System Events”
set front_proc to (name of first process whose frontmost is true)
end tell
end run
on idle
tell application “System Events”
set cur_proc to (name of first process whose frontmost is true)
end tell
if cur_proc is not front_proc then
set last_proc to front_proc
set front_proc to cur_proc
end if
return 1
end idle

on ReturnProc(cur_proc)
if cur_proc is front_proc then
set r to last_proc
else
set r to front_proc
end if
return r
end ReturnProc

It keeps track of the current process and the last process because of the slowness. You call the handlef ReturnProc(cur_proc) to get the front process because the scipt may or may not have detected a change in the frontmost app. If it did detect the change, then it returns last_proc. Otherwise it returns the front_proc because this hasn’t changed yet. You can get the last process by sending this script the current process with something like this:

tell application “Finder” to activate
–delay 2
tell application “System Events”
set cur_proc to (name of first process whose frontmost is true)
end tell
tell application “FrontProc1”
ReturnProc(cur_proc)
end tell

Process names were used because it’s faster than sending references. I named the stay open script “FrontProc1”. Note that adding the delay in the calling script give the stay open script time to get the new front process.

gl,

Hi kel,

Thanks for the reply and the explanation. It all helps.

I have found another way to do this that works Here!