I have achieved an improvement to GUI scripting as a solution for strike through text in Apple Mail. The following Automator service solution has been tested successfully on the following releases of OS X, and their Apple Mail clients without any code changes:
- 10.7.5 Lion
- 10.8.5 Mountain Lion
- 10.9.5 Mavericks
- 10.10.1 Yosemite
Example output:
The solution invokes a Bash shell script from AppleScript. This script massages the raw RTF code derived from what is on the clipboard, and injects the RTF syntax for strike through surrounding your selected text from Apple Mail. It then puts this back on the clipboard, where it is subsequently pasted back into Mail — replacing the original selection. This script is application agnostic, but the results will only work where rich text is supported. If you want to undo the strike through effect, press command+Z to undo it.
The Bash script only needs to be made executable, and preferably locked via Get Info, so you do not accidentally delete it. It can live anywhere you want, providing you synchronize this with the do shell script that launches it, and it is in your search path. Think of the shell script part of this solution as a black box — something you don't need to change, or necessarily understand.
I tried several ways to incorporate the Bash script code in Apple Script for a single file solution, and though it compiled cleanly, it never produced the desired strike through result, until I placed that code in the separate Bash script.
Here is the Bash Script code. Copy/paste into a programmer's editor, not a Word processor, as this must remain pure text with no embedded junk.
#!/bin/bash
#
# rtf_strike.sh
#
# get the raw RTF code from the clipboard, retaining all of the original
# copied selection characteristics (e.g. font, color, size, etc.). Use Perl to
# massage the RTF code, and sed to insert the RTF strikethrough operator
# to produce the strike-through effect on the original selection. Toss it
# back on the clipboard when done.
# Reference
# http://stackoverflow.com/questions/2545289/getting-rtf-data-out-of-mac-os-x-pasteboard-clipboard
/usr/bin/osascript -e 'the clipboard as «class RTF »' | \
/usr/bin/perl -ne 'print chr foreach unpack("C*",pack("H*",substr($_,11,-3)))' | \
/usr/bin/sed -e 's/\\cf/\\strike\\cf/;$s/}$/\\strike0}/g' | /usr/bin/pbcopy
exit 0
In Terminal:
chmod +x ~/rtf_strike.sh
Quit Apple Mail.
Launch Automator, and choose new Service. Service Receives selected Rich Text in Mail. Leave Output replaces ... unchecked.
- Library > Utilities > Copy to Clipboard — drag/drop into larger workflow window
- Library > Utilities > Run AppleScript — drag/drop into larger workflow window
- Select all contents and remove
- Add AppleScript per Automator screen capture below, and then compile (hammer) it.
- Save Automator Service with name MailStrike (so it is obvious in your Services submenu)
In the Mail compose window, select the text that you want to apply the strike through style, then apply the service via Mail > Services > MailStrike.