Scripting code

I’m using this code to force quit Word with AppleScript :

tell application “System Events”
set appname to “Microsoft Word”
set the_process to the process appname
set pid to the unix id of the_process
end tell
if pid is not “” then do shell script ("kill -9 " & pid)

Is there a way to add a script to this command (a condition) that would ensure that this script runs only if Word is open. Thank you.

The script errors when Word is not running. So the simplest way to have it not run (or at least not throw an error) when Word is not running is to put the entire thing into a try block:

try
	tell application "System Events" to set pid to the unix id of process "Microsoft Word"
	do shell script ("kill -9 " & pid)
end try

I notice this is blindingly fast, compared to quitting Word with cmd-Q.

Thank you very much Tam_X You’re making my day.
Very fast response, I’m impress.;):slight_smile:

By the way is there a way to confirm that the issue was resolved.

Even more reliable is not using Word at all unliek Tom’s examples.

tell application "System Events"
	set appname to "Microsoft Word"
	if appname is in name of every process then
		set the_process to the process appname
		set pid to the unix id of the_process
	else
		set pid to ""
	end if
end tell
if pid is not "" then do shell script ("kill -9 " & pid)

@alastor: It skips all Mac OS X handling for quitting the application the normal way. So the application won’t get a should and will terminate command and also won’t terminate by Mac OS X (in this case Cocoa’s NSApp). This means everything is lost which isn’t saved (including Word’s application settings). Therefore this way of quitting an application is recommended to use for darwin only processes.