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
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.
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.
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.