Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to activate Mac OS X application with a given process ID?

I know the process id of an application in Mac OS X. How can I switch to it (using applescript, or python, or whatever)?

By "switch", I mean, put in focus.

The usual solution is to use the applescript code tell application "Foo" activate, but here the name is not useful because I have many instances of the same application running. I am however able to get the process id of the application.

How can I switch to this application programmatically?

like image 352
Olivier Verdier Avatar asked Feb 19 '10 14:02

Olivier Verdier


People also ask

How do I find the PID of an application on a Mac?

FAQ about how to kill processes on your Mac Run the command lsof -i : (make sure to insert your port number) to find out what is running on this port. Copy the Process ID (PID) from the Terminal output.

How do I run an application as another user Mac?

Type su "account-name" at the terminal. It will then ask you the password for that account and let you run commands as that user.

What is PID on Mac?

In computing, the process identifier (a.k.a. process ID or PID) is a number used by most operating system kernels—such as those of Unix, macOS and Windows—to uniquely identify an active process.

How do I see processes in Mac terminal?

Launch Terminal (Finder > Applications > Utilities). When Terminal is running, type top and hit Return. This will pull up a list of all your currently running processes. As in the Activity Monitor, this list shows your processes in decreasing order of how much of your resources they're consuming.


2 Answers

If you don't want to /can't install any additional software, there is a built-in way of looking up process IDs and apps: ps.

ps is a useful command line tool to find info on running processes. To find a particular app given the process number (which I've assigned to a variable myProcessId):

do shell script "ps -p " & myProcessId

this will return a result like this

  PID TTY           TIME CMD
66766 ??         9:17.66 /Applications/Firefox.app/Contents/MacOS/firefox-bin -psn_0_3793822

to restrict the result to just the relevant line, pipe it to grep like so

do shell script "ps -p " & myProcessId & "|grep " & myProcessId

By parsing the answer you can find the name of the app. This might be a little tricky because the result will show the actual command used for the app, not the app name (if you look at the example you'll see it is possible to find it by looking for something.app in the result).

Edit - sorry, I misunderstood the question.

You can do it with system events (turns out to be much easier than faffing around with the shell anyway):

tell application "System Events"
    set theprocs to every process whose unix id is myProcessId
    repeat with proc in theprocs
        set the frontmost of proc to true
    end repeat
end tell
like image 107
stib Avatar answered Oct 17 '22 01:10

stib


@stib's answer works, but can be simplified:

Given that by definition only one process can match - PIDs (process IDs) uniquely identify a single process - there is no need for a loop: simply directly target the first - and by definition only - element of the list of PIDs returned by filter process whose unix id is ...:

# Assumes that variable `myProcessId` contains the PID of interest.
tell application "System Events"
  set frontmost of the first process whose unix id is myProcessId to true
end tell

ehime contributed the following bash function wrapper:

# Pass the PID as the 1st (and only) argument.
activateByPid()
{
  osascript -e "
    tell application \"System Events\"
      set frontmost of the first process whose unix id is ${1} to true
    end tell
  "
}

Sample call (assumes that activateByPid was defined or sourced in the current shell):

# Activate Safari by its PID.
activateByPid $(pgrep -x 'Safari')
like image 44
mklement0 Avatar answered Oct 17 '22 01:10

mklement0