AppleScript input text from user

applescriptautomatorremindersservices

tell application "Reminders"
    set newReminder to make new reminder
    set name of newReminder to "Text"
    set due date of newReminder to current date
end tell

How can I set the name of newReminder to input text?

Best Answer

Here's an AppleScript code example:

set newReminderName to ""
repeat until newReminderName is not ""
    set newReminderName to text returned of (display dialog "Enter a name for the reminder:" buttons {"Cancel", "OK"} default button 2 default answer "" with icon 1)
    if newReminderName is not "" then
        tell application "Reminders"
            set newReminder to make new reminder
            set name of newReminder to newReminderName
            set due date of newReminder to current date
        end tell
    end if
end repeat

This is coded so that you either have to enter a name for the reminder or cancel the prompt. In other words, if you don't type a name and click OK, you're prompted again and again until you do, or click Cancel.


Updated to address a point mentioned in comment:

The code below is used in an Automator service where the selected text passed to the Automator service becomes the name of the new reminder. It is coded not to create the reminder if an empty sting is passed. Although I don't think it can be triggered unless some text is selected, nonetheless it's always best to include some form of error checking.

on run {input}
    set newReminderName to input
    if newReminderName is not "" then
        tell application "Reminders"
            set newReminder to make new reminder
            set name of newReminder to newReminderName
            set due date of newReminder to current date
        end tell
    end if
end run

enter image description here