Running script with both osascript and as application

I’m trying to write a script that I can run both as script from command line and as application. The script may take arguments when run from command line, and thus I need to check run handler argument. I started by checking class of argv, to see if it’s application or script, but for some reason when running as application I can’t coerce class of argv into text.

Is there a simple way for detecting how the script is being run, or getting command line arguments when they exist while still allowing running as application?

When running this script from command line, I get alerts telling me that my class is ‘script’ and argv class is ‘script’. When running as application, first alert again tells me that my class is ‘script’, but second falls to error handler with error code of -1700, message “Can’t make class into type Unicode text.”

#!/usr/bin/osascript

on run argv
	
	tell application "System Events"
		activate
		display alert "my class = '" & my class & "'\n" as informational buttons {"OK"}
	end tell
	
	try
		tell application "System Events"
			activate
			display alert "argv class = '" & class of argv & "'\n" as informational buttons {"OK"}
		end tell
	on error errText number errNum
		tell application "System Events"
			activate
			display alert ¬
				"Error getting class of argv,\n" & ¬
				"errNum: " & errNum & "\n" & ¬
				"errText: \n" & errText & ¬
				"\n" as warning buttons {"OK"}
		end tell
	end try
	
	return 0
	
end run

Model: iMac
AppleScript: 2.1.2
Browser: Chrome 11.0.696.57
Operating System: Mac OS X (10.6)

on run argv
	if argv = me then
		tell application "Finder" to display dialog "I've been started from script editor"
	else if argv = current application then
		tell application "Finder" to display dialog "I'm an application"
	else
		tell application "Finder" to display dialog "OSAScript command line started me"
	end if
end run

Thanks, works great!