Search code examples
macosshellapplescriptzsh

Zsh function with argument stopped working with AppleScript


I have a function in my ~/.zshrc that runs an AppleScript like so

qapp() {
    osascript -e 'quit app "$1"'
}

This function would kill an app in terminal by using it like so $ qapp Messages to quit the Messages app. I am trying to figure out why this all of a sudden stopped working as in it no longer quits the application, no error message comes up or anything. The only thing I did after testing it was I committed the file and pushed it to my remote repo. When I add an echo $1 in the function, it prints the expected argument. I have no clue how this happened or why the script wouldn't actually quit the app. If I run that command from the command line, it works. I am running macOS 11.3.1 and I have tested another function that uses AppleScript to empty the trash and it works fine.

Could this be an issue with my $PATH? I pasted it below just in case.

/usr/local/opt/ruby/bin:/Users/USERNAME/.gem/ruby/2.6.0/bin:/Users/USERNAME/.gem/ruby/2.6.0/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/share/dotnet:/opt/X11/bin:~/.dotnet/tools:/Library/Apple/usr/bin:/Library/Frameworks/Mono.framework/Versions/Current/Commands


Solution

  • The following works for me:

    qapp() {
        osascript -e "quit app \"$1\""
    }
    

    Note: In 'quit app "$1"' as in your OP, $1 does not get expanded as it's wrapped in single-quotes.