Jan 27 2008

Using Mail.app with multiple users — using AppleScript: Part 2

Published by Jonathan Wise at 8:21 pm under Hacks

The previous post, about hacking multi-user support into Mail.app, seems to have struck a chord, and there have been lots of great questions about how to extend the script, or customize its behavior or appearance. Things tend to get lost in the comments, so I figured I’d create a part 2 to answer some of the questions that have popped up. If you haven’t already, check out part 1 to get caught up.

Some of these might seem a little basic to long-time users, but lets remember that not everyone has been using a Mac since OS 7 (or earlier!) and cut the newly Mac-faithful some slack…

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

A third account is done very easily by modifying the script slightly. (Update: If you’d like to do more than 3 accounts, see the solution in the comments which changes our user interface to a listbox)

The first thing you’ll want to do is update our crude little user interface to ask about the third account. This line here decides what options show up in the dialog box:

display dialog "Choose the Mail account to use" buttons {"Jon", "Nicole"} default button 1 with icon note

You can add as many up to 3 options as you want, just by comma seperating them, so instead of {”Jon”, “Nicole”} you could have {”Jon”, “Nicole”, “Yoda”}
Then you need to modify the “if” statement to support each individual referred to. In the original we said:

if the button returned of the result is "Jon" then
-- do stuff to change account to Jon
else
-- do stuff to change account to Nicole
end if


The else is a problem now because it assumes only two conditions. Instead of an else, we can do an else if — one for each individual we want to switch between:

if the button returned of the result is "Jon" then
-- do stuff to change account to Jon
else if the button returned of the result is "Nicole" then
-- do stuff to change account to Nicole
else if the button returned of the result is "Yoda" then
-- do stuff to change account to Luke
end if


Inside each condition you’ll need to disable all the other accounts (set enabled to account X to false) and then enable the account they’ve chosen.

Eric asks: 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.

This is one of those tricks that long time Mac users will know well, but is not very obvious for newer converts. You can copy and paste any icon from any application/document/folder to any other in the finder. Just click on the icon you want to “borrow” from and hit Apple + I for “Get Info.” In the info dialog, click on the icon and hit Apple + C to copy the icon to the clipboard. Now find your target icon (such as the script you just made) and “Get Info” on it, click the icon in the dialog and this time hit Apple + P to paste. Now you have a pretty icon!

Note: This will be limited by permissions, so if your user doesn’t have permission to ‘write’ to the target object, you won’t be able to paste.

Eric, having figured out the above on his own, then asks: 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.

A script, by itself is just a document. In order to make it into a runable application, you have to save it as such in Script Editor. From the File / Save As… dialog change the File Format to “application.”

Under most circumstances you won’t want to check the box for ‘Run Only’ because once you do, you cannot edit it in Script Editor again. Also, uncheck the box for ‘Startup Screen’ to make it run a little more gracefully.

You may also find that the Dock is not the best place for your script — since it will essentially give you two Mail icons. What I did instead was to enable the AppleScript menu and use that for all my common scripts.

If you want to do this, open the Application AppleScript Utility in the AppleScript folder and check the box for ‘Show Script Menu in the menu bar.’ You may also want to uncheck the box for “Show Library scripts” to hide the example scripts Apple includes to make your menu shorter.

Note: For scripts to show up in this menu you’ll have to save them where your Mac expects your scripts to be: In your user’s Library folder you’ll find a folder called “Scripts.” Put them, or an alias to them, in there.

Popularity: 21% [?]

