How do I apply a script to any open app?

After reading [ur]this tip[/url] on, I tested this trick by putting together a little script that changes the alpha of the frontmost iCal window. What I want to know is, how do I make the script so that it applies the alpha change to the frontmost window of the active app, regardless of which app. Currently, iCal is hardcoded into the script, cuz I don’t know how to get the active app via AS. Here’s the script:

[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

Any help here?

The easiest way to get the frontmost app is to ask System Events:

tell application "System Events"
   set frontApp to first application process whose frontmost is true
   tell application frontApp to set alphaValue of front window to theValue
end tell

You should probably wrap it all in a try/end try block to catch errors with non-Cocoa applications and apps that don’t support AppleScript/window commands.