How to properly (quickly) iterate over Reminders using AppleScript/osascript

applescriptcommand lineremindersterminal

What is the proper, efficient way to iterate over Reminders using AppleScript? The script below works as expected, but takes 18 seconds to iterate over 180 Reminders when run from the command line using osascript! (This same script takes only 2-3 seconds when run from within the ScriptEditor.)

# Find all Reminders whose name contains "Alumni"
set findMe to "Alumni"
set answer to "Maches: "

tell application "Reminders"
    set names to name of every reminder
end tell

repeat with name in names
    if name contains findMe then
        set answer to answer & " --- " & name
    end if
end repeat

return answer

I've run this script two ways from the command line: As a compiled script: osascript testReminders2.scrpt and as a text file: ./testReminders2.applescript (where this file begins with #! /usr/bin/osascript. Both versions take about the same amount of time.

Best Answer

I don't have enough reminders to test for speed but give this a try. You can get the 'named' reminders into a list with one step and then coerce that list into your text output with a second. This should be faster than iterating through them individually.

# Find all Reminders whose name contains "Alumni"
set findMe to "Alumni"
set answer to "Maches: "

tell application "Reminders" to ¬
    set names to name of ¬
        (every reminder whose name contains findMe)

set {TID, AppleScript's text item delimiters} to ¬
    {AppleScript's text item delimiters, " --- "}
set answer to answer & names as text
set AppleScript's text item delimiters to TID

return answer