Removing Text from a variable

I'm currently using OCR with screen capture and then saving the results to a variable

But it is sometimes capturing letters or special characters which i don't want

Here is two examples

Screenshot 2023-03-02 at 07.05.45

Screenshot 2023-03-02 at 07.05.59

I only want the numbers, i don't want any letters or special characters.

Is there a way i can remove them?

i think it's also important to know that every time the Screen Capture and OCR are triggered it will be a different number.

in the screenshots it was 12

but the next one could be 0 or 17 or 3 or 7, so it's totally random.

but i always want to keep whatever number has been captured.

Thanks

Here's a demo macro for you to try. The KM action in green is the one that does the hard work for you, so adapt it to your needs.

Download Macro(s): Test Remove Text from Variable.kmmacros (3.3 KB)

Macro-Image

Keyboard Maestro Export

Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System Information
  • macOS 10.14.6
  • Keyboard Maestro v10.2

Let us know how you get on!

2 Likes

@Tiffle beat me to it. But I'd already started having a go at this. So here is another way that goes about it the other way around (i.e. it extracts the number rather than deleting what isn't a number).

EXAMPLE Get Number from String.kmmacros (3.6 KB)

Click to Show Image of Macro

3 Likes

I think I prefer yours @Zabobon because its intent (to extract rather than replace) is more indicative of what's wanted :wink:

@tiffle Yes it absolutely works!!

Thanks a lot.

i don't understand exactly why the macro works but it does.

something i wouldn't of been able to figure out on my own :slight_smile:

1 Like

@Zabobon

Thanks a lot, i haven't tried this one yet. i will give it a go soon

i've been spoilt for choice.

Much appreciated!

1 Like