Lion: Using "run script" to start and stop applications?

I’m using Lion, and I want to create a handler in AppleScript that will start and stop specified applications, as follows:

on manage(theApp, doStart, secs)
	if doStart then
		set cmd to "activate"
		set op to "until"
	else
		set cmd to "quit"
		set op to "while"
	end if
	set scr to "tell application \"" & theApp & "\" to " & cmd
	--display dialog scr
	run script (scr)
	set scr to "tell application \"Finder\"" & return & "set theProc to a reference to process \"" & theApp & "\"" & return & "repeat " & op & " exists theProc" & return & "end repeat" & return & "delay " & secs & return & "end tell"
	--display dialog scr
	run script (scr)
end manage

My goal is to be able to do things like this within an AppleScript:

manage("ArbitraryApp", true, 2) -- Launch the named app and wait for it to fully start
manage("ArbitraryApp", false, 2) -- Quit from the named app and wait for it to fully stop

However, when I try to run either of these with any app, I get the following error on the first run script (scr) command: “An error of type -10810 has occurred.”

Can anyone see what I’m doing wrong?

Or is it the case that my desired functionality just isn’t possible under AppleScript?

Thanks in advance.

manage("BBEdit", true, 2)
manage("BBEdit", false, 2)

Runs fine for me under Lion.
I’m a little surprised Applescript is putting up with abstracting cmd and op values like that.
Perhaps it’s time for you to reboot?

It worked for me under Lion.

This is odd…

Processes have been relegated to the Finder’s Legacy suite (“Operations formerly handled by the Finder, but now automatically delegated to other applications”) ever since OS X was introduced. Try using System Events instead to handle them.

Note that some processes (famously application process “firefox-bin”) don’t have the same name as their corresponding applications.

Using Finder and System Events both didn’t work. However, I now realize that my error was due to having chosen the wrong app to test with. It turns out that my test app was one that doesn’t run under Lion. Once I chose a different app, my methodology did work after all, both with Finder and System Events.

Yes, I forgot about the fact that I need a name for the app and possibly a different name for the process. So now, here’s the latest version of my handler.

Thanks to all of you!

on manage(theApp, theProcess, doStart, secs)
	if doStart then
		set cmd to "activate"
		set op to "until"
	else
		set cmd to "quit"
		set op to "while"
	end if
	if theProcess is missing value then
		set theProcess to theApp
	end if
	set scr to "tell application \"" & theApp & "\" to " & cmd
	run script (scr)
	set scr to "tell application \"System Events\"" & return & "set theProc to a reference to process \"" & theProcess & "\"" & return & "repeat " & op & " exists theProc" & return & "end repeat" & return & "delay " & secs & return & "end tell"
	run script (scr)
end manage

manage("Firefox4", "Firefox", true, 2) -- start up Firefox4
manage("Google Chrome", missing value, true, 2) -- start up Google Chrome