Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to send a notification from Matlab to the Notification Centre in OSX?

I want to send a notification when my Matlab code is finished running to the Notification Centre. I'm on Mountain Lion 10.8.3 and have Matlab 2012b. Is this possible? If so, how?

like image 943
Saaru Lindestøkke Avatar asked Apr 03 '13 16:04

Saaru Lindestøkke


3 Answers

Here's a complete function. It shows Matlab as the name and icon, and clicking on the notification will bring you to Matlab:

function notify(message)
  escaped_message = strrep(message, '"', '\"');
  [~, ~] = system(['/usr/local/bin/terminal-notifier ' ...
                   '-title Matlab ' ...
                   '-group com.mathworks.matlab ' ...
                   '-activate com.mathworks.matlab ' ...
                   '-sender com.mathworks.matlab ' ...
                   '-message "' escaped_message '"']);
end
like image 137
MattyB Avatar answered Nov 09 '22 15:11

MattyB


terminal-notifier is tool to "send User Notifications on Mac OS X 10.8 from the command-line". Matlab can send commands to the Mac OS X command line using system or !. Putting these together, you can write something like this in your m-files:

!terminal-notifier.app/Contents/MacOS/terminal-notifier -message "Testing..."

To make things easier, you will probably want to make your own function so you can call it like this:

notify("Notifications from Matlab!")
like image 41
shoelzer Avatar answered Nov 09 '22 15:11

shoelzer


To add to the solutions, here's how this can be done via Matlab's system function and the terminal command osascript which executes AppleScript from the command line. The function below takes a message argument and optional title, subtitle, and alert sound arguments. Some crude escaping of the inputs is also done. The alert sound arguments can be any of the named sounds in the Sound Effects tab of the Sound pane of System Preferences, e.g., 'Sosumi'.

function status=notify(msg,titl,subtitl,alert)
%NOTIFY  Display OS X notification message window
%   NOTIFY(MESSAGE,TITLE,SUBTITLE,SOUND)

rep = @(str)strrep(regexprep(str,'["\\]','\\$0'),'''','\"');
cmd = ['osascript -e ''display notification "' rep(msg)];
if nargin > 1 && ischar(titl)
    cmd = [cmd '" with title "' rep(titl)];
    if nargin > 2 && ischar(subtitl)
        cmd = [cmd '" subtitle "' rep(subtitl)];
        if nargin > 3 && ischar(alert) && ~isempty(alert)
            cmd = [cmd '" sound name "' rep(alert)];
        end
    end
else
    cmd = [cmd '" with title "Matlab'];
end
status = system([cmd '"''']);

You can download a more complete version of this function from my GitHub. Unlike solutions based on terminal-notifier, I don't believe that the icon for the notification menu can be changed. However, the function should work on any OS X 10.8 or 10.9 system and doesn't require anything to be installed/downloaded. This code was tested under OS X 10.9.3 and 10.11.4.

like image 2
horchler Avatar answered Nov 09 '22 16:11

horchler