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?
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”, “Elizabeth”} you could have {“Jon”, “Elizabeth”, “David”}
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 Elizabeth
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 "Elizabeth" then
-- do stuff to change account to Elizabeth
else if the button returned of the result is "David" then
-- do stuff to change account to David
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.
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.
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.
“You can add as many options as you want, just by comma seperating them, so instead of {”Jonathan”, “Elizabeth”} you could have {”Jonathan”, “Elizabeth”, “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?
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…
Hi Jonathan
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!!
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!
Hi… This is exactly what I needed. Is it also possible to do the same with iCal and the address book?
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.
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!!
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
Jon,
I love this idea and was wondering how you would address the following situation-
I currently have 5 email accounts set up for myself (various organizations require separate accounts) and my wife has one account. I would like to set up your separate user access, but want to be able to send/receive mail on my 5 accounts at the same time without having to set up separate access for each of those. This is currently how I have it set up in Entourage, but I’m moving back to mac mail and want the same functionality, except I want to separate my wife’s account so she has her own access and her mail is not mixed in with my accounts. Any idea if your script or a variation of it would work?
Sure Dan, this script could be used to toggle 5 or 50 accounts. Just modify the blocks of code that happen after a button is pressed, adding support for those other accounts…
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
set enabled of account "Jon Work" to true
set enabled of account "Jon GMail" to true
end tell
else
tell application "Mail"
activate
delay 2
set enabled of account "Jon Home" to false
set enabled of account "Jon Work" to false
set enabled of account "Jon GMail" to false
set enabled of account "Elizabeth Home" to true
end tell
end if
Hallo Jon ,
vielen Dank für die ausführlichen Beschreibungen und Hilfen. Nach langem Suchen bin ich auf deine Seite gestoßen und es funktioniert fantastisch 🙂
Bitte 🙂
Jon. Fantastic little script. It took me around 2 minutes to find you on the internet as we had the same challenge as you and your wife with regards to accessing only our particular mail account. Thanks so much. Works like a charm! Gerald.
Hey Jon, this script works really well and is exactly what I was looking for. I’m having one issue though. The script runs really well when hitting “run” in script editor, but I’m getting the following error when I try to run it saved as an application:
When I try to access my account “Stew Gmail”
Can’t set <> of <> “Mere Gmail” of application “Mail” to false.
and when I try to access my wife’s email “Mere Gmail”
Can’t set <> of <> “Stew Gmail” of application “Mail” to false.
Any idea what I’m doing wrong?
Thanks!
Sorry, I can’t think of anything. If you want to send me the code of your script I can take a look…
Try it with a longer delay, Stew!
Dear Jon,
I tried running your email account switcher applescript only to be met with this error message…
NSCannotCreateScriptCommandError
The script editor highlights ‘accounts’ and got me to thinking, is the error generated because one account is .mac and the other is IMAP? If so do you think there may be an extra few lines of code which can be added to your initial script which would solve this?
I last wrote script nearly 20 years ago so I am more than rusty, any ideas?
I’m sure if I, as Admin, set up a new account from login I’d not need answer the above question but where would the fun be in that?
Thanks for taking the time out to read.
Sincerely,
Ian
Ian,
That error looks like a more systemic AppleScript problem on your machine… but I’ve never used .Mac/MobileMe so you could be right. Sorry I can’t help :-/