3

How would I write an AppleScript that deletes all Stickies notes which are currently open?

Or perhaps, where are the files stored?

I tried using Automator, but it doesn't work very well to locate elements on a wide screen.

0
3

The Stickies application does not contain an AppleScript dictionary, e.g. Stickies.sdef file and as such is not considered directly AppleScript scriptable using basic vanilla AppleScript, sans a possible few basic commands, i.e., activate, quit, etc.

The use of System Events and application process "Stickies", and also using UI Scripting can perform some events when trying to talk directly to application "Stickies" will not work.

Looking under macOS Catalina the Stickies application appears to store its unsaved notes in:

~/Library/Containers/com.apple.Stickies/Data/Library/Stickies

They are Rich Text Document Bundles, with the .rtfd file extension.


The following example AppleScript code will close the Stickies application and permanently delete any unsaved notes:

if running of application "Stickies" then ¬
    tell application "Stickies" to quit

set unsavedStickiesFolder to the POSIX path of ¬
    ((path to home folder) as string) & ¬
    "Library/Containers/com.apple.Stickies/Data/Library/Stickies/"

set shellCMD to {¬
    "/usr/bin/find ", ¬
    unsavedStickiesFolder, ¬
    " -type d -iname '*.rtfd'"} ¬
    as string

set unsavedStickies to ¬
    paragraphs of ¬
    (do shell script shellCMD)

repeat with thisFile in unsavedStickies
    tell application "System Events" to ¬
        delete item thisFile
end repeat

Notes:

Do not run the above example AppleScript code unless your unsaved notes are backed up, as they will be permanently deleted! You have been warned!

The example AppleScript code, shown above, was tested in Script Editor under macOS Catalina with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.

  • 1 Assumes necessary and appropriate setting in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.


Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.

1
  • you are excellent. works perfectly. – Ishvara Jun 20 at 19:59

You must log in to answer this question.

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