OS X - build a bash function to send notifications via osascript / Applescript

Displaying Notifications Notification Center offers another opportunity for providing feedback during script execution. Use the Standard Additions scripting addition’s display notification command to show notifications, such as status updates as files are processed.

To show a notification, provide the display notification command with a string to display. Optionally, provide values for the with title, subtitle, and sound name parameters to provide additional information and an audible alert when the notification appears, as shown in Listing 24-1 and Listing 24-2.

APPLESCRIPT

Open in Script Editor

Listing 24-1AppleScript: Displaying a notification

display notification "All graphics have been converted." with title "My Graphic Processing Script" subtitle "Processing is complete." sound name "Frog"

JAVASCRIPT

Open in Script Editor

Listing 24-2JavaScript: Displaying a notification

var app = Application.currentApplication()

app.includeStandardAdditions = true

app.displayNotification("All graphics have been converted.", {
    withTitle: "My Graphic Processing Script",
    subtitle: "Processing is complete.",
    soundName: "Frog"
})

NOTE

After using a script to display a notification, the script or Script Editor (if the script is run from within Script Editor) is added to the list of notifying apps in System Preferences > Notifications. There, you can configure options, such as whether to display notifications as alerts or banners.

Clicking the Show button in an alert-style notification opens the app that displayed the notification. For a script app, the action of opening the app again triggers the run handler of the script, potentially causing the script to begin processing a second time. Keep this in mind, and add code to your script to handle this scenario, if appropriate.

From: https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/DisplayNotifications.html


As Thomas Nelson pointed out, I had made this way too complex. Dropping the

cmd=$@
local $@ && 

sequence solved the problem.

Here is the finished function

    #######################
    ##### Send_Notify #####
    #######################

    # Call this and supply the text you want to have spoken ($1) and the notification message ($2)
    # ie: Send_Notify "This is my voice" "This is the message"
    Send_notify(){
    title="FOOScript"
    say "$1" && osascript -e "display notification \"$2\" with title \"$title\""
}