AppleEvents and Processes

Is it possible for AppleScript to receive message from OS, when new process launches. I need only name of process. thanks

Browser: Mozilla/5.0 (000000000; 0; 000 000 00 0; 00) 000000000000000 0000000 0000 000000 0000000000
Operating System: Mac OS X (10.4)

It’s possible to write an AppleScript to ask, it’s not possible to get the system to tell you. There are several ways to do it, but your question is not specific enough to suggest one.

Everytime new process launches, i want my script to get that processes name. I could use “tell application “Finder” to set listofprocesses to name of every process”, but i want to get information, that new process has launched immedeately after it launched. thanks

Better to ask System Events:

tell application "System Events"
	set myProc to name of every process
end tell

This returns the name of every User process.
Then you would test the list myProc to see if any of them are “new”.

There are a number of ways to do this but I’m fairly certain they all involve polling your system which could be a performance hit. The following code is one way to do it using a stay open script application that checks every 5 seconds for new apps (you can adjust the polling time by changing the idle_time property at the top of the script). This also just presents a dialog but you could easily modify the script to write the data to a log file, send an email, send an SMS to your mobile, whatever. Finally, you could make this a background-only script so it wouldn’t appear in the Dock (and most users would never know it was running) but then you’d have to use another script or Activity Monitor to quit it.

property idle_time : 5 --seconds
property open_apps : {}

on run
	my check_open_apps(true)
end run

on idle
	my check_open_apps(false)
	return idle_time
end idle

on check_open_apps(at_startup)
	tell application "System Events"
		if at_startup then
			set open_apps to name of processes
			return
		else
			set frontmost_app to name of item 1 of (processes whose frontmost = true)
			set open_apps_now to name of processes
		end if
	end tell
	if frontmost_app is not in open_apps then
		activate
		display dialog "The application " & frontmost_app & " is now running." buttons {"OK"} default button 1 with icon 1 giving up after 5
		tell application "System Events" to set frontmost of process frontmost_app to true
	end if
	set open_apps to open_apps_now
end check_open_apps

Jon

Jonn8;

Your script (nice) assumes that the most recently opened app is being used, i.e. has an active window in front, not so? [unless I misunderstand what frontmost means]. Suppose I was to start an AS app that “told” another app to activate. I’d only get the AS app.

Trying to understand.

Yes, this is correct, the script I posted above only looks for the frontmost application and checks to see if that was in the list of applications the last time the system was polled. If an application is launched but not active when the script checks, there won’t be any notification (not exactly a flaw of the script but a caveat). If you want something more robust, you have to do a little more work. The following script works in basically the same way except it checks for all new apps running since the last time it checked, regardless if they are active (frontmost) or not. In this way, apps that are background-only or that are launched as daemons or other support processes to an application will also be included in the notification:

property idle_time : 5 --seconds
property open_apps : {}

on run
	my check_open_apps(true)
end run

on idle
	my check_open_apps(false)
	return idle_time
end idle

on check_open_apps(at_startup)
	tell application "System Events"
		if at_startup then
			set open_apps to name of processes
			return
		else
			set frontmost_app to name of item 1 of (processes whose frontmost = true)
			set open_apps_now to name of processes
		end if
	end tell
	set new_apps to {}
	repeat with i from 1 to (count open_apps_now)
		set this_app to item i of open_apps_now
		if this_app is not in open_apps then set end of new_apps to this_app
	end repeat
	if new_apps is not {} then
		set the_plural to {" ", " is "}
		if (count new_apps) > 1 then set the_plural to {"s ", " are "}
		tell (a reference to my text item delimiters)
			set {old_tids, contents} to {contents, ", "}
			set {new_apps, contents} to {new_apps as Unicode text, old_tids}
		end tell
		activate
		display dialog "The application" & (item 1 of the_plural) & new_apps & (item 2 of the_plural) & "now running." buttons {"OK"} default button 1 with icon 1 giving up after 5
		tell application "System Events" to set frontmost of process frontmost_app to true
	end if
	set open_apps to open_apps_now
end check_open_apps

Jon

Very neat. I particularly like that it refocuses (after the dialog is dismissed) on the newly opened app.