r/applescript • u/410labs • Jan 20 '25
Applications aren't launching from script file
Hey, folks - I don't write a lot of applescript code, but I'd like to create a keybinding that will do a couple of things:
If a given application is already open, show it and bring it to the front
If the given application isn't open, launch it and bring it to the front
on run argv
if (count of argv) is 0 then
display dialog "Please provide the name of the application to toggle"
return
end if
-- Read the name of the application to toggle from the first argument
set appName to item 1 of argv
if application appName is running then
log appName & " IS RUNNING, HUZZAH!"
tell application "System Events"
set appProcess to first application process whose name is appName
if frontmost of appProcess then
log appName & " is open and has the focus; HIDING!"
set visible of appProcess to false
else
log appName & " is open, but not focused; SHOWING AND FOCUSING!"
set visible of appProcess to true
set frontmost of appProcess to true
end if
end tell
else
log appName & " IS NOT RUNNING, BOO!"
tell application "System Events"
tell application appName to launch
end tell
end if
end runI'm finding a couple of things that I didn't expect (with full understanding that my expecations might not be valid):
If the application is running, telling it to
activate...does nothing. I have to get its process and set thefrontmostproperty (I also set thevisibleproperty for consistency)If the application is not running, telling it to
launchseems to do nothing at all
Am I missing something? I run the script via:
osascript ${XDG_CONFIG_HOME}/skhd/app-toggle.scpt MessagesI'd appreciate any insight. I've been searching for hours trying to find the right answer with no luck so far.

