Using Mail.app with multiple users — using AppleScript!

OK, this is pretty brilliant — and SO simple.

Here’s the setup: my wife and I share a Mac at home. For memory reasons, among others, I don’t want to use Fast User Switching, and because of my automated tasks, I don’t want the primary account to ever be logged out. So all many of our programs need to be set-up for two different users. Firefox has user profiles, that once configured, works perfect. Mail.app has no such thing. What I decided to then, was write an AppleScript that would switch Mail.app between users for us. This example is for two users, but it could be edited for more. Here’s how to use it:

  • Setup Mail accounts for each user
  • Modify the script to prompt for each user you have, and reference their account name
  • Replace your Mail.app dock icon with a link to your AppleScript (you can even give it the Mail.app icon)
  • Whenever you launch Mail, you’ll be asked which user you want to use
  • Even better than that, you can switch users just by clicking the Mail icon in your dock again. You don’t even have to close down Mail!

The result looks like this whenever you invoke the script, and launches/reconfigures Mail within two seconds. Set the delay to longer if Mail.app takes longer to start on your Mac.

The code is dead simple, and took me only moments to put together. Note that the delays and the order in which things are done is important so that it doesn’t hang waiting for Mail to start if its not already open.

-- Mail Account Chooser, by Jon Wise
-- Add user profiles to Mail.app
display dialog "Choose the Mail account to use" buttons {"Jon", "Elizabeth"} default button 1 with icon note
if the button returned of the result is "Jon " then
  tell application "Mail"
    activate
    delay 2
    set enabled of account "Elizabeth Home" to false
    set enabled of account "Jon Home" to true
  end tell
else
  tell application "Mail"
    activate
    delay 2
    set enabled of account "Jon Home" to false
    set enabled of account "Elizabeth Home" to true
  end tell
end if

Advertisements

