Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Need some Applescript help with this example Apple made script

Need some Applescript help with this example Apple made script
Thread Tools
Addicted to MacNN
Join Date: Oct 1999
Location: The Tollbooth Capital of the US
Status: Offline
Reply With Quote
Jun 23, 2005, 06:24 PM
 
I keep getting a NSInternalScriptError

I'm not skilled enough at applescript to figure this out. Could someone please help me out?

Thanks.

The Script is below: It can also be found in the Applscript folder under example scripts. It's the Create new mail account script.
(*
Create New Mail Account

Copyright © 2002 Apple Computer, Inc.

You may incorporate this Apple sample code into your program(s) without
restriction. This Apple sample code has been provided "AS IS" and the
responsibility for its operation is yours. You are not permitted to
redistribute this Apple sample code as "Apple sample code" after having
made changes. If you're going to redistribute the code, we require
that you make it clear that the code was descended from Apple sample
code, but that you've made changes.
*)

(*
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()

-- .Mac accounts
if accountTypeString is equal to ".Mac" then
set theHostname to "mail.mac.com"
set theNewAccount to my createAccount(accountTypeString, theAccountName, theUsername, theHostname, thePassword, theEmailAddresses, theFullName)
if (theNewAccount is equal to false) then
set success to 0
end if

-- POP and IMAP share some settings
else 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 "Example: mail.example.com"
set theHostname to text returned of theResult
if theHostname does not start with "Example:" 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 "Account created!"
else
display dialog "Account creation failed!"
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 ".Mac" then
set theNewAccount to make new Mac 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 "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", ".Mac"} with prompt "What type of account would you like to create?" 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 "What would you like this account to be named?" default answer "Example: My Home Account"
set theAccountName to text returned of theResult
if theAccountName does not start with "Example:" 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 "What is your email user name?" default answer "Example: janedoe"
set theUsername to text returned of the theResult
if theUsername does not start with "Example:" 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 "What is the password for this account?" 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 "Example: steve@example.com"
set theEmailAddress to text returned of theResult
if theEmailAddress does not start with "Example:" 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 "What is the full name for this account?" default answer "Example: Steve Smith"
set theFullName to text returned of theResult
if (theFullName does not start with "Example:") 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 "Example: smtp.example.com"
set theServerName to text returned of theResult
if theServerName does not start with "Example:" 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 kerberos 4
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 kpop
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 "Example: janedoe"
set theSMTPLogin to text returned of the result
if theSMTPLogin does not start with "Example:" 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 true
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
"Evil is Powerless If the Good are Unafraid." -Ronald Reagan

Apple and Intel, the dawning of a NEW era.
     
Junior Member
Join Date: Mar 2000
Location: Salem, OR, USA
Status: Offline
Reply With Quote
Jun 23, 2005, 07:36 PM
 
Could you please post what happens when you run the script. Do you get any prompts? If so, what selections did you make at those prompts. It will help to know exactly what you have done.
     
typoon  (op)
Addicted to MacNN
Join Date: Oct 1999
Location: The Tollbooth Capital of the US
Status: Offline
Reply With Quote
Jun 23, 2005, 08:51 PM
 
Originally Posted by numero
Could you please post what happens when you run the script. Do you get any prompts? If so, what selections did you make at those prompts. It will help to know exactly what you have done.
I run the script nomally. it comes up first asking what type of server you want to setup. either IMAP, POP or .Mac. it then comes up with I go through following the prompts for username, email address and pop server. it seems to have a problem when you get to the prompt asking if you how long you want to keep mail on the server. The options to leave it on the server or remove immediately seem to cause the problem. it only seem to happen when I'm setting up a POP account.
"Evil is Powerless If the Good are Unafraid." -Ronald Reagan

Apple and Intel, the dawning of a NEW era.
     
Junior Member
Join Date: Mar 2000
Location: Salem, OR, USA
Status: Offline
Reply With Quote
Jun 24, 2005, 07:50 PM
 
I've been messing around with this for a little while. I'm running 10.4.1.

I'd say that you have found a bug. If I get the properties on a sample account I set up I get the following: "delete mail on server:missing value". This happens with the "Remove copy from server after retrieving a message" both checked and unchecked in the Accounts preferences pane of Mail.

Here is my debugging script.

tell application "Mail"
tell pop account "sample script"
get properties
set delete mail on server to true
get properties
end tell
end tell


I was going to get the properties before and after the setting of the property. I coudn't get beyond the setting of the property without getting the message you got.

Since the script is reporting back that the value is missing (and not TRUE or FALSE) I am guessing that the applescript code in Mail isn't wired correctly.

Sorry.
     
typoon  (op)
Addicted to MacNN
Join Date: Oct 1999
Location: The Tollbooth Capital of the US
Status: Offline
Reply With Quote
Jul 12, 2005, 11:31 AM
 
Originally Posted by numero
I've been messing around with this for a little while. I'm running 10.4.1.

I'd say that you have found a bug. If I get the properties on a sample account I set up I get the following: "delete mail on server:missing value". This happens with the "Remove copy from server after retrieving a message" both checked and unchecked in the Accounts preferences pane of Mail.

Here is my debugging script.

tell application "Mail"
tell pop account "sample script"
get properties
set delete mail on server to true
get properties
end tell
end tell


I was going to get the properties before and after the setting of the property. I coudn't get beyond the setting of the property without getting the message you got.

Since the script is reporting back that the value is missing (and not TRUE or FALSE) I am guessing that the applescript code in Mail isn't wired correctly.

Sorry.

Thanks. for looking into it for me.
"Evil is Powerless If the Good are Unafraid." -Ronald Reagan

Apple and Intel, the dawning of a NEW era.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:17 AM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2