It took me a while and the result is more complex than I would have liked but here is a script which appears to work reliably on Leopard. It installs a temporary account if Mail has no existing accounts.
I was determined not to put passwords in the script and this may have added slightly to the complexity as most modern mail clients prefer to check account viability by logging on during setup.
I would be grateful for feedback about performance on other OSs and with IMAP - please backup your email and settings before using this script for the first time.
I have tested the script posted here by copying everything between 'code' to Script Editor and running it.
{code}
-- Script to install a new POP (or IMAP) account on Mail
-- This script has only been tested for POP on Leopard
-- Old Mail folders will be renamed if no account is detected
-- Setting delete mail to false is the safest way to start
-- It is more secure to insert passwords when Mail is first used
-- Password for test account is: dumpty
-- Mail plist file location:
set plistFile to "~/Library/Preferences/com.apple.mail.plist"
-- For Mountain Lion change the above to:
-- ~/Library/Containers/com.apple.mail/Data/Library/Preferences/com.apple.Mail.pli st
-- Account details:
setaccountNameto"lavabit"
setaccountUserto"humpty"
set accountServer to "lavabit.com"
setaccountPortto995-- do not quote
setaccountSSLtotrue-- do not quote
setaccountTypeto"pop"
set fullName to "Humpty Dumpty"
set emailAddress to "humpty@lavabit.com"
setdeleteMailtofalse-- do not quote
-- Optional SMTP account details - not always easy to add later:
set smtpUser to "humpty"
set smtpServer to "lavabit.com"
setsmtpPortto465-- do not quote
setsmtpSSLto"YES"
setuseAuthenticationto"YES"
setsmtpAuthto"PASSWORD"-- do not change this to your password
tellapplication"Mail"
ifnot(firstaccountexists)then
try
do shell script "killall Mail; p=" & plistFile & ";
f(){ mv $1 $1-`date +%Y%m%d%H%M%S` || :; }; f ~/Library/Mail; f $p; printf \"
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE plist PUBLIC '-//Apple//DTD PLIST 1.0//EN' 'http://www.apple.com/DTDs/PropertyList-1.0.dtd'>
<plist version='1.0'>
<dict>
<key>MailAccounts</key>
<array>
<dict>
<key>AccountName</key>
<string>delete me</string>
<key>AccountType</key>
<string>POPAccount</string>
</dict>
</array>
</dict>
</plist>
\" >> $p"
endtry
endif
setstopScripttofalse
ifaccountaccountNameexiststhen
display dialog "Account '" & accountName & "' already exists. Either backup its emails and delete it or change one of the names." buttons {"Stop script"} with icon stop
else
repeatwithaccountNumberfrom1tocountofaccounts
if(user nameofaccountaccountNumber=accountUserandserver nameofaccountaccountNumber=accountServer)then
display dialog "Your existing '" & name of account accountNumber & "' account already has user name '" & accountUser & "' and server name '" & accountServer & "'. Either backup this account's emails and delete it or change one of the above names." buttons {"Stop script"} with icon stop
setstopScripttotrue
return
endif
endrepeat
ifstopScriptisfalsethen
ifaccountType="pop"then
setalphatomakenewpop accountwith properties{name:accountName,user name:accountUser,server name:accountServer,port:accountPort,uses ssl:accountSSL,full name:fullName,email addresses:emailAddress,delete mail on server:deleteMail}
else if accountType = "imap" then
setalphatomakenewimap accountwith properties{name:accountName,user name:accountUser,server name:accountServer,port:accountPort,uses ssl:accountSSL,full name:fullName,email addresses:emailAddress}
else
setstopScripttotrue
display dialog accountType & " Ensure that 'accountType' is set to \"pop\" or \"imap\"." buttons {"Stop script"} with icon stop
endif
ifstopScriptisfalsethen
if account "delete me" exists then delete account "delete me"
try
do shell script "killall Mail"
endtry
endif
endif
endif
endtell
ifstopScriptisfalsethen
setsmtpDetailsto{|AccountName|:accountName,|AccountType|:"SMTPAccount",|Hostname|:smtpServer,|SSLEnabled|:smtpSSL,|ShouldUseAuthentication|:useAuthentication,|Username|:smtpUser,|AuthenticationScheme|:smtpAuth,|PortNumber|:smtpPort}
tell application "System Events"
tellproperty list fileplistFile
tell property list item "DeliveryAccounts"
makenewproperty list itematbeginningwith properties{kind:record,value:smtpDetails}
endtell
endtell
endtell
tellapplication"Mail"
setsmtp serverofalphatosmtp serveraccountName
endtell
endif
{code}