Struggling to change the sort view of Mail using AppleScript

I’m a long time owner (just upgraded to V8) though I rarely actually use it so I struggle sometimes to figure out how to write a script given the dictionary.

Right now I’m trying to create (what I thought would be) a trivial script to change the sort view of AppleMail from Date Received to From and so forth but I’m having zero luck getting this to work.

I did however notice that having written

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application "Mail"
	activate
	set viewer to the front message viewer
	set sort column of viewer to from column  -- this fails
end tell

I see numerous errors (see screenshot) that seem related to accessing the columns but I have no idea what to do.

Suggestions would be very much appreciated. I’m running Mail on Big Sur

1 Like

Hey David,

Welcome to the forum!   :sunglasses:

Your script works for me on Mojave.

Here’s one I’ve been using for years:

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2015/10/04 00:29
# dMod: 2021/08/01 18:09
# Appl: Mail
# Task: Sort Messages on From-Column { toggle ascending/descending }.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Mail, @Set, @Sort, @Column, @Date, @Sent
# Test: macOS 10.12.6 Sierra through macOS 10.14.6 Mojave
# Vers: 1.00
--------------------------------------------------------

tell application "Mail"
   tell (some message viewer whose index is 1)
      
      if sort column = from column then
         set sorted ascending to not (get sorted ascending)
      else
         set sort column to from column
         set sorted ascending to true
      end if
      
   end tell
end tell

--------------------------------------------------------

-Chris

Chin up. The good news is there doesn’t appear to be anything you can do.

While there is a sort column property, the :pencil2: with a red line crossed through it, I believe, is to indicate that the property is read-only.

The error that appears in the column to the right for that property, I believe, is to indicate that it can’t even do that.

The error code is -10000, that’s AppleScript saying “well, if you don’t know, then I’m not going to tell you!”


Then…

Err… Now I’m confused. What’s with The Crossed-Out Pencil ? @ccstone @alldritt @ShaneStanley @god ?

Did AppleScript just do something it’s not supposed to be able to do ? Is this the mirror universe ?

One poster says Mojave and the other says Big Sur. Those properties also seem to be broken in Catalina.

tell application id "com.apple.mail" -- Mail.app
	sort column of message viewer 1
end tell
--> Mail got an error: AppleEvent handler failed. -10000

Script Debugger 8.0.1 (8A36) on macOS 10.14.6

Apple Mail 12.4 (3445.104.21)

So basically, this just isn’t possible?

Yep – it’s a bug in Mail.

Sigh — Apple Q/A has really gone downhill over the last few years.

Thanks

Hey David,

It’s been a long time since Apple’s Q/A was good, and they’ve been failing to fix AppleScript bugs in Mail for well over a decade.

Please take the time to complain: Feedback - Mail - Apple

The appended script works with Mail (Mojave) but may require tweaking for other versions of macOS.

On my system it’s a little sluggish activating “Oldest Messages on Top”, but it works – and it will toggle between Oldest and Newest if Date is already selected as the sort.

I’m using FastScripts to run my script.

Keyboard Maestro is another option. It runs AppleScript slower than FastScripts, but then again you don’t have to write AppleScript to work with (most) menu items.

-Chris

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2021/08/04 09:56
# dMod: 2021/08/04 09:56 
# Appl: Mail
# Task: Sort by Date ⇢ Oldest Message on Top (Toggle).
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Mail, @System_Events, @Sort, @Messages
--------------------------------------------------------

tell application "System Events"
   tell application process "Mail"
      tell menu bar 1
         tell menu bar item "View"
            # perform action "AXPress"
            tell menu 1
               tell menu item "Sort By"
                  # perform action "AXPress"
                  tell menu 1
                     tell menu item "Date"
                        perform action "AXPress"
                        
                        # if value of attribute "AXMenuItemMarkChar" ≠ "✓" then
                        # 	perform action "AXPress"
                        # end if
                        
                     end tell
                     
                     repeat until exists of menu item "Oldest Message on Top"
                        delay 0.05
                     end repeat
                     
                     tell menu item "Oldest Message on Top"
                        if value of attribute "AXMenuItemMarkChar" ≠ "✓" then
                           perform action "AXPress"
                        end if
                     end tell
                     
                  end tell
               end tell
            end tell
         end tell
      end tell
   end tell
end tell

--------------------------------------------------------

Well, first of all, I’m glad to know that problem was not due to my inability to deal with AppleScript.

I knew about the mechanism to control the GUI from AppleScript but jiust didn’t think I’d have to go down that road (sigh)

Thanks everyone for the responses, I really appreciate it.

What does “perform action 'AXPress” do? Mail 14 on my Big Sur doesn’t seem to like that command.

I get the message “Script Debugger is not allowed assistive access” but I do have Script Debugger in the list of allowed apps for Accessibility

Selects the given item as though you clicked it.

Look under Automation:

B.S. doesn’t always ask properly for assistive access.

The dialog flashes by, but you don’t get a chance to interact with it.

I have remote access to a friend’s MacBook Air with Big Sur, and I’ve had this problem a number of times already…

Sometimes rebooting will get the preference selector working again – sometimes not.

I have not seen a definitive fix anywhere… (Bad Apple!)

@ShaneStanley & @alldritt?

-Chris

Ah yes, completely obvious from the name :roll_eyes:

Yeah, I had already noticed Script Debugger was not in the list but even after rebooting and trying the script again, I was not prompted to add it to the Automation menu. There doesn’t seem to be a(n easy) way to manually add an item to that menu.

It’s supposed to be easy. A dialog is supposed to show up when you run such a script that allows the user to approve or deny.

When that works properly it’s easy – when it doesn’t it makes you pull out your hair.

-Chris