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
Growl has an AppleScript dictionary. I didn't find any way to show a single notification in Notification Center, but they are shown in it by default you've enabled forwarding all notifications.
tell application id "com.Growl.GrowlHelperApp"
register as application "Scripts" all notifications "test" default notifications "test"
notify with name "test" title "title" description "description" application name "Scripts"
end tell
Best Answer
The appeal in the notification is obviously typically its aesthetic, but if its functionalities were what you were in need of, then you're in luck because you can use display dialog to act literally the exact same way:
The "giving up after 8" means that if nothing happens within that time, the box will go away.
You could also theoretically make it more like a notification by making the textbox only appear after the user hits "Reply". Youd do this by making a copy of this dialog without the "default answer" part, and removing the "giving up after 8" part from the original, then putting the copy before the original, and putting this if statement around the original:
Another thing you could do with the dialog box, that i'm pretty sure you cant even do with the imessage notification, is control it with the keyboard. When a dialog box with a textbox appears, you can start typing immediately, press enter to send, and press esc at any moment.
If you're set on using a notification, unfortunately the notification section of the applescript documentation totally suggests that its not possible (with applescript). According to it, you cant even return one of two values let alone let the user enter a string.
]1
If you could get 2 answers from it, though, id suggest having one button cancel button and another button that gets the text from your clipboard. Perhaps not the most elegant approach, but it would get the job done.