user1098491
user1098491

Reputation: 11

Applescript: How can I stop the script until the user puts in their password in Apple Mail?

My applescript gets hung up right before it can Synchronize because of a bug in Apple Mail where if the SMTP server doesnt match the description it will ask for the password again..so the user has to input their password and hit ok or enter before anything will happen. I had a dialog prompt come up that said "it is a good idea to remember password in keychain" then I had the other prompt activate on top of it so that they couldn't click ok without entering their password…but that seems inconsistant and lame. I would rather figure out a way that the prompt wont come up, or if I can pause the script until the password is entered.

--Activate Mail Application
tell application "Mail"
activate "Mail"
display dialog "Welcome to Mailbox setup with Google Mail. Please enter the password for your e-mail when prompted. This should not take any more than a minute."

delay 0.5

--Variables
--Creating account


set email_address to (short user name of (system info)) & "@EMAIL.com"
set full_name to (long user name of (system info))
set mailboxname to (long user name of (system info))
set shortname to (short user name of (system info))
set myPass to text returned of ¬
    (display dialog "Enter password for " & ¬
        quoted form of shortname ¬
        with title ¬
        "Gmail" with icon stop ¬
        default answer ¬
        "" buttons {"Continue…"} ¬
        default button 1 ¬
        giving up after 5 with hidden answer)


try
    set newacct to "imap.gmail.com"
    set newacct to make new imap account with properties {name:mailboxname, user name:email_address, password:myPass, uses ssl:true, server name:"imap.gmail.com", port:993, full name:full_name, email addresses:{email_address}}
    tell application "System Events" to key code 53

    --Create SMTP filter
    set addsmtp to "smtp.gmail.com"
    set addsmtp to make new smtp server with properties {server name:"smtp.gmail.com", user name:email_address, uses ssl:true, authentication:password, password:myPass}


    --Attach SMTP server
    set smtp server of newacct to addsmtp
    delay 1.0
    tell application "System Events" to tell process "Mail"
        set frontmost to true
        keystroke return
    end tell

    --Mailbox Sync
    delay 0.5
    on return
    tell application "Mail"
        synchronize with account mailboxname
    end tell

end try
end tell

Example

Upvotes: 1

Views: 1246

Answers (3)

jonathanbardo
jonathanbardo

Reputation: 160

There is a workaround :

set authentication to ("axct" as constant)

instead of this

set authentication to password

Upvotes: 2

Ryan
Ryan

Reputation: 11

I started work on this the other day as well. As far as I can see, you should be having the same problem as me. When you tell the script to set the new SMTP server up, you have to declare

"authentication:password"

The problem is that this is quite simply a bug. If you check the Mail library in AppleCode, it states that "authentication" is the right prefix to the suffix of "password".

Despite this, it throws up errors every time.

I can't see any work around at the minute, nor can people who have tried this previously. Majorly rubbish

Upvotes: 1

Lars Blumberg
Lars Blumberg

Reputation: 21371

You can wait for the password input by checking if a sheet (in your case for password input) is opened:

tell application "System Events"
    tell process "Mail"
        repeat until exists sheet 1 of window 1
            delay 0.1
        end repeat
    end tell
end tell

Upvotes: 1

Related Questions