Is there a way to trim a variable up to the first colon or second comma?

Is there a way to trim text (which happens to be a variable from a browser webpage) up to :

  1. the first colon :
  2. first second comma ,
  3. the first [space] dash - or
  4. the first second [space] pipe |?

The reason for this is that I am parsing very long product titles... I want get the most important part of the title .. and I don't want to be cut off mid-word .. and this I think would achieve it

so for example:

1

Sony PlayStation 4 500GB Console (Black) : 1: Amazon.co.uk: PC & Video Games

would become

Sony PlayStation 4 500GB Console (Black)

(due to rule 1)

2

Nintendo Switch (Neon Red/Neon blue) : Amazon.co.uk: PC & Video Games

would become

Nintendo Switch (Neon Red/Neon blue)

3

Golden Swan White Vinegar for Cleaning, Pickling, Marinating & Cooking - Distilled White Vinegar- 5 Litre Bottle - Produced in The UK,5 l (Pack of 2) : Amazon.co.uk: Grocery

would become

Golden Swan White Vinegar for Cleaning, Pickling

due to rule 2

4

Apple iPod classic 160 GB Black - 7th Generation (Renewed)

would become

Apple iPod classic 160 GB Black

(due to rule 3)

5

CHEESIES | Crunchy Cheese Keto Snack | Variety Pack (Selection 1) | 100% Cheese | Sugar Free, Gluten Free, No Carb | High Protein and Vegetarian | Crunchy, Baked and Tasty | Multipack | 6 x 20g Bags : Amazon.co.uk: Grocery

would become

CHEESIES | Crunchy Cheese Keto Snack

(due to rule 4)

Hi, @Tinker. If I've assumed the correct precedence of your rules, then this should work. If incorrect, this might require some tweaking:

Download: Extract String Prefix.kmmacros (7.5 KB)

Macro-Image


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 13.5.1 (22G90)
  • Keyboard Maestro v10.2

2 Likes

thank you very much for your reply!

I enabled the macro and pressed play, it seemed to do the trick .

However, I will only be dealing with one title (sentence) at a time. (It's great that your example allows you to run them all at once, but in my use case it's just once sentence and it's coming from '%ChromeTitle%or%FrontBrowserTitle%`.

I dragged a few things around
Extract String Prefix from DaTitle.kmmacros (6.0 KB)

Can you please tell me if it looks okay?

How to test:

  1. Open Google Chrome to any amazon product page
  2. Use the status menu to invoke the macro
1 Like

I made a few minor changes. Unless you want variable values to persist after a macro is run, it's a good idea to use local variables.

Download: Extract String Prefix from DaTitle v2.kmmacros (5.7 KB)

Macro-Image


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 13.5.1 (22G90)
  • Keyboard Maestro v10.2

2 Likes

oh that's what local means!

In order to test, I opened Chrome at https://www.amazon.co.uk/Little-Plumber-LP1-operated-Electronic/dp/B004TM7WT2

The outputted text looks unchanged ?

Oops, going back up the thread, I misread your original requirements.

This version should work, but note that you might need to change the order of the actions depending on the precedence of your 4 rules. As written, the macro first checks for rules 2 and 4. If neither is matched, it checks for rules 1 and 3.

Note also that I added the red action that will always trim trailing spaces. You can remove it if that's not your preference; but without it, due to rule 1, the returned string would include a trailing space for a %ChromeTitle% like this:

Little Plumber : LP1 Water Softener - Perfect For 1-4 Bedroom Houses - Easy To Install - Removes Existing Limescale & Saving up to 70% Heating Costs - Protects & Extends Life Of Appliances : Amazon.co.uk: DIY & Tools


Download: Extract String Prefix from DaTitle v3.kmmacros (5.8 KB)

Macro-Image


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 13.5.1 (22G90)
  • Keyboard Maestro v10.2

1 Like