How to get all applications not in background and PID

This is what I have. And it works, but it gives me a list that is all over the place. And some process such as iChat have 2 pid’s and this messes up the layout. I want something that returns a nice list in the display dialog like

‘PID’ - ‘App Name’
‘PID’ - ‘App Name’
‘PID’ - ‘App Name’
‘PID’ - ‘App Name’
‘PID’ - ‘App Name’
‘PID’ - ‘App Name’

set theProcess to ""
set listProcess to ""
tell application "System Events" to set psNames to get name of every process whose background only is false
repeat with i from 1 to count of psNames
	set theProcess to (item i of psNames as text)
	set the_pid to (do shell script "ps ax | grep " & (quoted form of theProcess) & " | grep -v grep | awk '{print $1}'")
	set listProcess to the_pid & " - " & (item i of psNames as text) & "
	" & listProcess
end repeat

display dialog listProcess

I imagine that i’m pretty close to what I need… Just off a little bit.


first of all, every process has an unique PID, there aren’t processes with multiple PID.
Probably you mean iChat and some iChat helper application / daemon.

A shell call is not needed, System Events has a property unix id, which is the PID

set listProcess to {}
tell application "System Events"
	repeat with aProcess in (get processes whose background only is false)
		tell aProcess to set end of listProcess to (unix id as text) & " - " & its name
	end repeat
end tell
set {TID, text item delimiters} to {text item delimiters, return}
set listProcess to listProcess as text
set text item delimiters to TID
display dialog listProcess

Once again, you have come through for me. Thanks mate!