Skip to content

Instantly share code, notes, and snippets.

@flacle
Last active December 8, 2023 23:11
Show Gist options
  • Save flacle/183bc4e760951514564c to your computer and use it in GitHub Desktop.
Save flacle/183bc4e760951514564c to your computer and use it in GitHub Desktop.
Apple Script for the Mail App to sort email recipients alphabetically in the To, Cc, and Bcc fields. For when you don't wan't to give subliminal preference to coworkers ;)
(*
Author: Francis Laclé
Version: 1.0
Date: January 4 2016
Description: The purpose of this script is to sort email recipients alphabetically in the To, Cc, and Bcc fields.
Motivation: For cases when you don't want to give subliminal preference to coworkers in your organization ;)
Requirements: Apple Mail, OSX El Capitan
Installation and usage:
1) Use "Run AppleScript" as a new Service in Automator.
2) Choose "Service receives no input in Mail.app".
3) Copy & paste this whole script in Automator, replace any predefined template code.
4) Check in Automator the box: "Ignore this action's input".
5) Add both Automator and Mail as allowed assistive applications in System Preferences.
6) Use a defined keyboard shortcut (e.g. option + command + s) in Keyboard Preferences.
7) Restart your Mac.
Remark: This script was not thouroughly tested, so use at your own risk.
Perhaps there could be a better way, until then I opted for GUI scripting and Automator Services.
License:
The MIT License (MIT)
Copyright (c) 2016 Francis Laclé
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*)
--Talk with mail app through GUI
tell application "System Events"
tell application process "Mail"
set frontmost to true
-- clear the clipboard initially
my clearClipboard()
-- Focus to the To field
set value of attribute "AXFocused" of text field 1 of front window to true
keystroke "a" using {command down}
keystroke "c" using {command down}
delay 1
set addresseesSorted to my sortAddresseesThroughClipboard()
if addresseesSorted is equal to true then
-- delete the selection
keystroke (ASCII character 8)
-- paste the joined string of To addressees back
keystroke "v" using {command down}
my clearClipboard()
end if
-- Focus to the Cc field
set value of attribute "AXFocused" of text field 2 of front window to true
keystroke "a" using {command down}
keystroke "c" using {command down}
delay 0.25
set addresseesSorted to my sortAddresseesThroughClipboard()
if addresseesSorted is equal to true then
-- delete the selection
keystroke (ASCII character 8)
-- paste the joined string of To addressees back
keystroke "v" using {command down}
my clearClipboard()
end if
-- Focus to the Bcc field
set value of attribute "AXFocused" of text field 3 of front window to true
keystroke "a" using {command down}
keystroke "c" using {command down}
delay 0.25
set addresseesSorted to my sortAddresseesThroughClipboard()
if addresseesSorted is equal to true then
-- delete the selection
keystroke (ASCII character 8)
-- paste the joined string of To addressees back
keystroke "v" using {command down}
my clearClipboard()
end if
end tell
end tell
-- handler to sort the contents of a text field of an Apple Mail front window
on sortAddresseesThroughClipboard()
set addressees to (the clipboard) as text
if length of addressees is greater than 4 then
set addresseesSplitted to my explode(", ", addressees)
set addresseesSorted to my sortAlphabetically(the addresseesSplitted)
set addressees to my implode(", ", addresseesSorted)
set the clipboard to addressees as text
return true
else
return false
end if
end sortAddresseesThroughClipboard
-- handler to clear the contents of the clipboard
on clearClipboard()
try
delay 0.25
set the clipboard to ""
on error err_message
display dialog err_message
end try
end clearClipboard
-- handler to sort a list alphabetically
-- original: http://www.macosxautomation.com/applescript/sbrt/sbrt-05.html
on sortAlphabetically(theList)
set the indexList to {}
set the sortedList to {}
repeat (the number of items in theList) times
set the lowItem to ""
repeat with i from 1 to (number of items in theList)
if i is not in the indexList then
set thisItem to item i of theList as string
if the lowItem is "" then
set the lowItem to thisItem
set the lowItemIndex to i
else if thisItem comes before the lowItem then
set the lowItem to thisItem
set the lowItemIndex to i
end if
end if
end repeat
set the end of sortedList to the lowItem
set the end of the indexList to the lowItemIndex
end repeat
return the sortedList
end sortAlphabetically
-- handler to split a string on a specific delimiter
-- from: http://applescript.bratis-lover.net/library/string/#explode
on explode(delimiter, input)
local delimiter, input, ASTID
set ASTID to AppleScript's text item delimiters
try
set AppleScript's text item delimiters to delimiter
set input to text items of input
set AppleScript's text item delimiters to ASTID
return input --> list
on error eMsg number eNum
set AppleScript's text item delimiters to ASTID
error "Can't explode: " & eMsg number eNum
end try
end explode
-- handler to join a list using a specific delimiter.
-- from: http://applescript.bratis-lover.net/library/string/#explode
on implode(delimiter, pieces)
local delimiter, pieces, ASTID
set ASTID to AppleScript's text item delimiters
try
set AppleScript's text item delimiters to delimiter
set pieces to "" & pieces
set AppleScript's text item delimiters to ASTID
return pieces --> text
on error eMsg number eNum
set AppleScript's text item delimiters to ASTID
error "Can't implode: " & eMsg number eNum
end try
end implode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment