Catching "activate" events?

Hi all; I’m trying to write a kind of wrapper function around X11 apps. My general thoughts are that the app will act like this: (for consistency, the wrapper function called ScriptApp, and its target X11 application called XApp)

  1. When launched, will launch X11 (if necessary) and XApp
  2. When quit, will close XApp
  3. If the XApp quits of its own accord, the ScriptApp closes
  4. If the ScriptApp is “activated,” the XApp window comes to the front

The idea is just to make X11 apps appear in the dock with their own icons, and respond even more nicely to the Aqua interface than they already do…

Anyway, here’s my code as I have it so far:

property pid : "0"

global wrapper
global xprog

on run
	set wrapper to "~jomcmani/bin/do"
	set xprog to "/usr/X11R6/bin/xterm"
	tell application "X11"
	end tell
	set pid to "0"
	do shell script wrapper & " " & xprog
	set pid to the result
end run

on idle
	do shell script "ps -p " & pid
	if the result does not contain xprog then
		set pid to "0"
	end if
	return 1
end idle

on quit
	if pid is not "0" then
		do shell script "kill " & pid & " &"
	end if
	continue quit
end quit

(To test, make sure you have AppleX11 installed, and compile this and save it as an application that won’t quit on execute.)

The problem is with my goal #4. I don’t know how to catch an “activate” message… That is, clicking on the dock icon or alt-tabbing to the program after it is already launched. Any ideas?

Also, any general comments on what I’ve done? Ways to make it easier? Places it’s already been done before? :slight_smile:

I guess I forgot to mention; this is the “do” shell script mentioned:


source /sw/bin/
export DISPLAY=:0.0
$@ >/dev/null 2>&1 &
echo $!

Update your idle handler to ask the system (perhaps it is the finder) for the frontmost process, and check to see if it the name of your application.