With Mavericks and later, you can do this using AppleScript's 'display notification':
display notification "Lorem ipsum dolor sit amet" with title "Title"
That's it—literally that simple! No 3rd-party libraries or apps required and is completely portable for use on other systems. 10.9 notification on the top, 10.10 DP in the middle, 10.10 on the bottom.
AppleScript can be run from the shell using /usr/bin/osascript:
osascript -e 'display notification "Lorem ipsum dolor sit amet" with title "Title"'
You can also customise the alert further by adding…
a subtitle
Append 'subtitle' followed by the string or variable containing the subtitle.
display notification "message" with title "title" subtitle "subtitle"
The above example produces the following notification:
sound
Append 'sound name' followed by the name of a sound that will be played along with the notification.
display notification "message" sound name "Sound Name"
Valid sound names are the names of sounds located in…
~/Library/Sounds
/System/Library/Sounds
Posting notifications can be wrapped as a command-line script. The following code can be run in Terminal and will add a script to /usr/local/bin (must exist, add to $PATH) called notify
.
cd /usr/local/bin && echo -e "#!/bin/bash\n/usr/bin/osascript -e \"display notification \\\"\$*\\\"\"" > notify && chmod +x notify;cd -
This is the script that the above will add to notify.
#!/bin/bash
/usr/bin/osascript -e "display notification \"$*\""
Now to display a notification:
notify Lorem ipsum dolor sit amet
sleep 5; notify Slow command finished
Fixed:
This thread on the Apple forums is the key:
- Open the Library folder in your Home folder.
- In the Library folder, open the Application Support folder.
- Locate the folder named NotificationCenter. Drag this folder to the desktop.
- Next, open the Terminal application and enter the following
.
cd `getconf DARWIN_USER_DIR`
rm -rf com.apple.notificationcenter
killall usernoted; killall NotificationCenter
The issue is clearly to do with the NotificationCenter database. There are 3 files:
In my case db.wal
was a 0 byte file compared with 1.9MB on a correctly working Yosemite install. Permissions were correctly set, but looks as if Yosemite wasn't writing to it. Only deleting db.wal
didn't fix the problem - another 0 byte file was created on restart. Deleting everything works though!
Best Answer
This following AppleScript code should work if you save it as a “stay open” application. Just set the property values for
scriptToRun
andlookForThisText
and it should be good to go.Don’t forget to grant permissions in System Preferences for your new stay open application to control your computer
Here is a quick animation showing the process in action. I created an AppleScript app named Test Notification.app whose only purpose is to display a notification…
display notification "Blah" with title "BLAH BLAH" subtitle "DUH”
Then, using the code I used as the answer to this post, I created a “Stay Open Application”, whose only purpose is to monitor all incoming notification windows for the text in which I define in the variablelookForThisText
. Once the text is identified, it triggers another script "Merge Every Finder Window.scpt" which as the name says… merges every Finder window.