Run a script when clicking on a dock icon?

I was wondering if there is a way to run an applescipt only if an application is activated via the dock?

I’m asking this because there is a weird bug in the adobe cs3 applications when switching spaces. For example i’m on space 2 and photoshop is on space 1, when i click on the dock icon of photoshop i switch to space 1 but my menu’s and palette’s don’t appear (or sometimes 1 or 2). The remedy is to unfocus and focus again, or to hide and show the palette’s with the tab key.

So i’ve made this realy simple applescript:

delay 0.2
tell application "System Events" to keystroke tab
delay 0.2
tell application "System Events" to keystroke tab

and it’s now trigered by PreFab’s UI Actions on activation of each of the adobe programs. But the problem is that it also works when just switching to one of the programs by clicking on an active document or something like that.

So i only want the action to be ran when i click on the dock icon of one of the adobe programs, and preferably only when i switch spaces… is there a way to get this done?

This should help you. You need to save it as a “stay-open” application. Since I don’t have your version of photoshop I set it up to work with TextEdit… so you’ll have to make it work with photoshop. But what it does is every time you press TextEdit’s dock icon it displays a dialog. You’ll need to add your code in place of the display dialog code in this script.

I hope it helps.

(* Save this code as a "stay open" application, not as a script! This app will check for when you activate the application TextEdit. Upon activation a dialog box will display. *)

property app_check_delay : 2 -- this controls the amount of time between checks

global pressCheck

on run
	set pressCheck to true
end run

on idle
	tell application "System Events" to if (exists process "TextEdit") then -- only execute this code if the applcation is running
		set frontApp to my getFrontApp()
		if frontApp is "TextEdit" then
			if pressCheck is true then
				tell application "TextEdit" to display dialog "TextEdit has become active" buttons {"OK"} default button 1
				set pressCheck to false
			end if
		else
			set pressCheck to true
		end if
	else
		if pressCheck is false then set pressCheck to true
	end if
	
	return app_check_delay
end idle


(*================== SUBROUTINES ===================*)
on getFrontApp()
	set colon to ":" as Unicode text
	set dot to "." as Unicode text
	set appPath to (path to frontmost application as Unicode text)
	considering case
		if (appPath ends with colon) then
			set n to -2
		else
			set n to -1
		end if
		set astid to AppleScript's text item delimiters
		set AppleScript's text item delimiters to colon
		set appname to text item n of appPath
		if (appname contains dot) then
			set AppleScript's text item delimiters to dot
			set appname to text 1 thru text item -2 of appname
		end if
		set AppleScript's text item delimiters to astid
	end considering
	return appname
end getFrontApp