Runtime error on quit application - application not found


I have exhausted my ideas on this problem.

I have an application running from a mounted disk image. I want to quit this application using a script. Problem is that if the disk image is not mounted (and app is not running), I cannot avoid a run time error asking me “where is the application”.

I have tried various versions of testing for processes etc but they all throw this error. My last 'simple" attempt is as follows:

	set appname to "Billings"
	tell my application appname to quit
end try

Using this, I can at least compile the script without the disk image being mounted. But as soon as it runs with the disk image not mounted, it prompts me to “find” the application in question and the rest of the script does not run. I had hoped that the “try” wrapper would solve the problem, but no such luck.

Can anyone suggest a solution?

Many thanks

Model: MacBook Pro
Browser: Safari 535.7
Operating System: Mac OS X (10.6)

I should add that I have tried these two scripts:

set appname to "Billings"
tell my application appname to quit


set appname to "Billings"
tell application "System Events"
	set tst to exists process appname
end tell
if tst then tell my application appname to quit

The first works but throws the “find application” error when the disk image is not mounted.

The second detect whether the application is running but, for some inexplicable reason, is incapable of quitting Billings. Billings never quits when I run the second script.



i’ve some similar problems with applications which reside on other disks.
you can use instead:

do shell script "open -a '" & app &"'"

.to avoid that your applescript calls some applications unless you wish it explicitly.
note the two quotes ’ ’
which encloses the name of your application, in this case defined as variable ‘app’

another example if you want to open some documents with a certain application:

 do shell script "open -a 'TextEdit' '~/Desktop/SomePaper.txt'"

Thanks, Joy

However, I am looking to quit the application, not launch it.

Is there a shell script solution for that?


You need to refer to the app by its id, not its name. Something like:

tell application id "com.whateveritis.Billings" to quit
end tell

You can find the id by running:

id of application "Billings"

understood, but if your script launches some application (accidentally) because you used applescript to call applications instead of shell, you can prevent annoying ‘choose application’ messages.

i wrote:

however, a shell solution to quit an application isn’t such elegant as to quit it normally. Its like a force-quit, and honesty, i’m not enthusiast of it.

do shell script "killall '" & app & "'"

Thanks guys

Shane’s solution did the trick.

Problem solved, I think

killall is a bit brutal – it doesn’t test to see whether there are any files open in the app that gets killed.

In an altogether different operation, I use this handler to see if a backup disk is mounted. If the drive is running it returns the drive number (I have several), but if not, it returns 10 and I decide on the next step depending on that number. In your case, if the drive is not mounted, you don’t have to quit the application.

on driveRunning()
	set N to 10
	-- grab the whole readout for that disk with 3 partitions
		set d to do shell script "diskutil list | grep -C 5 'iMac-BKUP'"
	on error
		return N
	end try
	-- get the disk number the partion is on
	set N to last character of first paragraph of d -- the disk number
	return N
end driveRunning