I'm trying to create a short alias for creating System dialogs from the Terminal. I tried to create a Bash function (since an alias didn't seem to take arguments) and add it to my .bash_profile, but it's failing me.
My latest attempt is:
dialog() {
DIALOGVAR='tell app "system events" to display dialog "'$@'"'
CMD="osascript -e 'tell app \"system events\" to activate' -e '$DIALOGVAR'"
$CMD
}
But when I execute it via the Terminal, I get
0:1: syntax error: A unknown token can’t go here. (-2740)
Even though echo'ing the CMD
variable gives me a properly formatted command:
osascript -e 'tell app "system events" to activate' -e 'tell app "system events" to display dialog "foo bar"'
Perhaps I'm doing something wrong, perhaps there's an easier way to achieve this. All I'm trying to do is to create an easy to execute command that displays dialogs.
EDIT: Alternative attempt didn't lead to anything either. This one works for arguments without spaces, but fails for multiple arguments.
sysdialog() {
osascript -e "tell app \"system events\" to activate" -e "tell app \"system events\" to display dialog \"$@\""
}
"$@"
expands to a list of several word arguments, which is not what you want. Use"$*"
instead, which expands to a single word argument. See the BashWiki articles Arguments and Quotes. @GordonDavisson, care to turn your comment into an answer?$*
trick, and also includes a run handler trick (which makes it more robust about special characters in the message) that I didn't think of."$*"
vs."$@"
), which you pointed out.