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?
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
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!")
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With