How can I make two applications activate from an AppleScript app?

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

Solution 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

Solution 2:

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.