Application checks

Are there ways to ask if an app. is open, or is in the front? something like this:

if application "Safari" is open then
	tell application "Safari" to quit
end if

or:

if application "Safari" is (front application) then
	tell application "Safari" to quit
end if

or:

set frontapp to (front application)
if frontmost of application "Safari" is true then tell application "Safari" to quit

or -

set theprocess to name of every process as list
if theprocess contains "Safari" then
if frontmost of application "Safari" is true then
tell application "Safari" to quit
end if
end if

Hope it helps

Awesome, thanks

Hi. Just a couple of points you might find of interest.

If you send a command directly to an application to find out if it’s running or frontmost, or to tell it to quit, it’ll be launched (if necessary) in order to respond to that command. It’s bit like waking someone to ask them if they’re awake or to tell them to go to sleep! It’s usually better to check the current processes for this information.

Secondly, of course, the processes belong to System Events, so you must give that a mention in your script too.

-- Is Safari open?
tell application "System Events" to set SafariOpen to (application process "Safari" exists) --> true or false.
-- if (SafariOpen) then tell application "Safari" to quit

-- Is Safari the frontmost application? (If so, it's also open!)
tell application "System Events" to set SafariFrontmost to (name of (first application process whose frontmost is true) is "Safari")
-- Or:
tell application "System Events" to set SafariFrontmost to (name of (first application process where it is frontmost) is "Safari")

-- Is Safari open in the background?
tell application "System Events" to set SafariInBackground to ((application process "Safari" exists) and (frontmost of process "Safari" is false))
-- Or:
tell application "System Events"
	tell application process "Safari"
		set SafariInBackground to ((it exists) and (it is not frontmost))
	end tell
end tell

Ty also :smiley: looks very uniform, which as far as im concerned, is good :smiley: