What AppleScript's Notes "count of attachments" really returns on macOS 26

2026-11-04来源 dev.toEnglish

When I add a file to an Apple Note from a script, how do I know it really got there? The obvious answer is to ask Notes how many attachments the note has. On the Mac I tested, that answer was wrong in both directions.

What I tried

I restore notes with AppleScript: create the note, then make new attachment for each file. The command returns without an error, so it is tempting to call it done. It is not a proof. A command that returns success says nothing about what ended up in the note.

So I added a read-back: after each batch, ask Notes for count of attachments of the note and compare it with the number of files I added.

What I found

On macOS 26.6 (a VM, real Notes, real restore code), the count did not match what was in the note:

  • A note with one attachment reported 2.
  • A note whose attachment was a PDF reported 0.

A check built on that number would either cry wolf on good notes or pass a broken one. I could not use it.

What I do instead

I read Notes' own database after each batch and count the attachment records there. That count agreed with what I had added in my runs.

One more case turned up on the way. An empty (0 byte) file is stored with a file size of 0 (ZFILESIZE), and a check that treats a size of 0 as "missing" will flag it. That is the right outcome for a restore tool: an empty attachment is not a restored attachment, so the tool names the file instead of adding a blank one.

What I did not test

  • I did not reproduce the case that started this (a note that held a placeholder instead of the file) with a fake Time Machine folder or a real mounted local snapshot on macOS 15.7. Both attached normally, so I have not seen the check catch that exact case.
  • On the macOS 15.7 VM the database check itself said it could not check, because the VM's Terminal had no Full Disk Access. Reading the database needs it.
  • The count of attachments numbers are from macOS 26.6. I do not know if other versions behave the same.
  • Reading another app's database is not a supported interface. It can change with any macOS update.

Takeaway

If you script Notes, do not trust the return of make new attachment, and do not trust count of attachments either. Check the result somewhere independent, and say plainly when you cannot.

Full disclosure: I make a Mac app, NoteRescue, that restores Notes from backups and uses this check after every restore. The scan is free. You do not need it to use anything above.

Written with AI assistance. The findings come from my own test notes.

原文:https://www.a8q9.cn/en/article/what-applescript-s-notes-count-of-attachments-really-returns-on-macos-26-55452e.html

相关文章