Apple Script
Can Apple Script access any mailbox other than INBOX
MacBook Pro 13", macOS 10.14
Can Apple Script access any mailbox other than INBOX
MacBook Pro 13", macOS 10.14
Use Automator instead.
Create a new Workflow and add these Actions. Set them with the necessary data.
Select the mailbox by using the Add… button in the Get Specified Mail Items.
You can add more than one.
Set the date criteria in the Filter Mail Items action as appropriate.
If you want to add additional criteria, the All button means "AND" and the Any button means "OR".
The + buttons add new criteria lines or groups.
Change the AppleScript code to:
on run {input, parameters}
tell application "Mail"
repeat with aMessage in input
delete aMessage
end repeat
end tell
end run
Then would a script command like set box to mailbox named "Newspapers" of account named "iCloud"
followed by: set maxMessages to count of messages in "Newspapers"
then loop through all the messages looking for a date and delete the message if it the test was true?
What I am trying to accomplish is to delete messages older than a certain date from a selected mailbox
If you want to understand the syntax of AppleScript for this purpose, here is some info.
Then would a script command like set box to mailbox named "Newspapers" of account named "iCloud"
followed by: set maxMessages to count of messages in "Newspapers"
then loop through all the messages looking for a date and delete the message if it the test was true?
The syntax for that would be something like:
set box to mailbox whose name is "Newspapers" of account whose name is "iCloud"
But, I'm not certain that will work and isn't necessary.
set box to mailbox "Newspapers" of account "iCloud"
You don't need to create a counter as you can
tell application "Mail"
set cutoffDate to date "Saturday, June 1, 2019 at 12:00:00 AM"
set theMessages to messages of mailbox "Newspapers" of account "iCloud"
repeat with aMessage in (theMessages whose date received < cutoffDate)
delete aMessage
end repeat
end tell
Or, shorter:
tell application "Mail"
set cutoffDate to date "Saturday, June 1, 2019 at 12:00:00 AM"
set theMessages to messages of mailbox "Newspapers" of account "iCloud" whose date received < cutoffDate
repeat with aMessage in theMessages
delete aMessage
end repeat
end tell
Tried to use the Command as you suggested -
set theMessages to messages of mailbox "Newspapers" of account "iCloud" whose date received < target
but got an error message (attached png would not upload and HEIC is not supported- shame on you Apple). If I use INBOX it works.
Error was - "Mail got an Error" Can't get mailbox "Newspapers" of account "iCloud".
Looks like only INBOX can be address with script unless I am doing something else wrong.
When I created "Newspapers" the message said "Enter name for new mailbox to be created inside “Information” in account “Pete's iCloud”." and when I created "Information" it said "Enter name for new mailbox to be created at the top level in account “Pete's iCloud”."
So is it mailbox or something else?
Thank you for your help.
That did not work got:
SCRIPT ERROR - Mail got an error: Can’t get mailbox "Information/Newspapers" of account "Pete's iCloud".
Tried adding INBOX/Information... but that failed also
Tried "Pete's iCloud/Information/Newspapers" and that failed also..
I am not willing to give up yet but I have run out of ideas for the moment.
It seems that mailbox names that we see in mail.app are only for viewing convenience but the actual "name" used by Apple Mail is hidden. There must be a way to see or extract that "real name". I have not found it yet. Anyone know how a way of getting that "real name"? Obtaining that name should allow manipulating the contents.
Trying a few different things to get at mailbox NEWPAPERS..
Using the mailbox command - mailbox "Newspapers" in "Pete's iCloud" . IT seems to like the syntax because when I save it there is not error report. The bold part of the command is highlighted but when I execute the script and I got a script error - Can’t make "Newspapers" into type integer.
Kinda new to Apple script and I do not understand that error message
Using the mailbox command - mailbox "Newspapers" in "Pete's iCloud" . IT seems to like the syntax because when I save it there is not error report. The bold part of the command is highlighted but when I execute the script and I got a script error - Can’t make "Newspapers" into type integer.
Whatever you are doing with mailbox "Newspapers" it thinks you want to enumerate them. It is expecting an integer result and you are trying to pass a list.
Still trying to test each mail in the mailbox for a date and delete the if it meets the age criteria. So far the script..looks like... The date thing works but access to "Newspapers" does not.
set interval to -15
set theDate to current date
set target to (theDate) + interval * days
tell application "Mail"
set theMessages to mailbox "Newspapers" in "Pete's iCloud" whose date received < target <<<---- this not working
repeat with aMessage in theMessages
display dialog aMessage
#delete aMessage
set oldCount to (oldCount + 1) <<<--- right just counting but when it works I will actually delete emails
end repeat
end tell
Took out the display dialog commands I sue for diagnostics
set theMessages to mailbox "Newspapers" in "Pete's iCloud" whose date received < target <<<---- this not working
First, you are trying to set the variable "theMessages" to a Mailbox, not the messages in the mailbox.
Second, in "Pete's iCloud" isn't correct. The preposition "in" is for enumerating over a list of items. E.g.
aMessage in theMessages
That gets each message that is in the list "theMessages". The correct preposition is "of"
Third, you never initialized your counter, "oldCount."
Fourth, aMessage cannot be coerced into text for the dialog box. There are parts of the message that can be coerced into text, like the subject. If you understand Object Oriented Programming, aMessage is an instance of a Message object. It has various properties which can be get or set.
Here is what you need for the correct syntax (I don't know if the mailbox path is correct):
set interval to -15
set theDate to current date
set target to (theDate) + interval * days
tell application "Mail"
set theMessages to messages of mailbox "Newspapers" of account "Pete's iCloud" whose date received < target
set oldCount to 0
repeat with aMessage in theMessages
display dialog subject of aMessage as string
#delete aMessage
set oldCount to (oldCount + 1)
end repeat
end tell
Thank you very much.. It has been years since I wrote any code (you can tell). Your script gave me the same error I have had all along... Mail got an error: Can’t get mailbox "Newspapers" of account "Pete's iCloud".
The mailbox command implies you can access any mailbox in an account so either there is a bug or I am not interpreting the use of the command correctly. From the script help ---
mailbox n, pl mailboxes : A mailbox that holds messages
elements
contains mailboxes, messages; contained by application, accounts, mailboxes.
properties
name (text) : The name of a mailbox
unread count (integer, r/o) : The number of unread messages in the mailbox
account (account, r/o)
container (mailbox, r/o)
container n [inh. mailbox] : A mailbox that contains other mailboxes.
(This seems to imply you can address any mailbox in an iCloud email account)
At the bottom of Script Editor window, click on the right-most icon which looks like a box with lines in it.
Click on Result.
Run this Script:
tell application "Mail"
get mailboxes of account "Pete's iCloud"
end tell
You should see listed all of the names and id's of the mailboxes in that account.
Those are the correct names to use.
If you use,
get name of mailboxes of account "Pete's iCloud"
It will show the names without showing the Path, i.e., "Information/Newspapers" vs. "Newspapers"
If it fails, is "Pete's iCloud" correct?
tell application "Mail"
get name of accounts
end tell
get messages of mailbox "Sent" of account "iCloud"
Are you certain "Newspapers" is a mailbox in your iCloud account?
Or, is Newspapers in a subfolder? That would need the path, such as "Subscriptions/Newspapers"
Apple Script