Keyboard Maestro “Trim spaces and convert to sentence case” Macro

“Trim spaces and convert to sentence case” Macro

I'm having trouble getting this macro to work.

I would like the macro to do the following:

  1. Select a piece of text
  2. Trim leading and trailing spaces
  3. Remove any extra spaces within the selected text (extra being >1)
  4. Convert to title case
  5. Paste the result back to the clipboard

Example input text: This is a test sentence with one leading space, two spaces here and one trailing space.
Example output text: This is a test sentence with one leading space, two spaces here and one trailing space.

I'm not sure where I'm messing up. I'm learning RegEx and would prefer a solution that utilizes it.

Can someone help fix this for me please?

I appreciate it.

Trim spaces and convert to sentence case.kmmacros (2.8 KB)

Looks like you are saving the system clipboard into url_title, cleaning the system clipboard, and pasting url_title back to your document (or wherever). So the cleaned system clipboard is not what’s being pasted.

This should do the trick: Trim spaces and convert to sentence case.kmmacros (2.7 KB)

As @korm pointed out, the macro you were using was affecting the clipboard after you had set the clipboard's text to a variable, so your search-and-replace and filter actions were having no effect on the url_title variable's text. I replaced your actions with similar ones targeting the url_title variable's text after it had been set to the current clipboard, so now they produce the effects you wanted (though for the record, you could also have achieved the results you wanted by operating on the clipboard and pasting that again at the end without saving its contents to a variable first). I also removed the Delete Past Clipboard action, since I didn't see a need for it given the list of desired actions you stated, and replaced it with a "Copy" action, so now you can simply trigger the macro after highlighting any text you wish to use it on and do not need to manually copy it first yourself.

As for the extra spaces, the built in Filter Clipboard or Variable with Trim Whitespace action takes care of all leading and trailing spaces without the need to build your own regex (I can certainly understand the urge to build your own, but I figure why reinvent the wheel when there's an existing action that already does just what you want) and a much simpler \s+[SPACE] search-and-replace handles any number of additional consecutive spaces within the text. If you have any further questions, feel free to ask!

1 Like

Or just skinny down your macro to the third and fourth actions in your original posting (Search and Replace Clipboard, and Filter Clipboard). Since you wrote that you want to "Paste the result back to the clipboard" then you don't need the other actions because those two actions alone do the job. I.e., select the text and copy it, run the macro, paste the cleaned text. Your regex works -- a bit overwrought -- but it works. @gglick has a better alternative for that part.

Thank you @korm @gglick. This did the trick.

It’s the details like these that trip me up .

@gglick - Is there a reason that you are filtering with trim space before you use the search and replace using RegEx?

The lesson here is - always store what needs to be manipulated to a variable before processing, rather than trying to save a few steps.

That is sometimes a good rule of thumb, but I wouldn't think it's necessary in a case like this. It wasn't because you stored data to a variable that that macro failed -- it was because you recovered the stored data in the last step and ignored the results of the middle part of the macro, which had actually accomplished your goal.

Glad we could help, @ssheth! And yes, @korm is right; saving to a variable before manipulating is definitely not always necessary. This revision of the macro accomplishes the same tasks by only targeting the clipboard:


And no, there was no particular reason I filtered with Trim Space before search-and-replacing other than that was the order of actions you listed in your original post. This macro revision with the order reversed works just as well.

2 Likes

Many thanks @korm and @gglick.

I agree with the observation and realize my error now. I wasn’t using the result of the output I wanted.

2 Likes

@korm and @gglick just came across your macro and its great. I wonder if this can be converted to just kill whitespace and leave 1 space between words

for example I would want to go from this

1 petit  baguette roll or a 7-inch section cut from a regular length baguette, purchased or homemade 
Mayonnaise, real (whole egg) or homemade mayonnaise                                         
          Maggi Seasoning sauce or soy sauce                                                          
          Your choice of boldly-flavored meat or tofu, sliced and at room temperature                 
 3 or 4   thin seeded cucumber strips, pickling or English variety preferred                          
 2 or 3   cilantro sprigs, roughly chopped                                                            
            3 or 4        thin jalapeño pepper slices                                                                 
          Everyday Daikon and Carrot Pickle (do chua)

to this

1 petit  baguette roll or a 7-inch section cut from a regular length baguette, purchased or homemade 
Mayonnaise, real (whole egg) or homemade mayonnaise                                         
Maggi Seasoning sauce or soy sauce                                                          
Your choice of boldly-flavored meat or tofu, sliced and at room temperature                 
3 or 4  thin seeded cucumber strips, pickling or English variety preferred                          
2 or 3  cilantro sprigs, roughly chopped                                                            
3 or 4 thin jalapeño pepper slices                                                                 
Everyday Daikon and Carrot Pickle (do chua)

any clue?

thx so much in advance

Z