Search code examples
applescript

Bring application to front by ID


I want to use AppleScript to bring an app to the front. If I run the following script

tell application "System Events"
  tell process id 916
    activate
  end tell
end tell

the process doesn't come to front. Instead, only the active window of the currently front-most app loses focus, but that app stays in front.

Is it even possible to do this with a process ID rather than an application name? I have tried this on Mac OS X 10.6.8 and 10.7.5.

I am looking for a plain AppleScript solution. It should not use any shell commands or any other solution. I want to use the process ID number because I might have running multiple instances of the same application (in the same file location).


Solution

  • I have found the following solution:

    tell application "System Events"
        set myProcesses to every process whose unix id is myPocessID
        repeat with myProcess in myProcesses
            set the frontmost of myProcess to true
        end repeat
    end tell
    

    Foo's answer works too:

    tell application "System Events"
        set frontmost of every process whose unix id is myProcessID to true
    end tell