How do I close an OS X application from the command line using a alias defined in my .bash_profile?

5

3

I found the following shell script that can be used to tell an OS X application to quit:

#!/bin/sh

echo | osascript <<EOF
tell application "$*"
  quit
end tell
EOF

I have several simple alias commands in my .bash_profile and would like to add a "quit" command there instead of using this script. I created the following, but it doesn't work:

alias quit='osascript -e "quit application \"$1\""' 

I'm sure I've munged the command. Any advice?

Michael Prescott

Posted 2010-12-15T20:50:17.773

Reputation: 3 351

Answers

7

Use a function instead:

function quit {
osascript <<EOF
  tell application "$*" to quit
EOF
}

devanjedi

Posted 2010-12-15T20:50:17.773

Reputation: 151

tell application "$*" to quit is more compact. – Daniel Beck – 2010-12-16T02:02:09.147

@DanielBeck: I've edited it to remove other redundancies as well, such as defining an alias whose only purpose is to call the function. I left the HEREDOC-style quoting as I was having problems with alternatives and grew impatient ;) – iconoclast – 2013-10-04T00:28:02.293

2

Aliases can't have parameters. Aliases do a strict text substitution, where 'parameters' would kind of end up at the end.

I'd do a function, which can have parameters.

function quit
{
    if [ $# -ne 0 ]; then
        echo "usage: quit _appname_" >&2
        return
    fi
echo | osascript <<EOF
tell application "$1"
  quit
end tell
EOF
}

Sorry, but I can't test this and verify today (no Mac), but the idea would work as a function.

Rich Homolka

Posted 2010-12-15T20:50:17.773

Reputation: 27 121

0

does it have to be an alias?

pkill Application

like, for example pkill Safari should do the same

dajo

Posted 2010-12-15T20:50:17.773

Reputation: 1

Answer is for a proper answer, not another question, kindly read how do I write a good answer and edit your answer

– yass – 2017-03-22T22:45:18.160