KM -- Execute Last Run Macro Whose Name Ends with "TM" Macro (v9.2)

KM -- Execute Last Run Macro Whose Name Ends with "TM" Macro (v9.2)

Update (2021/09/20 19:16):
After more learning about sed. I have further simplified the script.

DOWNLOAD Macro File:

KM -- Execute Last Run Macro Whose Name Ends with "TM".kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


Hey Martin,

What are you trying to do with $d in:

| sed -En '
   $d 

-Chris

To be honest, I don't know. It was from your original macro. I did not understand this. But since I got the desired result. I assumed it was necessary to keep it. :joy:
Can you explain why you put it there? :joy::joy:

Eh?

Ah, so it is.

Hmm... Grey cells switching ON...

$d deletes the last line of the text first thing.

$ ⇢ Represents the last line of the file/string.
d ⇢ Is the command for delete.

I can't see that $d has any usefulness in this context, and I don't recall what I was thinking when I wrote the script.

I might have been trying to delete the training linefeed Unix files normally have, but I’m pretty sure that wouldn't matter at all in this context.

-Chris

1 Like

Ah. I see. That was because you wanted to exclude the current macro. I was a blind follower, haha. It was mainly because I did not comprehend the script (I still don't understand the sed command there).

After many tries and fails, I managed to accomplish with tail, grep, awk, grep, tail. (Since I only want to find the target macro, I don't have to restrict myself to the last 10 macros. I just use the last, say, 40 lines, of the engine log.


I like this version more and have replaced the one in the OP with this one.

Ah, so. You're exactly right.

| sed -En '
   $d
   /Execute macro/{
      s!^([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}) Execute macro “(.+)”.+!\1  ⇢  \2!
      p
   }
' \
-E ⇢ Extended Regular Expressions.
-n ⇢ Suppresses the printing of every line parsed.

/Execute macro/ ⇢ Find the given pattern.

{ … } ⇢ Perform the contained command(s) on each line found by the find command.

Search-and-Replace:

s!^([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}) Execute macro “(.+)”.+!\1  ⇢  \2!

p ⇢ Print the line.

Search finds lines of this format:

2021-09-20 17:41:15 Execute macro “Activate Last Application v1.00” from trigger The Hot Key ⌘Escape is pressed

Search-and-Replace transforms those lines to this format:

2021-09-20 17:41:15 ⇢ Activate Last Application v1.00

-Chris

1 Like

Thank you very much, Chris. Your explanation is extremely helpful! Now I can fully understand.

After further learning, I have been able to further simplify my macro. Now, I need only tail, sed, tail (see my updated post above):

grepString=$KMVAR_local__grepString
tailNum=$KMVAR_local__last_engine_log_lines
kmEngineLog=~/'Library/Logs/Keyboard Maestro/Engine.log'

tail -n $tailNum "$kmEngineLog" \
| sed -En "s/.* Execute macro “(.+${grepString})”.*/\1/p" \
| tail -1
1 Like