Mac OSX 账户自动创建.
Applescript
ldap 创建脚本
(*//This script walks through the creation of a new LDAP server entry to demonstrate the scriptability of Mail's Preference panels.*)
set success to 1
repeat
set theResult to display dialog "Enter a name for this LDAP server entry:" default answer "Example: My Company's LDAP server"
set theName to text returned of theResult
if (theName does not start with "Example:") then
exit repeat
end if
end repeat
repeat
set theResult to display dialog "Enter the address for the LDAP server:" default answer "Example: ldap.example.com"
set theAddress to text returned of theResult
if (theAddress does not start with "Example:") then
exit repeat
end if
end repeat
repeat
set theResult to display dialog "Enter the search base for the LDAP server:" default answer "Example: ou=people, o=company"
set theSearchBase to text returned of theResult
if (theSearchBase does not start with "Example:") then
exit repeat
end if
end repeat
set theResult to display dialog "Enter the port number: " default answer "389"
set thePort to text returned of theResult as integer
set theResult to choose from list {"Subtree", "One level", "Base"} default items {"Subtree"} with prompt "Select an LDAP server scope. The default selection should work in most situations." without multiple selections allowed
if theResult is not equal to false then
set theSelectedScope to item 1 of theResult
tell application "Mail"
if (theSelectedScope is equal to "Subtree") then
set theScope to subtree
else if (theSelectedScope is equal to "One level") then
set theScope to one level
else if (theSelectedScope is equal to "Base") then
set theScope to base
end if
try
make new ldap server with properties {name:theName, host name:theAddress, search base:theSearchBase, «class ldpo»:thePort, scope:theScope}
on error
set success to 0
end try
end tell
end if
if success is equal to 1 then
display dialog "LDAP server created!"
else
display dialog "LDAP server creation failed!"
end if
邮件账户设置脚本
(* //This script walks through the creation of an IMAP, POP, or .Mac account, including the setting of some advanced settings. This script demonstrates the scriptability of Mail Preferences, including accounts. *)
set success to 1
set theResult to getAccountType()
if theResult is not equal to false then
set accountTypeString to item 1 of theResult
set theAccountName to getAccountName()
set theUsername to getUsername()
set thePassword to getPassword()
set theEmailAddresses to getEmailAddress()
set theFullName to getFullName()
if accountTypeString is equal to "POP" or accountTypeString is equal to "IMAP" then
repeat
set theResult to display dialog "Enter the hostname for your incoming mail server:" default answer "例如: mail.example.com"
set theHostname to text returned of theResult
if theHostname does not start with "例如:" then
exit repeat
end if
end repeat
-- //POP specific options
if accountTypeString is equal to "POP" then
set deletionPolicy to my getDeletionPolicy()
if deletionPolicy is not equal to false then
set deletionPolicy to item 1 of deletionPolicy
set theNewAccount to my createAccount(accountTypeString, theAccountName, theUsername, theHostname, thePassword, theEmailAddresses, theFullName)
if theNewAccount is not equal to false then
setDeletionPolicy(theNewAccount, deletionPolicy)
getAndSetAuthenticationScheme(accountTypeString, theNewAccount)
getAndSetSMTPServer(theNewAccount)
else
set success to 0
end if
end if
-- //IMAP specific options
else if accountTypeString is equal to "IMAP" then
set theNewAccount to my createAccount(accountTypeString, theAccountName, theUsername, theHostname, thePassword, theEmailAddresses, theFullName)
if theNewAccount is not equal to false then
getAndSetSpecialMailboxes(theNewAccount)
getAndSetCachingSettings(theNewAccount)
getAndSetAuthenticationScheme(accountTypeString, theNewAccount)
getAndSetSMTPServer(theNewAccount)
else
set success to 0
end if
end if
end if
if success is equal to 1 then
display dialog "账户创建完成!"
else
display dialog "账户创建失败!"
end if
end if
-- //Convenience handler for creating accounts
on createAccount(theAccountType, theAccountName, theUsername, theHostname, thePassword, theEmailAddresses, theFullName)
tell application "Mail"
try
if theAccountType is equal to "IMAP" then
set theNewAccount to make new imap account with properties {name:theAccountName, user name:theUsername, server name:theHostname, password:thePassword, full name:theFullName, email addresses:{theEmailAddresses}}
else if theAccountType is equal to "POP" then
set theNewAccount to make new pop account with properties {name:theAccountName, user name:theUsername, server name:theHostname, password:thePassword, full name:theFullName, email addresses:{theEmailAddresses}}
end if
on error
set theNewAccount to false
end try
end tell
return theNewAccount
end createAccount
-- //Ask the user for the type of account they want to create
on getAccountType()
set theResult to choose from list {"IMAP", "POP"} with prompt "您需要创建何种类型的账户? SMTP配置会根据需要自动创建." without multiple selections allowed
return theResult
end getAccountType
-- //Ask the user what they would like to name the account
on getAccountName()
repeat
set theResult to display dialog "输入您的配置信息名称:" default answer "例如: My Home Account"
set theAccountName to text returned of theResult
if theAccountName does not start with "例如:" then
exit repeat
end if
end repeat
return theAccountName
end getAccountName
-- //Ask the user for the user name for their email account
on getUsername()
repeat
set theResult to display dialog "输入您邮件账户的用户名:" default answer "例如: janedoe"
set theUsername to text returned of the theResult
if theUsername does not start with "例如:" then
exit repeat
end if
end repeat
return theUsername
end getUsername
-- //Ask the user for the password for their email account
on getPassword()
set theResult to display dialog "输入您邮件账户密码:" default answer ""
set thePassword to text returned of theResult
return thePassword
end getPassword
-- //Ask the user for the email addresses for their email account
on getEmailAddress()
repeat
set theResult to display dialog "What email address would you like to use for this account?" default answer "例如: steve@example.com"
set theEmailAddress to text returned of theResult
if theEmailAddress does not start with "例如:" then
exit repeat
end if
end repeat
return theEmailAddress
end getEmailAddress
-- //Ask the user for the full name for their email account
on getFullName()
repeat
set theResult to display dialog "输入您的全名:" default answer "例如: Steve Smith"
set theFullName to text returned of theResult
if (theFullName does not start with "例如:") then
exit repeat
end if
end repeat
return theFullName
end getFullName
-- //Convenience handler for asking the user what settings they would
-- //like to have for their special mailboxes. This handler also sets these
-- //values in Mail.
on getAndSetSpecialMailboxes(theAccount)
-- Sent messages default to storing locally
set theResult to display dialog "Would you like to store Sent Messages on the IMAP server?" buttons {"Yes", "No"} default button 2
log theAccount
tell application "Mail"
tell theAccount
if button returned of theResult is equal to "Yes" then
set store sent messages on server to true
else if button returned of theResult is equal to "No" then
set store sent messages on server to false
end if
end tell
end tell
-- //Drafts default to storing locally
set theResult to display dialog "Would you like to store Drafts on the IMAP server?" buttons {"Yes", "No"} default button 2
tell application "Mail"
tell theAccount
if button returned of theResult is equal to "Yes" then
set store drafts on server to true
else if button returned of theResult is equal to "No" then
set store drafts on server to false
end if
end tell
end tell
-- //Trash defaults to storing on the IMAP server
set theResult to display dialog "Would you like to store Deleted Messages on the IMAP server?" buttons {"Yes", "No"} default button 1
tell application "Mail"
tell theAccount
if button returned of theResult is equal to "Yes" then
set store deleted messages on server to true
else if button returned of theResult is equal to "No" then
set store deleted messages on server to false
end if
end tell
end tell
end getAndSetSpecialMailboxes
-- //Convenience handler for asking the user what IMAP
-- //caching setting they would like to use and configuring
-- //it in Mail.
on getAndSetCachingSettings(theAccount)
set theResult to choose from list {"Cache everything", "Cache everything but attachments", "Cache when read", "Don't cache"} ¬
with prompt "Choose a message caching setting for this account:" default items {"Cache everything"} without multiple selections allowed
if theResult is not equal to false then
tell application "Mail"
tell theAccount
if (item 1 of theResult is equal to "Cache everything") then
set message caching to all messages and their attachments
else if (item 1 of theResult is equal to "Cache everything but attachments") then
set message caching to all messages but omit attachments
else if (item 1 of theResult is equal to "Cache when read") then
set message caching to only messages I have read
else if (item 1 of theResult is equal to "Don't cache") then
set message caching to do not keep copies of any messages
end if
end tell
end tell
end if
end getAndSetCachingSettings
-- //Convenience handler for asking the user whether they want to use
-- //an already defined SMTP server (if any) or whether they want to
-- //define a new one.
on getAndSetSMTPServer(theAccount)
tell application "Mail" to set everySMTPServer to every smtp server
if ((count of everySMTPServer) > 0) then
set listOfSMTPServers to {}
repeat with eachServer in everySMTPServer
try
set listOfSMTPServers to listOfSMTPServers & name of eachServer
end try
end repeat
set theResult to display dialog ¬
"Would you like to create a new SMTP server or use one of the SMTP servers you have already defined?" buttons {"Use existing server", "Create new server"} default button 1
if button returned of theResult is equal to "Use existing server" then
set theResult to choose from list listOfSMTPServers with prompt ¬
"Select one of the SMTP servers you already have defined or click the 'Create New' button." without multiple selections allowed
if theResult is not equal to false then
set theResult to (item 1 of theResult) as string
repeat with eachServer in everySMTPServer
try
if (name of eachServer is equal to theResult) then
tell application "Mail" to set smtp server of theAccount to eachServer
end if
end try
end repeat
end if
else
createNewSMTPServer(theAccount)
end if
else
createNewSMTPServer(theAccount)
end if
end getAndSetSMTPServer
-- //Handler for creating a new SMTP server, if the user has none set up
-- //already or if they choose not to use one of their existing servers.
on createNewSMTPServer(theAccount)
repeat
set theResult to display dialog "What is the hostname of your SMTP server?" default answer "例如: smtp.example.com"
set theServerName to text returned of theResult
if theServerName does not start with "例如:" then
exit repeat
end if
end repeat
tell application "Mail"
set theSMTPServer to make new smtp server with properties {server name:theServerName}
set smtp server of theAccount to theSMTPServer
end tell
getAndSetAuthenticationScheme("SMTP", theSMTPServer)
end createNewSMTPServer
-- //Handler for asking the user what authentication scheme their server supports.
-- //The options are different for POP, IMAP, and SMTP. Unless you are told otherwise,
-- //it's best to leave these at their default settings.
on getAndSetAuthenticationScheme(accountType, theAccount)
if accountType is equal to "POP" then
set theChoices to {"Password", "Kerberos 4", "Kerberos 5", "KPOP", "MD5"}
set theDefault to {"Password"}
else if accountType is equal to "IMAP" then
set theChoices to {"Password", "Kerberos 4", "Kerberos 5", "MD5"}
set theDefault to {"Password"}
else if accountType is equal to "SMTP" then
set theChoices to {"None", "Password", "Kerberos 4", "Kerberos 5", "MD5"}
set theDefault to {"None"}
end if
set theResult to choose from list theChoices with prompt "Choose an authentication scheme for this " & accountType & " server. Most servers support 'Password' authentication." default items theDefault without multiple selections allowed
if theResult is not equal to false then
tell application "Mail"
set theScheme to item 1 of theResult
tell theAccount
if theScheme is equal to "Password" then
set authentication to password
else if theScheme is equal to "Kerberos 4" then
set authentication to «constant exutaxk4»
else if theScheme is equal to "Kerberos 5" then
set authentication to kerberos 5
else if theScheme is equal to "MD5" then
set authentication to md5
else if theScheme is equal to "None" then
set authentication to none
else if theScheme is equal to "KPOP" then
set authentication to «constant exutakpo»
end if
end tell
end tell
if accountType is equal to "SMTP" then
repeat
display dialog "What is the user name for this SMTP server?" default answer "例如: janedoe"
set theSMTPLogin to text returned of the result
if theSMTPLogin does not start with "例如:" then
exit repeat
end if
end repeat
display dialog "What is the password for this SMTP server?" default answer ""
set theSMTPPassword to text returned of the result
tell application "Mail"
tell theAccount
set user name to theSMTPLogin
set password to theSMTPPassword
end tell
end tell
end if
end if
end getAndSetAuthenticationScheme
-- //Handler for asking the user what POP deletion policy
-- //they would like to use for their account.
on getDeletionPolicy()
set theResult to choose from list {"Immediately after being downloaded", "After a specified number of days", "When I remove them from the inbox", "Always leave them on the server"} with prompt "Choose a POP message deletion option:" default items {"Always leave them on the server"} without multiple selections allowed
return theResult
end getDeletionPolicy
-- //Handler for setting the deletion policy established in getDeletionPolicy()
on setDeletionPolicy(theAccount, thePolicy)
tell application "Mail"
tell theAccount
if thePolicy is equal to "Immediately after being downloaded" then
set delete mail on server to true
set delayed message deletion interval to 0
else if thePolicy is equal to "After a specified number of days" then
set numberOfDays to my getDeletionInterval()
set delete mail on server to true
set delayed message deletion interval to numberOfDays
else if thePolicy is equal to "When I remove them from the inbox" then
set delete mail on server to true
set delete messages when moved from inbox to true
else if thePolicy is equal to "Always leave them on the server" then
set delete mail on server to false
end if
end tell
end tell
end setDeletionPolicy
-- //Handler for asking the user what deletion interval they
-- //would like to use, if they are setting up a POP account
on getDeletionInterval()
set theResult to display dialog "After how many days would you like POP messages to be deleted from the server?" default answer "30"
set numberOfDays to text returned of theResult as integer
return numberOfDays
end getDeletionInterval