Launching an app is pretty straight forward and this is easily achieved using just LogiOptions software, however my needs were a little a more nuanced. For one thing, I wanted one button that would open AND close the app. Here's where it gets a little different: I didn't want the app to quit if it was already open but hiding. So I needed the system to check if the app was running in the background, and if it was, then I wanted it to be brought to the foreground. This would save me from unexpected results when toggling the app.
Now, no matter whether app is closed or hidden behind other windows, I will get predictable results when toggling the app. Now I have a simple work flow that looks slick. Try it out, and try changing the prompt if you want more specific results. For example, I want to revisit this and have it check if it is minimized as well.
Here is the prompt I used for ChatGPT:
"Please, write me an apple script that will open an app if it is not already opened, check if the app is the frontmost app and if it’s not, then bring it to the front. If it is upfront, and open, the script will close the app."
And here is the code it gave me:
-- Specify the app name
set appName to "YourAppName" -- Replace "YourAppName" with the actual app name
-- Check if the app is running
tell application "System Events"
set appRunning to (name of processes) contains appName
end tell
if appRunning then
tell application "System Events"
-- Check if the app is frontmost
set frontmostApp to name of first application process whose frontmost is true
end tell
if frontmostApp is appName then
-- If the app is frontmost, quit the app
tell application appName to quit
else
-- If the app is not frontmost, bring it to the front
tell application appName to activate
end if
else
-- Open the app if it is not running
tell application appName to activate
end if
I hope you find this helpful! If you have any questions about the process, leave a comment below and I'd be happy to chat about it.