Jan 06 2008

Using Mail.app with multiple users — using AppleScript!

Published by Jonathan Wise at 3:15 pm under Code Snippets, Hacks

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 Jonathan Wise
-- Add user profiles to Mail.app
display dialog "Choose the Mail account to use" buttons {"Jon", "Nicole"} 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 "Nicole 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 "Nicole Home" to true
  end tell
end if

Popularity: 35% [?]

12 Responses to “Using Mail.app with multiple users — using AppleScript!”

  1. Rosson 07 Jan 2008 at 10:02 pm

    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. Jonathan Wiseon 07 Jan 2008 at 11:14 pm

    Yes, this is a script to create two “users” for Mail only, without having to use separate OS X users.
    Yes, “Jon Home” and “Nicole 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. Rosson 08 Jan 2008 at 3:12 am

    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. Jonathan Wiseon 08 Jan 2008 at 9:29 am

    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. Rosson 08 Jan 2008 at 7:56 pm

    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. Eric Hardmanon 26 Jan 2008 at 12:48 pm

    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. Evanon 26 Jan 2008 at 2:49 pm

    Absolutely perfect, exactly what I was looking for. Well, almost. How about adding a 3rd account?

    :)

  8. Eric Hardmanon 26 Jan 2008 at 11:16 pm

    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

  9. Jonathan Wiseon 28 Jan 2008 at 10:37 am

    Wow! This is an active post, I think I’ll write a Part 2 to answer some of these awesome questions…

  10. Dallason 30 Mar 2008 at 8:34 am

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

  11. Jonathan Wiseon 30 Mar 2008 at 9:27 am

    In what application? Are you a Windows/Outlook user? Mac/Entourage? Mac/Mail.app Exchange Support?

  12. [...] puedes crear otro usuario para mail.app codepoetry | Using Mail.app with multiple users — using AppleScript! Para Hotmail necesitas HTTPMail Plugin __________________ the revolution will not be [...]

Print This Post Print This Post   Trackback URI Comments RSS

Leave a Reply