Notifications Center support in Xojo Desktop software

When we create desktop software for Mac using Xojo IDE we can display simple and advanced user notifications using AppleScript. This time we will learn how can we easily add to our Xojo application support for Notifications Center. Check out this Xojo tutorial.

AppleScript is a scripting language built into the Mac OS since System 7. It can be used to run an intelligent mechanism to control software and to access and modify data and documents. Today we will use the display notification command that can be run anywhere within a script to trigger system notifications. Below you can find the scripting dictionary listing for the command.

display notification v : Display a notification.
display notification [text] : the body text of the notification
[ with title (text) ] : the title of the notification
[ subtitle (text) ] : the subtitle of the notification
[ sound name (text) ] : the name of the sound to play

In the last tutoral, I showed you how can we use Apple Script Editor app to create a script that can be used later on in our Xojo app. Today we will try a different method. Xojo offers Shell class that executes Unix or DOS shell commands under Windows, OSX, or Linux. Using Shell we can access osascript, a command line tool that is available in OSX. Osascript executes AppleScript, but it will work with any Open Scripting Architecture (OSA) language.

Let's display a welcome notification to the user when he will run our Xojo app. Select Window1 and navigate to menu Insert -> Event Handler -> Open. If we got a part of a command line that uses eg. "Kuba" we need to add additional brackets to make it work in Xojo. To execute "Kuba" we need to execute in Xojo """Kuba""".

Dim kubadownload As Shell
kubadownload = New Shell

#If TargetMacOS Then //check if OSX system
    kubadownload.Execute("osascript -e" + "'display notification " +
"""Notification Description"""  + "with title" + """Notification title""'")

If kubadownload.ErrorCode = 0 Then
 //your code here if no error
  MsgBox("Error code: " + Str(kubadownload.ErrorCode))
 //if error display a MsbBox
End If

Now when we will run our Xojo application the Notifications Center will display our message. In our example, we will display the Title and Description, but if we won't we can also add subtitle and sound that will play when the message will display. Hope you enjoy this tutorial and if you appreciate the work I'm doing please leave a comment.