2

I would like to delete any notes in Notes.app that are older than two weeks. However, the following code results in an error, "Can’t get item [X] of every note.":

tell application "Notes"
    set mainFolder to folder "Notes"
    set cutoffDate to (current date) - (14 * days)
    repeat with aNote in notes
        if container of aNote = mainFolder then
            if (creation date) of aNote is less than cutoffDate then
                delete aNote
            end if
        end if
    end repeat
end tell

I've tried to move the note into the "Recently Deleted" folder, but I get a similar error that the script can't "get" the note. What am I doing wrong?

2 Answers 2

0

Your syntax is a bit awkward. If you used this, then it would likely work. That said, it is 'getting' the list of notes each cycle. This isn't inherently bad but it's not good and could modify the results.

delete item 2 of (get notes)

Some things to consider:

Try using a list to represent the notes. As you delete notes, the contents of notes changes so the results should be more predictable when you work from a static list. For example, this should also work.

set nList to notes
delete item 2 of nList

Generally, if you can do things that are unrelated to the application in question, try and do them outside of the tell block. In this case, calculating a date is unrelated so set it beforehand.

Since all of the notes to delete are within the same folder, loop through that folder rather than through every note. You can remove the related if…then statement.

To help track the deleted notes, I added 'xList' to record those being deleted. When run from Script Editor, the list will appear in the Result. If you plan on running it in some other manner, you would need to explicitly provide the results (e.g. display dialog, save item names to text file). Of course, all of the deleted notes should appear in the 'recently deleted' folder but so would any other recently deleted note which might skew your understanding of what the script deleted.

This should achieve your purpose:

set cutoffDate to (current date) - (14 * days)
tell application "Notes"
    set mainFolder to folder "Notes"
    set nList to notes of mainFolder
    set xList to {}
    repeat with aNote in nList
        if (creation date) of aNote is less than cutoffDate then
            set end of xList to name of aNote
            delete aNote
        end if
    end repeat
    xList -- list of deleted notes
end tell
0

My first try was:

tell application "Notes" to ¬
    delete item 2 of every note of account "[email protected]"

with the result:

error "Notes got an error: Can’t get item 2 of every note of account \"[email protected]\"." number -1728 

Perhaps deleting from a list by index is AppleScript's objection? A good trick (if available) is to get the id of the object and use that later to delete:

Add:

    set noteID to id of item 2 of every note of account "[email protected]"

and then later:

    delete note id noteID

More available at https://www.macosxautomation.com/applescript/notes/04.html

Just encountered this problem on High Sierra (so perhaps on more recent OSes also:)

You must log in to answer this question.

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