1

The script below works as expected, but takes 18 seconds to iterate over 180 Reminders when run from the command line using osascript.

Worse, this same script takes only 2-3 seconds when run from within the ScriptEditor and I’d like that level of performance when the script runs itself.

# 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.

Os there a more efficient way to iterate over Reminders using AppleScript?

1
  • Heh, could you automate opening script editor to get your answer? It probably keeps a connection active what the runtime doesn’t causing a delay each iteration when the script runs…
    – bmike
    Commented Sep 13 at 13:17

1 Answer 1

0

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
5
  • Just an FYI... As a general rule and proper coding, anytime you change AppleScript's text item delimiters you should restore it to what it previously was, e.g. set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "DELIMITER"} replacing "DELIMITER" with the delimiter you want, followed by the relevant code regarding its use, and then followed by set AppleScript's text item delimiters to TID. Commented Apr 1, 2021 at 14:23
  • This is interesting: I tried this on my machine, both as a .scpt file and as a text file with a "shebang". For the most part, the times are identical to my version. However, the times vary a lot on my very lightly loaded machine. Most of the time they range between 17 and 22 seconds (today). However, a few of the runs finished in 3 seconds. This makes no sense.
    – Zack
    Commented Apr 1, 2021 at 23:45
  • @Zack, Try changing set names to name of ¬ to set names to a reference to name of ¬ and see if that makes any difference. Commented Apr 2, 2021 at 14:40
  • The overall time didn't change. But it did change where the delay is. I added "print statements" to both versions of the code. In my version (above), the "set names to name of ..." line is what takes 99% of the time. In your version, however, the "set names to a reference ..." line finished almost instantly. Instead, it is the next line to reference names that takes 99% of the script's total time.
    – Zack
    Commented Apr 3, 2021 at 21:17
  • I was curious as to what effect 'reference to' might have.
    – Mockman
    Commented Apr 5, 2021 at 22:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .