Apple Script notification bubble – quick reply style prompt

applescriptnotifications

I know that you can trigger a notification using Apple Script:

osascript -e 'display notification "Hello" with title "How are you feeling?"'

However, I also want to be able to prompt for certain values in the style of iMessages reply-from-notification-popup.

For example, I want to be able to do this from shell script:

ANS=$(osascript -e 'display notification "Hello" with title "How are you feeling?"' with prompt)
echo "User is feeling $ANS"

Is this possible at all?

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:

set userCanceled to false
try
    set dialogResult to display dialog ¬
        "How are you feeling?" with title ¬
        "Hello" buttons {"Close", "Reply"} ¬
        default button "Reply" cancel button ¬
        "Close" giving up after 8 ¬
        default answer ("")
on error number -128
    set userCanceled to true
end try

log "User is feeling " & text returned of dialogResult 

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:

if button returned of dialogResult is "Reply" then

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.

https://upload.vstanced.com/images/2015/12/07/ScreenShot2015-12-06at8.03.06PM.png]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.