Source: https://my.remarkbox.com/2fc4f7e5-4208-11e9-9d31-040140774501
Snapshot: 2026-04-27T03:23:45Z
Generator: Remarkbox 97a1fff

This is a subthread snapshot. The living document lives at the source URI above — it may have been edited, extended, or replied-to since.

Scan for living source

Final solution. Don't know whether this is better than other solutions. I guess it is one way to do it ...

set appName to "Safari"
set alreadyLaunched to false

tell application "System Events"
    if not (exists process appName) then
        set alreadyLaunched to false
    else
        set alreadyLaunched to true
    end if
    set frontmostApp to name of the first process whose frontmost is true
end tell

if alreadyLaunched is false then
    tell application appName to launch
else if alreadyLaunched is true then
    if frontmostApp is equal to appName then
        tell application "System Events" to set visible of process appName to false
    else
        tell application appName to activate
    end if
end if

D Curtis — Jan 23, 2011 08:56 am

@Zett activate will bring the app to the front whether it is open or not. So really, all you need to do is check if the app you're looking for is frontmost. If it is, you hide it. If it isn't, you activate it. Ideally this should work but I don't care enough anymore to test it.

set appName to "Mail"
set needsActivation to false
tell application "System Events"
  if frontmost of process appName then
    set visible of process appName to false
  else
    set needsActivation to true
  end if
end tell

if needsActivation then
  tell application appName to activate
end if

Source: https://my.remarkbox.com/2fc4f7e5-4208-11e9-9d31-040140774501
Snapshot: 2026-04-27T03:23:45Z
Generator: Remarkbox 97a1fff