Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Apple Script

Can Apple Script access any mailbox other than INBOX

MacBook Pro 13", macOS 10.14

Posted on Jun 16, 2019 1:57 AM

Reply
16 replies

Jun 16, 2019 7:53 AM in response to trainengine

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

Jun 16, 2019 7:08 AM in response to Barney-15E

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

Jun 16, 2019 8:21 AM in response to trainengine

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

Jun 16, 2019 6:41 PM in response to Barney-15E

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.

Jun 18, 2019 1:01 AM in response to trainengine

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

Jun 18, 2019 6:20 AM in response to trainengine

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.

Jun 18, 2019 6:35 AM in response to Barney-15E

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

Jun 18, 2019 7:59 AM in response to trainengine

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

Jun 18, 2019 3:36 PM in response to Barney-15E

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


mailboxn, 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)

containern [inh. mailbox] : A mailbox that contains other mailboxes.

(This seems to imply you can address any mailbox in an iCloud email account)

Jun 18, 2019 4:09 PM in response to trainengine

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

Apple Script

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.