Return to front app after activating another

I have a script that needs to activate iTunes. I’d like to launch it with a keyboard shortcut or trackpad gesture without leaving whatever app I might be running. So, I have it saved as an application bundle, and I configured Better Touch Tool to launch it. However, I can’t figure out how to have it switch back to the app I was using when I launched it.

I’ve tried

and

with, at the end:

but this doesn’t behave as I hoped. In both cases, frontapp gets set to the app bundle for the script. I’ve considered having it use the app switcher (cmd+tab) after it’s finished, but this isn’t my desired behavior if I launch it from within iTunes. One thing that would do the trick would be the “second frontmost” app.

Any suggestions? I can post my full code if it’s useful.

This is normal because you launch an application not just run a script.
An application is not necessary, BTT can also run complied scripts.
I recommend to use the bundle identifier instead of the process name which can be different from the application name


tell application "System Events" to set frontapp to (get bundle identifier of 1st process whose frontmost is true)

-- 

activate application id frontapp

Thanks Stefan, that worked. Also, running it as a script, rather than as an application, made it faster.

Of course, the runtime application environment is not needed to be loaded