How to create sub-mailboxes with Applescript?

Discussion in 'Mac Programming' started by torchy, May 26, 2014.

  1. torchy, May 26, 2014
    Last edited by a moderator: May 26, 2014

    torchy macrumors newbie

    Joined:
    Mar 22, 2014
    Location:
    London, UK
    #1
    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
     
  2. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #2
    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
     
  3. torchy thread starter macrumors newbie

    Joined:
    Mar 22, 2014
    Location:
    London, UK
    #3
    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?
     
  4. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #4
    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
     
  5. torchy thread starter macrumors newbie

    Joined:
    Mar 22, 2014
    Location:
    London, UK
    #5
    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"
     
  6. zeppenwolf macrumors regular

    zeppenwolf

    Joined:
    Nov 17, 2009
    #6
    What happens with

    Code:
    make new mailbox with properties {name:"[COLOR="Red"][B]/[/B][/COLOR]Big Prospect"}
     
  7. torchy thread starter macrumors newbie

    Joined:
    Mar 22, 2014
    Location:
    London, UK
    #7
    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?
     
  8. kryten2, May 28, 2014
    Last edited: May 28, 2014

    kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #8
    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
    Can you also post the result you get from :

    Code:
    get name of every mailbox of account "Work"
     
  9. torchy thread starter macrumors newbie

    Joined:
    Mar 22, 2014
    Location:
    London, UK
    #9
    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?
     
  10. kryten2, May 28, 2014
    Last edited: May 28, 2014

    kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #10
    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.
     

    Attached Files:

  11. torchy thread starter macrumors newbie

    Joined:
    Mar 22, 2014
    Location:
    London, UK
    #11
    kryten, happy to do that but this is me being silly. How do I attach the screenshot? It seems to want a URL?

    David
     
  12. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #12
    Look at Additional options --> Attach Files --> Manage Attachments.
     

    Attached Files:

  13. torchy thread starter macrumors newbie

    Joined:
    Mar 22, 2014
    Location:
    London, UK
    #13
    Thanks. Here we go:
     

    Attached Files:

  14. kryten2, May 29, 2014
    Last edited: May 29, 2014

    kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #14
    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.
     
  15. torchy thread starter macrumors newbie

    Joined:
    Mar 22, 2014
    Location:
    London, UK
    #15
    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......
     

Share This Page