3

So I am playing around with AppleScript and am quite new to it.

How do I make the script quit both apps if:

  • app1 and app2 are running
  • or if app1 or app2 is running
  • and run them if they are not running

My script:

if application "app" is running or "app2" is running then
        tell application "app1" to quit
        tell application "app2" to quit
else
        tell application "app1" to activate
        tell application "app2" to activate
end if
1

I think this may be what you're looking for.

set app1 to "TextEdit" -- Change as needed
set app2 to "Mail" -- Change as needed

if (application app1 is running or application app2 is running) or ¬
    (application app1 is running and application app2 is running) then
    tell application app1 to quit
    tell application app2 to quit
else
    tell application app1 to activate
    tell application app2 to activate
end if
  • I don't think you need or (application app1 is running and application app2 is running): (A OR B) OR (A AND B) is logically equivalent to A OR B. – CJK May 16 at 22:06
  • Yes I agree but not knowing the scope of any other possible code in his file, I figured I would just provide the exact solution as per his three separate bullet points in his question – wch1zpink May 16 at 22:12
1

Try this code for your first line:

if application "app" is running or application "app2" is running then

The fixed one line of code makes the rest of this script run correctly.

Your example seems to be written partially backwards. It is first asking if either of the App's is running and if either app is running then tell both of them to quit. If either app is running they both quit, so why then check to see if both apps are running? They would have already been quit by your first if block. So you would first want to check if both the Apps are running, then quit them both, and then check to see if either App is running, then quit which ever is running. But even that seems like overkill.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .