Getting Started Hiccups - Starting With the Create PDF of Mail Messages With Useful Names

I am a couple days into a trial of KM. So far mostly failures but with a few glimmers of hope. I got the applescript that Sparky posted that retrieves sender, date, subject etc info and concatenates into a filename for a PDF version working. Properly named PDFs were being created so I moved on to adding another script to the macro that would move the emails to other folders. Things started going down hill in a hurry this morning. The macro started throwing errors and I started simplifying back trying to get to a point where any part of the script worked. It went from throwing errors to losing all the color in the window. Since the standalone applescript still builds the file name properly, I am wondering if the free trial expired. I am running Big Sur on MBP 2018.
The script that runs as a standalone script but fails inside KM:

--clear variables
set msgSourceDay to {}
set msgSender to {}
set msgRecipient to {}
set msgSubject to {}

tell application "Mail" to if running then
	repeat with tMsg in (get selection)
		--get information from Mail about the message
		set msgSourceDay to the (date received of tMsg)
		set msgSender to the (extract name from sender of tMsg)
		set msgRecipient to the (name of first to recipient of tMsg)
		set msgSubject to the (subject of tMsg)	
	end repeat
end if
--Set message date and time in ISO Format
set msgMonth to rich text -1 thru -2 of ("0" & (month of msgSourceDay as number))
set msgDay to rich text -1 thru -2 of ("0" & (day of msgSourceDay as number))
set msgYear to year of msgSourceDay

--Set message Name text string
set newName to "Emails " & msgYear & "-" & msgMonth & "-" & msgDay & "- Sender: " & msgSender & "- Recipient:" & msgRecipient & "-" & msgSubject

display dialog newName

--Set Keyboard Maestro Variable
tell application "Keyboard Maestro Engine"
	setvariable "New Name" to newName
end tell

Hi @Pam

It is always good to upload the actual Keyboard Maestro Macro you are having problems with to the Forum. Much quicker for others to test and see if they can find why it doesn't work. Here's how to do that:

https://wiki.keyboardmaestro.com/Forum#How_to_PostUpload_Your_Macro_to_the_Forum

As Keyboard Maestro is designed as a modular system with Actions simply added one after the other and as many native Keyboard Maestro Actions can do similar things to AppleScript (with the advantage that they are already bug free and are easy to customise) you might find that some of your script can be replaced with Keyboard Maestro Actions. Just a thought.

Giving the suggestion a try...

Moderator Edit: I removed all of the below lines as they clearly are not what you intended to upload. Please see

How to upload your macro. Instructions for uploading to an existing post are just below the main instruction.
You may also find this Macro useful: MACRO: Upload KM Macro to New or Existing Topic @KM [Pub]

Execute an AppleScript Action (v9.2)

Execute an AppleScript.kmactions (1.7 KB)

Clearly you were not able to upload your macro, and included a lot of other text from a forum page that was unrelated to your upload. So I removed that text.

Also, you did NOT upload your Macro, you uploaded only one Action.
Before you start the upload process, you must first select the Macro in the Macro panel/column.

Perhaps this video would be helpful to you:
See this YouTube video: How To Upload Macro to Existing Post in Keyboard Maestro Forum

If you are still having trouble, just let us know with specific questions/problems, and we'll try to help.

The best way to start using Keyboard Maestro is to start simple:

If you try to run before you can walk, then it is likely to lead to a lot of frustration, in the same way if you opened Microsoft Word for the first time and tried to start writing your dissertation, you would find it very frustrating figuring out how to set up style sheets and bibliographies and such.

Hey Pam,

Your AppleScript would not compile.

Always compose AppleScripts in the Apple Script Editor or better yet Script Debugger.
(The demo reverts to its freeware “Lite” version after a 30 day evaluation period, but is still enormously superior to the Script Editor).

Once you have a script compiling and working in a proper development environment, you can paste it into your Keyboard Maestro macro.

Mail has a bad habit under certain circumstances of turning the noun “text” into “rich text”, and this will usually fail to compile.

This compiles and works in Apple Mail on Mojave:

tell application "Mail" to if running then
   
   repeat with tMsg in (get selection)
      
      --get information from Mail about the message
      set msgSourceDay to the (date received of tMsg)
      set msgSender to the (extract name from sender of tMsg)
      set msgRecipient to the (name of first to recipient of tMsg)
      set msgSubject to the (subject of tMsg)
      
      --Replace Teresa's name with TC
      if msgSender is "Pamela Parks" then set msgSender to "PP"
      if msgRecipient is "Pamela Parks" then set msgRecipient to "PP"
      
   end repeat
end if


--Set message date and time in ISO Format
set msgMonth to text -1 thru -2 of ("0" & (month of msgSourceDay as number))
set msgDay to text -1 thru -2 of ("0" & (day of msgSourceDay as number))
set msgYear to year of msgSourceDay

--Set message Name text string
set newName to "Emails " & msgYear & "-" & msgMonth & "-" & msgDay & "- Sender: " & msgSender & "- Recipient:" & msgRecipient & "-" & msgSubject

--Set Keyboard Maestro Variable
tell application "Keyboard Maestro Engine"
   setvariable "New Name" to newName
end tell

I don't know why you're using a repeat loop, when you're not accumulating anything – only the last selected message will give you any data...

-Chris

Hi Pam, welcome to Keyboard Maestro (KM) and its Forum.
KM is one of the best Mac automation tools available, its Forum is one of the best and friendliest forums on the Internet. Whenever you reach a tough stumbling block trying to use KM, please feel free to post your question/problem here for help.

You will also find this helpful:
Tip: How Do I Get The Best Answer in the Shortest Time?

I found it difficult to understand the exact problem you are having. So following the above tip should help quite a bit.

Whenever you present a problem with your macro, please:

  1. Upload your Macro image and file
  2. If you get any errors, or unexpected results, post a screenshot of those.
  3. Post the expected result of your macro
  4. Provide the versions of KM and macOS you are running.

As @peternlewis mentioned above, you may be trying to write a complicated macro before you are ready to do so. One good approach is to write separate macros for each part of your workflow, so that you can easily test and confirm the results before moving on to the next step.

Getting Started with Keyboard Maestro

For more help, see Getting Started with Keyboard Maestro and the Forum .

Plenty of good advice in here that probably bares repeating even though it may not have much to do with my situation.
Early on I got the applescript portions of my macro working as stand alone scripts.... and indeed these scripts were not my first. They followed working through several dozen scripts from the now dated but useful texts by Sanderson and Rosenthal. And the KM macro I attempted to post was not the first attempted.... I got through several examples and tutorials before modifying Sparky's Macro that makes PDF of Mail messages. I did get his macro to work briefly and was in the process of tweaking it for my situation when things started failing. No amount of reverting to earlier versions would get his Macro working again. The major impediment in me making further progress is that at this point KM is not functioning at all. My guess is that the trial period ended. Less than a week seems short but I don't recall seeing what the trial limit was. The effort has certainly not been wasted as I was able to finish up the applescripts and they do the job. Not nearly as slick as could be done with KM but functional. At this point my best way forward may be to either procure another trial period or just wait and see what the Shortcuts application will offer later this year.

1 Like