Runtime error on quit application - application not found

Hi

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:

try
	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

and

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.

Yours

Stumped!

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?

Cheers

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

try
tell application id "com.whateveritis.Billings" to quit
end try -- edited; was mis-typed as '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
	try
		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

I know this is a very old thread, but I just came across it the other day looking to add an application “quit” script to a shut down script that I already have running.

My question is this, I had to change the last line from “end tell” to “end try” to get it to work (my application ID is different, but that should not matter). Is that correct, or should it be “end tell” and then the next line would be “end try”?

Simple curiosity as I try to learn more about AppleScript.

No, the ‘to’ in the tell line obviates the need for an ‘end tell’. The last line should be replaced with end try so as to complete the try block. Of course, you can just delete the word ‘tell’ and when you recompile, the line will be completed automatically.

Functionally, these are the same:

tell application id "com.apple.TextEdit" to quit
tell application id "com.apple.TextEdit"
	quit
end tell

Thank you. I have copied my question, as well as your explanation, along with the two example scripts you provided. No visit to MacScripter goes by without me learning something new. Thanks again.