8 Responses to “Using Mail.app with multiple users — using AppleScript: Part 2”

  1. Michael Pachecoon 04 Mar 2008 at 7:50 am

    “You can add as many options as you want, just by comma seperating them, so instead of {”Jon”, “Nicole”} you could have {”Jon”, “Nicole”, “Luke”, “Leia”, “Yoda”}”

    Every time I try it Applescript gives me the error message “Maximum of 3 buttons allowed.”

    Is this a version problem? I’m using applescript editor V. 2.1.1. Is there a later version that allows more than 3 buttons?

  2. Jonathan Wiseon 05 Mar 2008 at 1:58 pm

    I didn’t realise there was a 3 button limit. The only way I could think of would be to nest button sets… cludgy but it would work.
    For example first level buttons like: A-J, K-S, T-Z and then once you’ve narrowed it down, you’d have less buttons to show on the second round…

    Or! D’uh, I should have thought of this. What about a list box? That should be pretty easy to do, although i don’t know how off the top of my head. I’ll try to look into it tonite — I know I have some sample code somewhere that does a list box…

  3. Michael Pachecoon 05 Mar 2008 at 5:19 pm

    Hi Jon

    Thanks for the reply. I’ve been trying the list box but have had no luck. I’ve never done any kind of programming be fore so not surprising I can’t get it to work.
    What I have done in the mean time is split it into 2 applications one with 3 and one with 2 buttons.

    Thanks for all the help this is great stuff.
    This is why I love the internet!!

  4. Jon Wiseon 09 Mar 2008 at 3:55 pm

    Ok Michael, I finally looked this up. Here’s what you need to do:

    In place of your statement:
    display dialog "Choose the Mail account to use" buttons {"Jon", "Nicole"} default button 1 with icon note

    Put this code to create a list box instead of a dialog box:
    set theMailUsers to {"Jon", "Nicole", "Luke", "Leia", "Yoda", "Darth Vader"}
    choose from list theMailUsers with title "Choose the Mail account to use..."
    set selectedMailUser to result as string

    Then, in place of each “if” statement, you’ll need a slightly different condition checker:
    if selectedMailUser is "Jon" then
    ...
    else if selectedMailUser is "Nicole" then
    ...
    etc
    ...
    end if

    That will give you a list box at start-up to chose from instead of buttons. I’m not sure what the maximum number of list items is, but it should be enough for even the largest of families! Hope this helps!

  5. Jacobon 31 May 2008 at 4:42 pm

    Hi… This is exactly what I needed. Is it also possible to do the same with iCal and the address book?

  6. Jonathan Wiseon 06 Jun 2008 at 4:34 pm

    With iCal, I would wager that yes, you can. I can’t look at the AppleScript iCal Dictionary from here (no Mac present) but I’m sure you could use script in a similar way to turn on and off Calendars.

    With Address Book, the only solution I can think of is a shell script to swap out the underlying Address Book database — which is very self-healing… so much so that this approach likely won’t work.

    At any rate, I think if you want this much separation of data, a cost-benefit analysis would likely lead you to just create a different user login on your Mac.

  7. Mannyon 08 Jul 2008 at 10:28 pm

    This is awesome! Seriously, thank you!! I followed your easy steps and had this up and running in a matter of minutes, with absolutely no issues!

    Thanks so much!!

  8. Kishan Dattanion 10 Jul 2008 at 11:26 am

    hey guys….really new to apple, only got it last week
    i started using mail and realised that i had the same problem, as in anyone could click on mail and see all my mail, i have used all your ideas so thank you to everyone but have made some adjustments…..not really sure how relevent or usefull they are but it gave me a big smile when it worked.
    i have added a couple lines that will quit mail when a new login is made and then re-opens it to the new user.

    set theMailUsers to {”luke”}
    choose from list theMailUsers with title “Choose the Mail account to use…”
    set selectedMailUser to result as string
    if selectedMailUser is “luke” then
    set thePassword to “skywalker”
    display dialog “Enter the password to acceess this account: ” default answer “”
    set dialogInfo to result
    set theAttempt to text returned of dialogInfo
    if theAttempt is equal to thePassword then
    tell application “Mail”
    if application “Mail” is running then
    quit
    delay 1
    end if
    activate
    delay 0
    set enabled of account “luke” to true
    end tell
    else
    tell application “Mail” to set enabled of account “luke” to false
    display dialog “Incorrect password!” buttons {”OK”}
    end if
    end if

    its like coding in matlab….loads of fun
    Peace Kishan

Print This Post Print This Post   Trackback URI Comments RSS

Leave a Reply