-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotify.m
66 lines (60 loc) · 2.57 KB
/
notify.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
function status=notify(msg,titl,subtitl,alert)
%NOTIFY Display OS X notification message window
% STATUS = NOTIFY(MESSAGE) displays the string MESSAGE in an OS X notification
% window. MESSAGE may be an empty string. A nonzero STATUS value indicates a
% failure.
%
% NOTIFY(MESSAGE,TITLE) additionally specifies a string TITLE for the
% notification. TITLE may be an empty string.
%
% NOTIFY(MESSAGE,TITLE,SUBTITLE) additionally specifies a string SUBTITLE for
% the notification. SUBTITLE may be an empty string.
%
% NOTIFY(MESSAGE,TITLE,SUBTITLE,SOUND) additionally specifies an alert SOUND
% to play when the notification first displays. Valid sound names are in
% /System/Library/Sounds and ~/Library/Sounds and include: Basso', 'Blow',
% 'Bottle', 'Frog', 'Funk', 'Glass', 'Hero', 'Morse', 'Ping', 'Pop', 'Purr',
% 'Sosumi', 'Submarine', and 'Tink'.
%
% Note: The behavior of the notification window can be controlled by going to
% System Preferences > Notifications > Script Editior and choosing Alerts
% rather than Banners. This way the notification can be dismissed by clicking
% Close (rather than clicking on the body of the notification which launches
% Script Editor).
% See: http://apple.stackexchange.com/q/57412/112204
% http://stackoverflow.com/q/15793534/2278029
% Andrew D. Horchler, horchler @ gmail . com, Created 5-23-14
% Revision: 1.0, 4-9-16
status = 1; %#ok<NASGU>
if ~ismac
error('notify:InvalidOS','This function only supports OS X.');
end
if ~ischar(msg)
error('notify:InvalidMessage','Message must be a string.');
end
rep = @(str)strrep(regexprep(str,'["\\]','\\$0'),'''','\"');
cmd = ['osascript -e ''display notification "' rep(msg)];
if nargin > 1
if ~ischar(titl)
error('notify:InvalidTitle','Title must be a string.');
end
cmd = [cmd '" with title "' rep(titl)];
if nargin > 2
if ~ischar(subtitl)
error('notify:InvalidSubtitle','Subtitle must be a string.');
end
cmd = [cmd '" subtitle "' rep(subtitl)];
if nargin > 3
sounds = {'Basso','Blow','Bottle','Frog','Funk','Glass','Hero',...
'Morse','Ping','Pop','Purr','Sosumi','Submarine','Tink'};
if ~isempty(alert) && ~any(strcmpi(alert,sounds))
str = [sprintf('%s, ',sounds{1:end-1}) 'or ' sounds{end}];
error('notify:InvalidSound','Sound must be a string: %s.',str);
end
cmd = [cmd '" sound name "' rep(alert)];
end
end
else
cmd = [cmd '" with title "Matlab'];
end
status = unix([cmd '"''']);