Jim Downward

Q: Mail Applescript "set new mail sound" -> Apple Event handler failed

The following Applescript segment works in Snow Leopard and fails in Lion with "AppleEvent handler failed. number -10000"

 

tell application "Mail"

          set new mail sound to "Glass"

end tell

 

In addition, the Apple-supplied, Mail Applescript for gathering Mail information "Display All Accounts and Preferences.scpt" fails

(again with "AppleEvent handler failed. number -10000") on the line

 

                    set displayString to displayString & my formatFields("New mail sound", new mail sound) & return

 

The Console Log reports the following error.

 

 

Mail: [<MailApp 0x7fd6a9e009a0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key newMailSound.

 

The Mail Applescript property "new mail sound" appears not to be recognized correctly.

 

I do not have any Mail plugins.  Several additional sounds have been added to the system sound set.  Mail reports no problems using them.

 

Under Snow Leopard, I used the same Applescripts (fired off by iCal) which turned on/off Mail's New Mail Sound depending on the time of day, i.e. I  set the new mail sound to be "Silent Alert" in the evening and restored it to my default new mail alert in the morning.  Any guidance as to what might be wrong and how to solve this problem would be very welcome.

 

 

iMac, Mac OS X (10.7.3)

Posted on Apr 16, 2012 1:36 PM

Close

Q: Mail Applescript "set new mail sound" -> Apple Event handler failed

  • All replies
  • Helpful answers

  • by Pierre L.,Solvedanswer

    Pierre L. Pierre L. Apr 16, 2012 2:46 PM in response to Jim Downward
    Level 5 (4,484 points)
    Apr 16, 2012 2:46 PM in response to Jim Downward

    The following script might act as a workaround:

     

    tell application "Mail" to activate

    tell application "System Events" to tell process "Mail"

        keystroke "," using {command down} -- open Mail preferences

        click button 1 of tool bar 1 of window 1 -- General

        click pop up button 5 of group 1 of group 1 of window 1

        keystroke "Glass" & return

        keystroke "w" using {command down} -- close Mail preferences

    end tell

     

    Since the above script uses GUI Scripting, you must enable the Accessibility Frameworks by clicking the checkbox labeled "Enable access for assistive devices" in the Universal Access System Preference pane.

  • by Pierre L.,

    Pierre L. Pierre L. Apr 16, 2012 3:50 PM in response to Jim Downward
    Level 5 (4,484 points)
    Apr 16, 2012 3:50 PM in response to Jim Downward

    Here's an improved version of my previous script:

     

    set myDefaultNewMailAlert to "New Messages Sound" -- enter the sound of your choice

    set theEvening to 18 * hours -- 6 PM (adjust to fit your needs)

     

    set volume with output muted

    tell application "Mail" to activate

    tell application "System Events" to tell process "Mail"

        keystroke "," using {command down} -- open Mail preferences

        click button 1 of tool bar 1 of window 1 -- General

        click pop up button 5 of group 1 of group 1 of window 1

        if (time of (current date) > theEvening) then -- no alert in the evening

            keystroke "None" & return

        else

            keystroke myDefaultNewMailAlert & return

        end if

        keystroke "w" using {command down} -- close Mail preferences

    end tell

    delay 2

    set volume without output muted

     

    Message was edited by: Pierre L.

  • by Camelot,Helpful

    Camelot Camelot Apr 16, 2012 3:55 PM in response to Jim Downward
    Level 8 (47,305 points)
    Mac OS X
    Apr 16, 2012 3:55 PM in response to Jim Downward

    This appears to be a bug. Workarounds notwithstanding, it should be reported at http://bugreporter.apple.com/

  • by hubionmac,Helpful

    hubionmac hubionmac Apr 16, 2012 4:13 PM in response to Jim Downward
    Level 2 (435 points)
    Apr 16, 2012 4:13 PM in response to Jim Downward

    Instead of using AppleScript-Commands or GUI-Scripting you could edit Apple Mail's Preferences by using defaults write to set another MailSound

    defaults write com.apple.mail MailSound "/System/Library/Sounds/Tink.aiff"

    This is a quick and direct way... to be 100%sure that this setting is enabled right away, I would restart Mail or open/close the prefs window quickly to make Apple Mail realize the changes in the plist

  • by Jim Downward,

    Jim Downward Jim Downward Apr 17, 2012 6:57 AM in response to Jim Downward
    Level 1 (10 points)
    Apr 17, 2012 6:57 AM in response to Jim Downward

    Thanks for the variety of suggested workarounds.  I have also filed a bug report.  I had hoped that the error I encountered would not turn out to be an Applescript bug but rather would be a documentation error caused perhaps when the Lion rewrite of Mail changed the property name.  In that event, all that would be needed would be to find the new, and correct, property name.

  • by Pierre L.,

    Pierre L. Pierre L. Apr 17, 2012 7:05 AM in response to Jim Downward
    Level 5 (4,484 points)
    Apr 17, 2012 7:05 AM in response to Jim Downward

    Thanks for your feedback.

  • by Jim Downward,

    Jim Downward Jim Downward Jul 31, 2012 2:23 PM in response to Jim Downward
    Level 1 (10 points)
    Jul 31, 2012 2:23 PM in response to Jim Downward

    After installinging Mountain Lion, I tested this script again.  The error persists.  Mid April, 2012, I filed a bug report on Lion and discovered it was a known bug.  One day, maybe the bug will be fixed.