[关闭]
@khan-lau 2016-03-09T13:21:00.000000Z 字数 12829 阅读 2011

Mac OSX 账户自动创建.

Applescript

ldap 创建脚本

  1. (*//This script walks through the creation of a new LDAP server entry to demonstrate the scriptability of Mail's Preference panels.*)
  2. set success to 1
  3. repeat
  4. set theResult to display dialog "Enter a name for this LDAP server entry:" default answer "Example: My Company's LDAP server"
  5. set theName to text returned of theResult
  6. if (theName does not start with "Example:") then
  7. exit repeat
  8. end if
  9. end repeat
  10. repeat
  11. set theResult to display dialog "Enter the address for the LDAP server:" default answer "Example: ldap.example.com"
  12. set theAddress to text returned of theResult
  13. if (theAddress does not start with "Example:") then
  14. exit repeat
  15. end if
  16. end repeat
  17. repeat
  18. set theResult to display dialog "Enter the search base for the LDAP server:" default answer "Example: ou=people, o=company"
  19. set theSearchBase to text returned of theResult
  20. if (theSearchBase does not start with "Example:") then
  21. exit repeat
  22. end if
  23. end repeat
  24. set theResult to display dialog "Enter the port number: " default answer "389"
  25. set thePort to text returned of theResult as integer
  26. 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
  27. if theResult is not equal to false then
  28. set theSelectedScope to item 1 of theResult
  29. tell application "Mail"
  30. if (theSelectedScope is equal to "Subtree") then
  31. set theScope to subtree
  32. else if (theSelectedScope is equal to "One level") then
  33. set theScope to one level
  34. else if (theSelectedScope is equal to "Base") then
  35. set theScope to base
  36. end if
  37. try
  38. make new ldap server with properties {name:theName, host name:theAddress, search base:theSearchBase, «class ldpo»:thePort, scope:theScope}
  39. on error
  40. set success to 0
  41. end try
  42. end tell
  43. end if
  44. if success is equal to 1 then
  45. display dialog "LDAP server created!"
  46. else
  47. display dialog "LDAP server creation failed!"
  48. end if

邮件账户设置脚本

  1. (* //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. *)
  2. set success to 1
  3. set theResult to getAccountType()
  4. if theResult is not equal to false then
  5. set accountTypeString to item 1 of theResult
  6. set theAccountName to getAccountName()
  7. set theUsername to getUsername()
  8. set thePassword to getPassword()
  9. set theEmailAddresses to getEmailAddress()
  10. set theFullName to getFullName()
  11. if accountTypeString is equal to "POP" or accountTypeString is equal to "IMAP" then
  12. repeat
  13. set theResult to display dialog "Enter the hostname for your incoming mail server:" default answer "例如: mail.example.com"
  14. set theHostname to text returned of theResult
  15. if theHostname does not start with "例如:" then
  16. exit repeat
  17. end if
  18. end repeat
  19. -- //POP specific options
  20. if accountTypeString is equal to "POP" then
  21. set deletionPolicy to my getDeletionPolicy()
  22. if deletionPolicy is not equal to false then
  23. set deletionPolicy to item 1 of deletionPolicy
  24. set theNewAccount to my createAccount(accountTypeString, theAccountName, theUsername, theHostname, thePassword, theEmailAddresses, theFullName)
  25. if theNewAccount is not equal to false then
  26. setDeletionPolicy(theNewAccount, deletionPolicy)
  27. getAndSetAuthenticationScheme(accountTypeString, theNewAccount)
  28. getAndSetSMTPServer(theNewAccount)
  29. else
  30. set success to 0
  31. end if
  32. end if
  33. -- //IMAP specific options
  34. else if accountTypeString is equal to "IMAP" then
  35. set theNewAccount to my createAccount(accountTypeString, theAccountName, theUsername, theHostname, thePassword, theEmailAddresses, theFullName)
  36. if theNewAccount is not equal to false then
  37. getAndSetSpecialMailboxes(theNewAccount)
  38. getAndSetCachingSettings(theNewAccount)
  39. getAndSetAuthenticationScheme(accountTypeString, theNewAccount)
  40. getAndSetSMTPServer(theNewAccount)
  41. else
  42. set success to 0
  43. end if
  44. end if
  45. end if
  46. if success is equal to 1 then
  47. display dialog "账户创建完成!"
  48. else
  49. display dialog "账户创建失败!"
  50. end if
  51. end if
  52. -- //Convenience handler for creating accounts
  53. on createAccount(theAccountType, theAccountName, theUsername, theHostname, thePassword, theEmailAddresses, theFullName)
  54. tell application "Mail"
  55. try
  56. if theAccountType is equal to "IMAP" then
  57. 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}}
  58. else if theAccountType is equal to "POP" then
  59. 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}}
  60. end if
  61. on error
  62. set theNewAccount to false
  63. end try
  64. end tell
  65. return theNewAccount
  66. end createAccount
  67. -- //Ask the user for the type of account they want to create
  68. on getAccountType()
  69. set theResult to choose from list {"IMAP", "POP"} with prompt "您需要创建何种类型的账户? SMTP配置会根据需要自动创建." without multiple selections allowed
  70. return theResult
  71. end getAccountType
  72. -- //Ask the user what they would like to name the account
  73. on getAccountName()
  74. repeat
  75. set theResult to display dialog "输入您的配置信息名称:" default answer "例如: My Home Account"
  76. set theAccountName to text returned of theResult
  77. if theAccountName does not start with "例如:" then
  78. exit repeat
  79. end if
  80. end repeat
  81. return theAccountName
  82. end getAccountName
  83. -- //Ask the user for the user name for their email account
  84. on getUsername()
  85. repeat
  86. set theResult to display dialog "输入您邮件账户的用户名:" default answer "例如: janedoe"
  87. set theUsername to text returned of the theResult
  88. if theUsername does not start with "例如:" then
  89. exit repeat
  90. end if
  91. end repeat
  92. return theUsername
  93. end getUsername
  94. -- //Ask the user for the password for their email account
  95. on getPassword()
  96. set theResult to display dialog "输入您邮件账户密码:" default answer ""
  97. set thePassword to text returned of theResult
  98. return thePassword
  99. end getPassword
  100. -- //Ask the user for the email addresses for their email account
  101. on getEmailAddress()
  102. repeat
  103. set theResult to display dialog "What email address would you like to use for this account?" default answer "例如: steve@example.com"
  104. set theEmailAddress to text returned of theResult
  105. if theEmailAddress does not start with "例如:" then
  106. exit repeat
  107. end if
  108. end repeat
  109. return theEmailAddress
  110. end getEmailAddress
  111. -- //Ask the user for the full name for their email account
  112. on getFullName()
  113. repeat
  114. set theResult to display dialog "输入您的全名:" default answer "例如: Steve Smith"
  115. set theFullName to text returned of theResult
  116. if (theFullName does not start with "例如:") then
  117. exit repeat
  118. end if
  119. end repeat
  120. return theFullName
  121. end getFullName
  122. -- //Convenience handler for asking the user what settings they would
  123. -- //like to have for their special mailboxes. This handler also sets these
  124. -- //values in Mail.
  125. on getAndSetSpecialMailboxes(theAccount)
  126. -- Sent messages default to storing locally
  127. set theResult to display dialog "Would you like to store Sent Messages on the IMAP server?" buttons {"Yes", "No"} default button 2
  128. log theAccount
  129. tell application "Mail"
  130. tell theAccount
  131. if button returned of theResult is equal to "Yes" then
  132. set store sent messages on server to true
  133. else if button returned of theResult is equal to "No" then
  134. set store sent messages on server to false
  135. end if
  136. end tell
  137. end tell
  138. -- //Drafts default to storing locally
  139. set theResult to display dialog "Would you like to store Drafts on the IMAP server?" buttons {"Yes", "No"} default button 2
  140. tell application "Mail"
  141. tell theAccount
  142. if button returned of theResult is equal to "Yes" then
  143. set store drafts on server to true
  144. else if button returned of theResult is equal to "No" then
  145. set store drafts on server to false
  146. end if
  147. end tell
  148. end tell
  149. -- //Trash defaults to storing on the IMAP server
  150. set theResult to display dialog "Would you like to store Deleted Messages on the IMAP server?" buttons {"Yes", "No"} default button 1
  151. tell application "Mail"
  152. tell theAccount
  153. if button returned of theResult is equal to "Yes" then
  154. set store deleted messages on server to true
  155. else if button returned of theResult is equal to "No" then
  156. set store deleted messages on server to false
  157. end if
  158. end tell
  159. end tell
  160. end getAndSetSpecialMailboxes
  161. -- //Convenience handler for asking the user what IMAP
  162. -- //caching setting they would like to use and configuring
  163. -- //it in Mail.
  164. on getAndSetCachingSettings(theAccount)
  165. set theResult to choose from list {"Cache everything", "Cache everything but attachments", "Cache when read", "Don't cache"} ¬
  166. with prompt "Choose a message caching setting for this account:" default items {"Cache everything"} without multiple selections allowed
  167. if theResult is not equal to false then
  168. tell application "Mail"
  169. tell theAccount
  170. if (item 1 of theResult is equal to "Cache everything") then
  171. set message caching to all messages and their attachments
  172. else if (item 1 of theResult is equal to "Cache everything but attachments") then
  173. set message caching to all messages but omit attachments
  174. else if (item 1 of theResult is equal to "Cache when read") then
  175. set message caching to only messages I have read
  176. else if (item 1 of theResult is equal to "Don't cache") then
  177. set message caching to do not keep copies of any messages
  178. end if
  179. end tell
  180. end tell
  181. end if
  182. end getAndSetCachingSettings
  183. -- //Convenience handler for asking the user whether they want to use
  184. -- //an already defined SMTP server (if any) or whether they want to
  185. -- //define a new one.
  186. on getAndSetSMTPServer(theAccount)
  187. tell application "Mail" to set everySMTPServer to every smtp server
  188. if ((count of everySMTPServer) > 0) then
  189. set listOfSMTPServers to {}
  190. repeat with eachServer in everySMTPServer
  191. try
  192. set listOfSMTPServers to listOfSMTPServers & name of eachServer
  193. end try
  194. end repeat
  195. set theResult to display dialog ¬
  196. "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
  197. if button returned of theResult is equal to "Use existing server" then
  198. set theResult to choose from list listOfSMTPServers with prompt ¬
  199. "Select one of the SMTP servers you already have defined or click the 'Create New' button." without multiple selections allowed
  200. if theResult is not equal to false then
  201. set theResult to (item 1 of theResult) as string
  202. repeat with eachServer in everySMTPServer
  203. try
  204. if (name of eachServer is equal to theResult) then
  205. tell application "Mail" to set smtp server of theAccount to eachServer
  206. end if
  207. end try
  208. end repeat
  209. end if
  210. else
  211. createNewSMTPServer(theAccount)
  212. end if
  213. else
  214. createNewSMTPServer(theAccount)
  215. end if
  216. end getAndSetSMTPServer
  217. -- //Handler for creating a new SMTP server, if the user has none set up
  218. -- //already or if they choose not to use one of their existing servers.
  219. on createNewSMTPServer(theAccount)
  220. repeat
  221. set theResult to display dialog "What is the hostname of your SMTP server?" default answer "例如: smtp.example.com"
  222. set theServerName to text returned of theResult
  223. if theServerName does not start with "例如:" then
  224. exit repeat
  225. end if
  226. end repeat
  227. tell application "Mail"
  228. set theSMTPServer to make new smtp server with properties {server name:theServerName}
  229. set smtp server of theAccount to theSMTPServer
  230. end tell
  231. getAndSetAuthenticationScheme("SMTP", theSMTPServer)
  232. end createNewSMTPServer
  233. -- //Handler for asking the user what authentication scheme their server supports.
  234. -- //The options are different for POP, IMAP, and SMTP. Unless you are told otherwise,
  235. -- //it's best to leave these at their default settings.
  236. on getAndSetAuthenticationScheme(accountType, theAccount)
  237. if accountType is equal to "POP" then
  238. set theChoices to {"Password", "Kerberos 4", "Kerberos 5", "KPOP", "MD5"}
  239. set theDefault to {"Password"}
  240. else if accountType is equal to "IMAP" then
  241. set theChoices to {"Password", "Kerberos 4", "Kerberos 5", "MD5"}
  242. set theDefault to {"Password"}
  243. else if accountType is equal to "SMTP" then
  244. set theChoices to {"None", "Password", "Kerberos 4", "Kerberos 5", "MD5"}
  245. set theDefault to {"None"}
  246. end if
  247. 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
  248. if theResult is not equal to false then
  249. tell application "Mail"
  250. set theScheme to item 1 of theResult
  251. tell theAccount
  252. if theScheme is equal to "Password" then
  253. set authentication to password
  254. else if theScheme is equal to "Kerberos 4" then
  255. set authentication to «constant exutaxk4»
  256. else if theScheme is equal to "Kerberos 5" then
  257. set authentication to kerberos 5
  258. else if theScheme is equal to "MD5" then
  259. set authentication to md5
  260. else if theScheme is equal to "None" then
  261. set authentication to none
  262. else if theScheme is equal to "KPOP" then
  263. set authentication to «constant exutakpo»
  264. end if
  265. end tell
  266. end tell
  267. if accountType is equal to "SMTP" then
  268. repeat
  269. display dialog "What is the user name for this SMTP server?" default answer "例如: janedoe"
  270. set theSMTPLogin to text returned of the result
  271. if theSMTPLogin does not start with "例如:" then
  272. exit repeat
  273. end if
  274. end repeat
  275. display dialog "What is the password for this SMTP server?" default answer ""
  276. set theSMTPPassword to text returned of the result
  277. tell application "Mail"
  278. tell theAccount
  279. set user name to theSMTPLogin
  280. set password to theSMTPPassword
  281. end tell
  282. end tell
  283. end if
  284. end if
  285. end getAndSetAuthenticationScheme
  286. -- //Handler for asking the user what POP deletion policy
  287. -- //they would like to use for their account.
  288. on getDeletionPolicy()
  289. 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
  290. return theResult
  291. end getDeletionPolicy
  292. -- //Handler for setting the deletion policy established in getDeletionPolicy()
  293. on setDeletionPolicy(theAccount, thePolicy)
  294. tell application "Mail"
  295. tell theAccount
  296. if thePolicy is equal to "Immediately after being downloaded" then
  297. set delete mail on server to true
  298. set delayed message deletion interval to 0
  299. else if thePolicy is equal to "After a specified number of days" then
  300. set numberOfDays to my getDeletionInterval()
  301. set delete mail on server to true
  302. set delayed message deletion interval to numberOfDays
  303. else if thePolicy is equal to "When I remove them from the inbox" then
  304. set delete mail on server to true
  305. set delete messages when moved from inbox to true
  306. else if thePolicy is equal to "Always leave them on the server" then
  307. set delete mail on server to false
  308. end if
  309. end tell
  310. end tell
  311. end setDeletionPolicy
  312. -- //Handler for asking the user what deletion interval they
  313. -- //would like to use, if they are setting up a POP account
  314. on getDeletionInterval()
  315. set theResult to display dialog "After how many days would you like POP messages to be deleted from the server?" default answer "30"
  316. set numberOfDays to text returned of theResult as integer
  317. return numberOfDays
  318. end getDeletionInterval
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注