0

I am trying to make an automator workflow that opens an app if it's not already open, or closes the app if it is open. I need a script that will run the QuickShade.app

when I tap the button to run the script in my TouchBar it will pull up a dialogue box with the option of Cancel and Toggle.

when I press toggle, a script is to run that does the following

If Quickshade.app is not running --> Open app --> end script

Else

If QuickShade.app is running --> Quit App --> End Script

The issue here is the code I am putting in is not working to check if the app is open, or not, and it won't swap the states, or it swaps the states twice and crashes, or there's a syntax error and the script doesn't run.

I have given up and hope someone might be able to help.

I apologise if this is not making sense. It makes sense in my head, but probably not on paper.

What I have tried is below

Replace 'App Name' with App path

First Attempt

on run {input, parameters}
    tell application "System Events" to set visible of process "<app name>" to not (visible of process "<app name>")
end run

no error, won't run

Attempt 2

set appName to "<app name>"
set isRunning to false

tell application "System Events"
    set isRunning to count (every process whose name is appName) > 0
end tell

if isRunning then
    tell application appName to quit
else
    tell application appName to activate
end if

tell application "System Events" to set visible of process "Script Editor" to false

Can't understand what the parameters/input is and I don't know what to put here.

Attempt 3

set appName to "<app name>"
set isRunning to false

tell application "System Events"
    set isRunning to (count (every process whose name is appName)) > 0
end tell

if isRunning then
    tell application appName to quit
else
    tell application appName to activate
end if

tell application "System Events" to set visible of process "Script Editor" to false

it can't get the application

3
  • You just need to group expressions so that they get evaluated in the proper order, for example set isRunning to (count (every process whose name is appName)) > 0. The {input, parameters} arguments are a list of input items from the previous Automator action and a record of the current action's parameters. Commented Jun 14, 2023 at 14:21
  • ...Except, don't use System Events to determine whether an application is running or not. Just retrieve the running property of an application, e.g. application "QuickShade" is running (which returns true if it is running, and false otherwise). Commented Jun 21, 2023 at 19:23
  • Your attempt 3 works fine for me. What was the exact error you got? (Also, seconding what @CJK says about using the running property. Shorter and more reliable.) Commented Jul 9, 2023 at 23:53

1 Answer 1

1

Usually, the problem is created if you try to activate the application too quickly after closing it. It is necessary to add an automated delay with a check of whether it had time to close. Try this way:

set appName to "QuickShade"

tell application appName
    if running then
        quit
        repeat while running -- this repeat loop is automated delay
            delay 0.1
        end repeat
    else
        activate
    end if
end tell

tell application "System Events" to set visible of process "Script Editor" to false

To confirm my words above, test the following code. Normally, it should throw a connection error unless an automated delay is added between the quit and activate commands.

tell application "Safari"
    quit
    activate
end tell

Following script should work stably:

tell application "Safari"
    quit
    repeat while running
        delay 0.1
    end repeat
    activate
end tell
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.