Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

torchy

macrumors newbie
Original poster
Mar 22, 2014
19
4
London, UK
Hi, hoping someone can help me. I am quite new to Applescript but was hoping that what I am trying to do would be simple. It appears it isn't.

I am using Applemail (on 10.9.3) and have three accounts. One is iCloud, one is Gmail and one is Exchange. The Exchange one is called "Work"

I have a mailbox within "Work" called "Prospects" and then within that a series of mailboxes for each client/prospect where I file messages.

I want an Applescript that will create one of these client/prospect sub-folders. Here's what I have:

Code:
tell application "Mail"
          set theMailbox to mailbox "Prospects" of account "Work"
  make new mailbox at theMailbox with properties {name:"Foo inside Bar"}
end tell

It completes the 'set' part okay. However the 'make' command produces the following:

Code:
Result:
error "Mail got an error: AppleEvent handler failed." number -10000

I have tried various different iterations of this all wuth the same result and have no idea where I am going wrong.

Not sure if it's relevant but my Library (my entire home directory) is on a separate disk to the OS itself. Running 10.9.3.

Any help would be greatly appreciated.

David
 
Last edited by a moderator:
Hi,

It's not particularly intuitive, but I'd do it like this:

Code:
tell application "Mail"
	tell account "Work"
		make new mailbox with properties {name:"Prospects/Foo/Bar"}
	end tell
end tell

Hope that helps
 
It gets me a lot closer, but not quite there.

"Prospects" is already a first level sub-folder of "Work". If I use your code to try and create a sub-folder within Prospects called "Big Prospect" then it creates a mailbox within Prospects called "Prospects/Big Prospect". That's when I use
Code:
make new mailbox with properties {name:"Prospects/Big Prospect"}

If I use:
Code:
make new mailbox with properties {name:"Big Prospect"}

then I just get the folder Big Prospect but it's not created as a sub-folder to Prospects, it sits at the same level.

It's as if I need to give it an path to navigate?
 
Hi,

Yup. Your path analogy is right. Is this what you mean?

Code:
tell application "Mail"
	tell account "Work"
		make new mailbox with properties {name:"Prospects/Big Prospect"}
	end tell
end tell
 
If I use the code from your last message I get the new mailbox within Prospects" but that new mailbox is called "Prospects/Big Prospect". What I want is a way to just get "Big Prospect" as a new mailbox within the existing one called "Prospects".

The Applescript result from your code is this:

Code:
mailbox "Prospects/Prospects/Big Prospect" of account "Work" of application "Mail"
 
What happens with

Code:
make new mailbox with properties {name:"[COLOR="Red"][B]/[/B][/COLOR]Big Prospect"}
 
Odd, in the e-mail about your post the line read, {name:"*/*Big Prospect"} yet in the thread it shows as {name:"/Big Prospect"}. Neither work!

The first gives this as the result:

A folder called * at root of the mailbox, with a folder inside it called */*Big Prospect.

The second gives this:

A folder called /Big Prospect, and again at the root level of the folder.

Let's assume for one moment that this is a bug in Applescript. We can get a wrongly named folder created in the correct hierarchy. Is there and Applescript way to then rename that folder?
 
Try this :

Code:
tell application "Mail"
	tell account "Work"
		set newMailbox to make new mailbox with properties {name:"Prospects/Big Prospect"}
set newMailbox's name to "Big Prospect"
	end tell
end tell

I have a mailbox within "Work" called "Prospects" and then within that a series of mailboxes for each client/prospect where I file messages.

Can you also post the result you get from :

Code:
get name of every mailbox of account "Work"
 
Last edited:
Well your code creates the mailbox at the right place in the hierarchy (i.e. as a sub-folder of "Work/Prospects") but names it as "Prospects/Big Prospect". The rename seems to fail. The error message is:

Code:
error "Mail got an error: Can’t set mailbox \"Prospects/Prospects/Big Prospect\" of account \”Work\” to \"Big Prospect\"." number -10006 from mailbox "Prospects/Prospects/Big Prospect" of account “Work”

The result of:

Code:
get name of every mailbox of account "Work"

is a list of all mailboxes within "Work", including the new Prospects/Big Prospect. I haven't provided the listing here because it contains real names of businesses and there are confidentiality issues, but the content did seem to be complete with no errors or omissions.

Thanks for everyone's continued perseverance with this!

----------

Sorry, incomplete post in the previous note, the error is:

error "Mail got an error: Can’t set mailbox \"Prospects/Prospects/Big Prospect\" of account \”Work\” to \"Big Prospect\"." number -10006 from mailbox "Prospects/Prospects/Big Prospect" of account “Work”

(Doesn't seem to display when I wrap in the
Code:
 tags?
 
Here are some results I get in trying to replicate your problem. The difference is that I don't have/use an Exchange account.

Code:
tell application "Mail"
	tell account "Foo" --IMAP account
		set newMailBox to make new mailbox with properties {name:"Prospects/Big Prospect"}
		set newMailBox's name to "Big Client"
	end tell
end tell

Events/Replies :

tell application "Mail"
make new mailbox with properties {name:"Prospects/Big Prospect"}
--> mailbox "Prospects/Big Prospect" of account "Foo"
set name of mailbox "Prospects/Big Prospect" of account "Foo" to "Big Client"
end tell


Could you post a screenshot of the sidebar of the Mail app with the Prospects mailbox? You can leave the mailbox folded so there aren't any confidentiality issues.
 

Attachments

  • Screen Shot 2014-05-29 at 02.20.41.png
    Screen Shot 2014-05-29 at 02.20.41.png
    13.1 KB · Views: 482
Last edited:
kryten, happy to do that but this is me being silly. How do I attach the screenshot? It seems to want a URL?

David
 
Look at Additional options --> Attach Files --> Manage Attachments.
 

Attachments

  • image.jpg
    image.jpg
    352.5 KB · Views: 189
Looks like a mailbox. I'm grasping at straws here but can you try the following :

Code:
tell application "Mail"
set name of mailbox "Prospects/Big Prospect" of account "Work" to "Big Client"
-- or
-- set name of mailbox "Prospects/Prospects/Big Prospect" of account "Work" to "Big Client"
end tell

What happens if you try to create a completely new mailbox and sub-mailbox inside the Work account and try to rename the sub-mailbox. For example let's name it Test/torchy. Do you get the same behaviour and results?

Code:
tell application "Mail"
	tell account "Work"
		set newMailBox to make new mailbox with properties {name:"Test/torchy"}
		set newMailBox's name to "Big Client"
	end tell
end tell

Note : First create the Big Prospect mailbox inside the Prospects mailbox manually.
 
Last edited:
Here's the issue:

1. If I manually create "Big Prospect" as a sub-folder of "Prospects", your first 'set name' does the rename perfectly.

2. There appears to be no way to get the script to create this hierarchy automatically. However we play with the make new mailbox path, the sub-folder always ends up being "Prospects/Big Prospect" within the "Prospects" main folder. Logically your second 'set name' should find that folder but it doesn't. It errors out saying it can't be found.

I can only think this is an Exchange bug/feature. It's a Catch 22. It will only create the folder one way, then it can't find it.

This should be simple......
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.