Sorting mail rules alphabetically

Hello I ma trying to sort my mail rules alphabetically without success.

I can collect them in a list then sort them but cannot have mail window to show the new list.

Can anyone help here is the part script that is working?

tell application “Mail” to set the_list to (get name of every rule)
set sorted_list to my shell_sort(the_list)
my quick_sort(the_list, 1, the_list’s length)
on shell_sort(the_list)
set new_delim to {ASCII character 10}
tell (a reference to my text item delimiters)
set {old_delim, contents} to {contents, new_delim}
set {the_list, contents} to {“” & the_list, old_delim}
end tell
return paragraphs of (do shell script “echo " & quoted form of the_list & " | sort -f”)
end shell_sort

on quick_sort(a, l, r)
–this sorts the list in place, no need to return anything
local i, j, v
script o
property p : a
end script
set {i, j} to {l, r}
set v to o’s p’s item ((l + r) div 2)
repeat while (j > i)
repeat while (o’s p’s item i < v)
set i to i + 1
end repeat
repeat while (o’s p’s item j > v)
set j to j - 1
end repeat
if (not i > j) then
set o_s_p_s_item_i to o’s p’s item i
set o’s p’s item i to o’s p’s item j
set o’s p’s item j to o_s_p_s_item_i
set {i, j} to {i + 1, j - 1}
end if
end repeat
if (l < j) then quick_sort(o’s p, l, j)
if (r > i) then quick_sort(o’s p, i, r)
end quick_sort

Mail’s dictionary could tell you whether it knows how to sort its rules…

Why are you sorting the same list twice?

As Alastor933 says. You are sorting your list twice?

Also i am not sure why you think Mail would show you the sorted list.
The only thing you have asked mail to do is get a list of rule names.

And then you sort that text list. And do nothing with it?


sorting a list is not always a good idea.
Be aware that the mail rules are processed in the displayed order.
If one of your rules contains a “full stop”, any consecutive rules are not processed.

Hello alastor933,

Thanks for your reply but i could not find in mail dictionary any information on sorting mail rules.

So finally i look on the internet if I could find some existing scripts that was close to what I wanted to do and found this piece of code that I adapted to mail and I am aware that I am sorting the list twice but do not know how to break it to make only one sort and since on running it it worked I kept it that way!

Hello mark hunte,

I know that I just called the list and do nothing with it and this is why I posted here this piece of script to see if anyone could help me finishing it as I am stuck! I cannot find a way for sorting it in mail so that it stays there each time I run the script.

I want to sort my mail rules alphabetically as I set new rules everyday but I have a hard time to organise them.

Hello StefanK my rules are only to collect some emails and move them to a desired folder without any stop or other further action to them so for me it is more appropriate to have them organised alphabetically.

I hope I answered everyone and that someone can help me finish this piece of script!

The dictionary contains everything Mail can do. If it contains nothing about sorting, then Mail cannot sort.
Maybe you can do it by hand?

Just remove one of the calls; that is, one of the my bits on lines 2 & 3.
Then remove the now useless subroutine/handler; its name follows the “my” in the bit you deleted in step 1.
The handler begins with on and ends with end .
To catch the result of the sort you must keep the set sorted_list to part from line 2.

I suggest you read some of stuff in MacScripter’s “Unscripted” section.

Thanks alastor933 for your reply

I have cleaned my original script with only one sorting but still cannot figure out how to replace my old mail rules with this new one?

Here is my new script

tell application “Mail” to set rule_list to (get name of every rule)
set text item delimiters to {ASCII character 10}
set rule_list to rule_list as string
set rule_list to paragraphs of (do shell script “echo " & quoted form of (rule_list) & " | sort -f”)
set text item delimiters to “”
return rule_list

The only way to get your sorted rules I can think of is re-creating all rules from scratch, in alphabetical order.
The Dutch call this shooting midges with a cannon - not worth doing.

Thanks alastor933

This is bad news but luckily I am already doing the manual sorting but I wanted to be more efficient and using a script for that…

Anyway thanks for your time. Cheers.