Pass Application as a variable

I am trying to check if an application is running. This script will not work if I try and pass the application as a variable.

--CheckAppOpen
--Checks if an appllication is open
display dialog "Enter Name of app or accept default " buttons {"Cancel", "OK"} default button "OK" default answer "Microsoft Excel"
set appName to text returned of result
tell application "System Events"
	if (get name of every application process) contains appName then
		tell application "Microsoft Excel"--Works when specific not when passed as a variable
			quit
		end tell
	else
		display dialog "is not" buttons {"Cancel", "OK"} default button "OK"
	end if
end tell

If I try and use the variable “appName” the script runs but does not close the application. I guess its something simple I am missing. I have tried text, string and specifier etc.

This should work

set appName to text returned of (display dialog "Enter Name of app or accept default " buttons ¬
	{"Cancel", "OK"} default button "OK" default answer "Microsoft Excel")
tell application "System Events"
	if (get name of every application process) contains appName then
		quit my application appName
	else
		display dialog "is not" buttons {"Cancel", "OK"} default button "OK"
	end if
end tell

No need System Events, and processes. Many applications have names that differ from their process name, and this will create additional problems for you…


set appName to text returned of (display dialog "Enter Name of app or accept default " buttons {"Cancel", "OK"} default button "OK" default answer "Microsoft Excel")

if running of application appName then -- THIS
	tell application appName to quit
else
	display dialog "Application  \"" & appName & "\" is not running" buttons {"Cancel", "OK"} default button "OK"
end if

Better, using display notification:


set appName to text returned of (display dialog "Enter Name of app or accept default " buttons {"Cancel", "OK"} default button "OK" default answer "Microsoft Excel")

if running of application appName then -- THIS
	tell application appName to quit
else
	display notification "Application  \"" & appName & "\" is not running" with title "notification:"
end if

Thank you both and of course both work.

KniazidisR, I have had the application and process name problems in the past so your solution works better for me.

wch1zpink, for the life of me I do not understand why your’s works and mine does not. You have removed my tell block but you seem to be saying the same thing as mine with the addition of “my”.And as I said in my post using the actual name rather than passing it as a variable worked.

Anyway thank you again. without a doubt this is the best site for solving AppleScript issues.

Since no explanation has yet been received from wch1zpink, I will try to give an answer.

In your original script, the tell application myApp block is nested within the “System Events” tell block. That is, you tell AppleScript to look for application myApp of application “System Events”, which cannot be. All applications are children of the current applicaton, which in this case is the script editor.

The working script for post #2 uses the my specifier, which tells AppleScript to look for myApp among the children of the script editor, not among the children of “System Events”. That’s why it works.

So, your original script should work with me or my specifier:


--CheckAppOpen
--Checks if an appllication is open
display dialog "Enter Name of app or accept default " buttons {"Cancel", "OK"} default button "OK" default answer "Microsoft Excel"
set appName to text returned of result
tell application "System Events"
	if (get name of every application process) contains appName then
		tell application appName of me --or, tell my application appName
			quit
		end tell
	else
		display dialog "is not" buttons {"Cancel", "OK"} default button "OK"
	end if
end tell

Understand and again my apologies for the delay in responding to your post