30 thoughts on “Using Mail.app with multiple users — using AppleScript!

  1. Hi Jonathan,

    This looks exactly like what I am looking for but have never used any apple scripts before and had a few questions before I proceeded.

    I want to have 2 private and separate users for the mail app without having 2 accounts on my computer. Is this a script to do this?

    In your example is “Jon Home” and “Nicole Home” the name of the mail account within the mail app or the name of the user for the computer?

    Thanks for your help in advance 🙂

    Ross

  2. Yes, this is a script to create two “users” for Mail only, without having to use separate OS X users.
    Yes, “Jonathan Home” and “Elizabeth Home” are separate mail accounts. They will be completely private from each other when the script runs.

    Note that Mail will startup with all accounts enabled, but within a few seconds will switch (set by the “delay” statement) to the account you’ve chosen.

    You’ll find the Script Editor in the “AppleScript” folder within Applications. Its a helpful environment in that it will highlight any errors for you when you test your code. Good luck!

  3. Jonathan,

    Thanks heaps mate! It works like a charm. One more thing though, is there any way to put a password function on the script?

    Cheers

    Ross

  4. Hmm… sort of. But its probably not good enough for what you want. Certainly it won’t actually secure anything from anyone more adept than the most basic of users. Nonetheless, here’s something you can try.

    Inside each result of the “if” statement of the original script add code like this around the “tell application” blocks:

    set thePassword to "JonsSecretPassword"
    display dialog "Enter the password to access this account: " default answer ""
    set dialogInfo to result
    set theAttempt to text returned of dialogInfo
    if theAttempt is equal to thePassword
      -- put the "tell application" stuff here
    else
      tell application "Mail" to set enabled of account "JonHome" to false
      display dialog "Incorrect password!" buttons {"OK"}
    end if

    There are a couple problems with this though:
    – You have to hard code a password into the Script. Unless you save the Script as an uneditable application, anyone could open the script and read your password.
    – There’s no “password” field in basic AppleScript, so you’ll see the password in the text box as you type it in — so will anyone else watching. Not very secure.
    – Anyone who knows a little about Mail.app can still go into Preferences and re-enable your account.

    If you really need security, your best bet is to leverage the Unix base of OS X, and create a separate user account with a password on it. Still not unhackable, but much harder than AppleScript.
    AppleScript is really just a little utility language for automating common tasks. Its not a full programming language.

  5. Thanks very much for your help Jonathan.

    The purpose of having a password was just an idea I had (my inquisitiveness causes me to get carried away sometimes…) but is not essential as the computer is only for personal use and our email accounts have no important or secret information to hide. All the same will plug in the script just to see how it works/looks.

    Cheers 🙂

    Ross

  6. THANK YOU !! I just fiddled with apple script for the first item using the above code. I added the lines for the passwords and it works great.

    I have a follow-up question for you. I would like to attach to the script the mail icon or a similar icon. Then when I added it to the dock, it is more obvious to click it to run. How can I change the icon from the standard apple script font to the apple mail font or similar font.

  7. Slight problem for me. When I finished creating the srcipt I would hit RUN to make sure it worked. It worked. I finally was able to figure out how to change the icon to the.
    Mail icon.

    My 2 problems are: 1, I can not add the script to the dock. 2, when I click the script icon, it take me to the editor where I then have to click RUN.

    Can you direct me on how to resolve these two

  8. Is there a similar script that could be used to select different Microsoft Entourage identities?

  9. Pingback: app para webmail (hotmail Gmail etc) - MacAdicto Foro

  10. Fantastic. I just set it up and it works great. Thank you. I wonder if there is a way to have the script run when you click on the actual Mail program rather than the script program first. It’s not big deal but the reason I’m thinking is so that there won’t be extra icons on the menu bar once the program has been opened. Thanks again.

  11. Unfortunately, I couldn’t find a way to do that. Each Dock item represents a different file, and since two files are involved (the script and the Mail.app executable) two files will have to be on the Dock. I use the Script menu instead…

  12. I have the Applescripts profile set-up with two profiles, one problem. The mail opens
    to the same profile regardless of which button I click.

    How do I get the Applescripts to select the individual mail according to the selection?

    Currently, I have both users in the one Mail app. Am I suppose to make to seperate
    Mail applications, one for each user?

  13. Hi Jon, Thanks for your reply – I appreciate your help. I was so happy to find
    your post – I have been researching for weeks to try and figure a way to switch
    users.

    Is there anyway to switch users after Mail has opened or quit Mail after you click
    on the script?

    Here is my Script – I just copied and pasted yours and then edited it.

    — Mail Account Chooser, by Steve Hirschberg
    — Add user profiles to Mail.app
    display dialog “Choose the Mail account to use” buttons {“Steve”, “Rada”} default button 1 with icon note
    if the button returned of the result is “Steve” then
    tell application “Mail”
    activate
    delay 2
    set enabled of account “Rada” to false
    set enabled of account “Steve” to true
    end tell
    else
    tell application “Mail”
    activate
    delay 2
    set enabled of account “Rada” to false
    set enabled of account “Steve” to true
    end tell
    end if

  14. This script provides a great way to separate work mail from home mail. Sure, you can use separate mailboxes or smart mailboxes to separate your mail, but sometimes you don’t even want to know that you have new “home” mail while you’re at work (or vice versa). Sometimes you want to work exclusively in one context, free from the distractions of the other context: you don’t want to hear that “new mail” chime and you don’t want to see the “unread” count for mail in that other context. This script fits that need beautifully. Thanks!

  15. I just wanted to do a quick post on something I am starting to do.

    I use my MBP for both work and personal. I don’t like mail/addressbook mixed so I have two separate login accounts. The problem is that I tend to need to switch between the two accounts with Fast User Switching… which is a hinderance with such things as music and alerts.

    What I have found, which I have not written a script for as of yet… is that you can do a ‘su [account]” in the terminal.. then launch mail or addressbook under that account.

    Its rather nice.. and so far has had few limitations.

    Anyway… a bit sideways in topic.. but maybe can get others thinking.

  16. I’ve used it on OS 10.5 (Leopard).
    Now it doesn’t work on OS 10.6 (Snow Leopard): I run the script and if I choose the first one user it works great, but if I choose the second one user Mail crash!!!!
    Could you please help me?
    Thnk.

    • I haven’t had time to go to Snow Leopard yet, but when I do, I’ll try this out and see if I can find out where the issue is. In theory, it shouldn’t make any difference which OS you’re running, but something might have changed in Mail.app’s AppleScript dictionary, so I’ll poke around and write an update if I can find the problem!

  17. Another little question:
    is it possibile to censor the password that I’m writing (e.g. with dark points)?
    Thnk.

  18. Hi Jonathon, thanks for the script, any chance you could publish the entire script including the password block? Not sure exactly where to insert it. Haven’t ever done any scripting.
    Cheers, JC

  19. Eric Hardman :
    THANK YOU !! I just fiddled with apple script for the first item using the above code. I added the lines for the passwords and it works great.

    Eric, any chance u could paste the entire script up? Cheers

  20. Jon Wise :
    I haven’t had time to go to Snow Leopard yet, but when I do, I’ll try this out and see if I can find out where the issue is. In theory, it shouldn’t make any difference which OS you’re running, but something might have changed in Mail.app’s AppleScript dictionary, so I’ll poke around and write an update if I can find the problem!

    Me too. Opens Mail and tries to close down an account and then crashes.

  21. This script is awesome! Is there a way to include the folder’s in the “ON MY MAC” section?

Comments are closed.