Macro: Extract Found Image Image and Replace with Path

Macro: Extract Found Image Image and Replace with Path

Author: Pariah Burke @iampariah on KM Forums

Version 0.1

This macro is under construction and open to improvement. Please feel free to suggest improvements or contribute your own on the thread in the KM Forums.

————————————————————

RAISON D'ÊTRE

————————————————————

I love Found Image actions, but they have a significant cost if you embed the images directly in your macros. (More info, more info, even more info). But, if you've already embedded the images--such as, if you've been using KM since before version 9--it's a lot of work to convert embedded images into external images and paths for Keyboard Maestro to use. This macro is my beta version of a way to significantly reduce the amount of effort to make that conversion.

————————————————————

INTRUCTIONS

————————————————————

  1. Change the variables in red to match your system

  2. Search in KM Editor for “found image”. This is essential to get the blue striping of finding the Found Image actions.

  3. In a Found Image action, click on the image to highlight it.

  4. Execute this macro.

————————————————————

WHAT WILL HAPPEN

————————————————————

  1. The name of the macro you’re editing will be saved to a variable.

  2. The image in the Found Image action will be copied.

  3. Preview will be launched, a new document created with that Found Image image as its content.

  4. You will prompted for the filename of the image to save.

  5. The image will be saved to the path and filename you selected (in the red variables as well as in the user prompt).

  6. Back in KME, the Image will be replaced with a Path condition.

  7. The Path field will be set to point to the newly extracted image.

Extract Found Image.kmmacros (205 KB)

5 Likes

This is a good idea! I no longer have any images embedded directly in my macros, but this makes me want to throw some into my scratchpad macro just to test it out. Thanks for building and sharing this!

-Chris

2 Likes

I used to place images embedded directly into macros, but I had so many of them (hundreds) that it bogged down the KM Editor app because that app writes the entire set of macros to a file about one second after the keyboard is idle. That's a lot of writing, causing a lot of lag in the KM Editor. It was very annoying.

So now I tend to save my images to files instead, but if you do that you have to be careful because screen scaling can totally change your images' resolutions. I have a 4K monitor, and worse, I change the scaling in the System Preferences to a non-standard value. This setup is a frequent cause of accidental re-scaling of images when storing images in files, especially when using the MacOS Preview app. When the image file is loaded by the KM macro, the image can be scaled to a totally different size and the KM Engine is unable to find the image. But if you do it right, it's an effective method.

Sometimes I load dozens of images into a folder, and my KM macro loads each of the files into a Find Image action and stops when it finds a hit. In that case, the name of the file could be used to correspond to the name of the thing that was found, and my macro's next steps could be based on the file's name. So for example if a file contained the image of an OK button, I could name the file "OK Button" and when that file matched something on screen, my macro would know what it found.

When preparing dozens of images for storing into a file, what I do is create a macro that gets triggered on a System Clipboard change, and then I press the MacOS key that lets me collect an image with the mouse, and when it gets copied to the system clipboard, the macro recognizes that a new image was in the clipboard, and it saves the image into the required folder, using %RandomUUID%.jpg as the filename. Once I've collected all the images I need, I manually change the filenames. However now that Monterey has really good voice recognition, I should be using my voice to specify the file name.

@iampariah thanks for sharing your macro and also @cdthomer for the information, the influence on the KM editor, regarding the size of the screenshots.

I changed the macro a bit for me, so that first the desired screenshot and then the replace with path is done.
The filename also contains a date/time stamp in addition to the macro UUID, in case I need multiple "Click a Found Image" actions in one macro.

Here is an example:

2022_02_19_Support_1

15)ExtractShot <7C0F 220219T022223>.kmmacros (68,3 KB)

Click to show image

2 Likes

I've managed to remove the need for Preview by using the Write to a File action instead. Much neater.

This creates a folder in a parent directory ("Found Images" in this case), named according to the currently selected macro. Fortunately, subsequent runs within the same macro appear to add images to this folder rather than create it anew. I'm not looking a gift horse in the mouth!

