The scenario is when I first enter the expression it doesn't trigger in finder.
(It runs from a barcode scanner and I use it to rename files.
I select the file press return and then scan, here it should trigger and copy the trigger value to the clipboard. It's here it doesn't trigger.
However if I do the same but before scanning type an underscore and then cursor back and scan it will trigger.
Is this a hardware barcode scanner or a software barcode scanner getting images from previously scanned images?
Are you pressing those keys while inside an app, or inside Finder? We may need to know what application you are using at that moment.
We can see your macro in the image fine, but to get the best support it's better to upload the actual macro so people don't have to retype your macro to conduct tests. In your case here, it might actually be better to create a short video of your process and upload the video so we can see how it works, when it actually does work. Maybe we can spot a difference in your "process" which could be causing the issue.
I don't see anything wrong with your macro, but I'm unsure how you know that the macro didn't trigger. Was it just the lack of a submarine sound, or did you check the log?
I'm not the best person on this site for spotting problems, but I do try to help. Maybe other people can spot the problem quicker than I can.
So I have a 2D QR scanner, I scan a code that is the to be the name of the file.
The device is recognised as keyboard so essentially the same as using a keyboard.
So while in Finder I select the file, (highlight it) tap return this has now put the file into a positions to be renamed, where the current name is highlighted. So this is all within Finder
I'll upload the macro again I don't think it went through before. Video to follow) Barcode Scanner QR.kmmacros (21.8 KB)
As an initial experiment, I changed the macro group's availability to "all applications", and typed a matching string into a text editor. As soon as I hit return, your macro worked (I actually added an Alert action to be sure!).
Next I tried your macro when entering the name of a folder. This time, typing a matching string did not trigger the macro – but that seems logical. The regular expression ends in $, which indicates (in this context) the end of the line. When renaming a file or folder in the Finder, the end of the line is signalled by hitting the Return key (or the Enter key). Once that key has been hit, the text editing field is closed – so there is no newline to match the end of the macro trigger.
If you remove the closing $, the macro works when naming in the Finder... however, it doesn't seem very reliable.
You could try a different workflow in your macro, such as pasting the %TriggerValue% into the filename.
Hopefully you can follow this in the first scan you then see me attempt to paste the trigger value and it has not worked. I undo this. and then repeat but in the second attempt I use the _2 then when I paste it has worked. It can be any character but that will trigger the macro.
In the trigger, ^ and $ never make any sense in this context.
^ means start of line or start of text, but it does not make sense when matching the typing buffer, and $ will always be the case as the string is always matched against the end of the typing buffer.
Remove both of those and then see if the match works reliably.
^ by default means the start of the text, not the start of the line, so it would match against all the text in the text buffer (which would be cleared when you click, but then you press return, and so now there is an additional return in the text buffer).
Regardless, the regex matches against the end of the text buffer, and as far back into the text buffer as it needs to to get a match - so once you have typed something to match the string, it will fire.
Probably I should add a warning for regex there that start with ^ or end with $.