Importing Mail signature from a file

mail_apple
applescript

#1

For many years, I had a script that set my .signature file and then set a signature in Mail to the contents of that file. It worked perfectly until 10.12 when Apple broke the AppleScript command that let me set the signature. I have found posts on how to SELECT a signature with KBM, but not on how to import from a file. I don’t care if that Signature in Mail gets set or not, as long as I can read and append my .signature to the email I am writing. It would be great if it was smart enough to REPLACE a signature that was already there, but that’s def not needed.

I am wondering if I can do that same thing with keyboard maestro somehow?

For reference, this is the script I ran for many versions of OS X:

#!/bin/bash
 SIGHOME="$HOME/.signature"
 $HOME/bin/fortune $HOME/mysigs > $SIGHOME

 MAIL=`ps -U"$USER" -co command | grep "\bMail\b"`

 if [ "$MAIL" ]; then
      cat $SIGHOME | sed -e 's/"/\\"/g' > $HOME/.mail_signature
      MYSIG="$(<$HOME/.mail_signature)"
      osascript <<EOF
           tell application "Mail" to set content of signature "Fortune" to "-- " & return & "$MYSIG"
      EOF
 fi

 exit 0

#2

I noodled about a bit and got this, which I think should work, but it is pasting the signature in the “to” field (that is, the script is not moving to the mail body area and may or may not be executing the “end” keypress.


#3

Well, replying to myself again. I got a script that works. I tried adapting it to invoke on Command-R (sending a command-R to mail as the first action, but that blew up badly). Still, this works, using a named clipboard to store the file contents, then using a “type characters return-dash-dash-space-return” and then pasting the named clipboard.

tell application "System Events"
   tell application process "Mail"
      delay 0.1
      set frontmost to true
      set value of attribute "AXFocused" of UI element 1 of scroll area 1 of front window to true
   end tell
   delay 0.1
   tell application "System Events" to key code 125 using command down
   delay 0.1
end tell

The script tends to fail if Mail is in full screen mode, but poking around it seems that FSM is the enemy of all scripting, so I don’t think this is something KMP could fix.


#4

Hi @lbutlr,

This version should let you use the macro to replace Mail’s default ⌘R shortcut without things blowing up, and in my tests it also works equally as well in both windowed mode and fullscreen mode:

Reply with Signature.kmmacros (3.2 KB)


#5

Oh, silly me, I didn’t think of the obvious solution of using the menu, I was trying to send a ⌘-R.

Your script works without the AppleScript to select the message body, move the cursor to the end and then paste the signature?

OK, never mind. I see what you did. I ended up with this script, which is temporary until quotefix gets fixed for the new macOS beta, it works as long as I don’t leave my finger on the command key after hitting ⌘-R.


#6

Just in case it’s not known, shortcuts can be created in System Preferences > Keyboard > Shortcuts for switching among signatures that are saved in Mail preferences. For a better explanation, see this piece by David Sparks which describes setting shortcuts for moving amongst different send-from addresses. The same method works for signatures.


#7

yes, but my fortune file has over 1,000 random sigs, and I already create .signature for Usenet posts. There’s no way I am going to try to manually manage 1000+ signatures in Mail.app.