It then saves the image selected in KM as a .png and names it according to the action name and action ID. This is great (for me at least) because it differentiates the image from others that may be in identically named actions, but also means that if you replace the image in the image well and run this again, it will overwrite the previous file (as the action ID doesn't change), unless of course you rename the action.

What I haven't managed to figure out yet is how to get the group name in order to create a more easily useable folder hierarchy.

Found Image - Image to Path.kmmacros (39.7 KB)
(Updated: Now works with "/" in Macro/Action names)

Screenshot

This will preview the selected action's image path in a floating window in KM, for use with troubleshooting:

Display Image at Path.kmmacros (40.8 KB)

Screenshot

3 Likes

If for some reason you want to undo the process and get an image back in its well, then here's the opposite macro. As with the Display Image macro, click the path before triggering.

Found Image - Replace Path with Image.kmmacros (31.9 KB)

Screenshot

2 Likes

@noisneil thanks for the macros. They work great for me. Here is an example of how I use them on my end:

Video

I have added your "Found Image - Replace with Path" to my screenshot macro.

02)ExShot <7C0F 220303T232027>.kmmacros (36,5 KB)

Click to show image

1 Like

Great demo! Very smooth! :clap:t3:

One thing I noticed that I haven't found a solution for is... If you have an If/Else action with two or more image conditions and try to convert the images to file paths, the second will overwrite the first. Fortunately I realised before deleting the images.

EDIT: removed incorrect info about unique files that resulted from my misunderstanding of how it worked.

Ok one more issue. If the macro name contains a forward slash, the Replace with Path macro doesn't work. Is this because it thinks it's a regular expression? (I know nothing about them!)

Any suggestions much appreciated.

I can tell you that regular expression don't use the forward slash character, just the backwards slash character. What you might be thinking of are the delimiters that sometimes surround/separate regular expressions, like this one which, if I've written it correctly, changes tabs to spaces:

s/\t/ /g

Thanks! Any suggestions about how to ensure the slash is included as plain text in the variable?

I actually wrote a macro to search the KM plist file for any embedded file paths and tag them so I could keep track of which files (mainly images and AppleScripts) that are currently in use in my macros. I've been using it quite extensively for a little over two weeks and it has helped me clean up my files. Check it out, it might help you keep track of which images are being used and which ones aren't.

Find Which Files Are Used in Your Macros and Tag Them Accordingly

2 Likes

Fixed the slash thing. No clever regex magic, just loads of actions. I may be dumb, but I get it done. Updated above.

2 Likes

Does anyone know if macro icons contribute to the size of the plist, or are they stored elsewhere? I'm just wondering if some of my hastily acquired screenshots might be a leeetle bit bigger than necessary.

Hey Neil, yes they do if they're custom images. For instance, see my scratchpad macro and the difference between not using a custom icon at all, using an icon from the KM icon picker, and using a custom image (whose actual file size is only 15KB).

No custom icon screenshot: plist size 3,786,778 bytes (click to expand/collapse)

Custom icon from KM icon picker screenshot: plist size 3,786,802 bytes (click to expand/collapse)

Custom icon from 15 KB PNG image: plist size 3,797944 bytes (click to expand/collapse)

Thanks for that. I went through and set a load of icons yesterday to help me identify macros quickly and all of a sudden found images are laggy. Possibly unrelated but it's given me pause.

@peternlewis, is there any need for these to be in the plist? Could they be stored elsewhere and referenced in the way we've been describing for found images in this thread? Perhaps it could be an optional thing. "Offload all images" in preferences.

1 Like

My KM plist is 4.3MB. Is that considered large? If not, then maybe something else is going on with found images (other than them being broken in Brave, but I digress...).

Having had a look at a fair few of the icons in my macro set, I don't think it matters If you use large images, as they seem to resize to 64x64.

Not at all. There's plenty of users here who have much larger plist files. Mine used to be over 9MB before I replaced all my found images from embedded images to image file paths.

1 Like

Almost certainly entirely unrelated.

Not particularly.

The plist size will not affect the performance of macros. Only the editor will be affected by a large plist size or by large macros.

Yes.

No.

This would break all the macro syncing.

1 Like

Has anyone given any thought to doing the same thing with AppleScript text actions and converting them to compiled script files? Apparently this can also speed up the editor and cut down on AS run times.

I've got as far as being able to save the text inside the selected Execute an Applescript action as a .scpt file using the Write to a File action. As far as I can tell, this is ok to do, as the resultant file does run.

I'm at the stage where I want to replace the existing AS Text with the new script file, but I'm not sure how to change the action to "Execute Script File" while the KM editor is focused on the AS text field.

Any suggestions?

Convert AS Text to Compiled Script.kmmacros (45 KB)

Macro screenshot

1 Like