MACRO: Text Transformation EXAMPLES

Text Parsing : Text Transformation EXAMPLESx4-EL


DESCRIPTION

Often macros require text transformation actions. Although a common requirement, many times developing these can be challenging.

I’ve created this macro with a goal to efficiently share text transformation techniques amongst users. I started by including 16* (see list below), but this macro would be especially valuable if you and others in the forum contribute more.

If you have techniques that you’d be willing to share, please download, add and test yours, and repost an updated version of the macro in this thread. The first comment in the macro includes full instructions.

*2022-07-05 UPATE: Macro now includes 21 examples


List of Examples

  • EX 1: Remove leading whitespace on lines [regex]
  • EX 2: Remove trailing whitespace on lines [regex]
  • EX 3: Remove leading whitespace on lines + whitespace-only lines [regex]
  • EX 4: Remove trailing whitespace on lines + blank lines [regex]
  • EX 5: Remove leading/trailing whitespace on lines + whitespace-only lines [regex]
  • EX 6: Remove leading/trailing whitespace on lines + comments/whitespace-only lines [regex]
  • EX 7: From URL, extract the domain excluding .com, .org, etc. [JavaScript]
  • EX 8: Double-space lines removing leading/trailing whitespace-only lines [shell-awk]
  • EX 9: Randomize characters [shell-shuf]
  • EX 10: Randomize characters [shell-native Bash]
  • EX 11: Capitalize every word removing leading/trailing whitespace-only lines [JXA]
  • EX 12: Titlecase using the clipboard removing leading/trailing whitespace-only lines [JXA]
  • EX 13: Uppercase letters [filter]
  • EX 14: Uppercase letters removing leading/trailing whitespace-only lines [Ruby]
  • EX 15: Straighten quotes removing leading/trailing whitespace-only lines [Perl]
  • EX 16: Uppercase consonants; lowercase vowels [regex-case conv meta chars]
  • EX 17: Remove trailing whitespace on lines [JXA]
  • EX 18: Extract nth line from a list [shell-head & tail]
  • EX 19: Extract nth line from a list [awk]
  • EX 20: Extract nth line from a list [sed]
  • EX 21: Extract nth (or -nth) line from a list [AppleScript]

DOWNLOAD Macro File:
Text Transformation EXAMPLES.kmmacros (181 KB)Note: This macro was uploaded in a DISABLED state. It must be ENABLED before it can be run. If it does not trigger, the macro group might also need to be ENABLED.

Macro-image

1 Like

That could be quite helpful, partly because the wiki and docs say so little for so many filters.

https://wiki.keyboardmaestro.com/manual/Filters

For example, the filter for "Trim whitespace" simply says "Trim whitespace". It doesn't say whether that removes duplicate spaces, removes trailing empty lines, removes internal tabs, etc. Do the words in the actions in the macro that you uploaded get indexed by this website's internal search engine? If not, maybe you should find a way to copy the titles of your examples into the text of your post.

1 Like

@Sleepy, thanks for commenting.

Especially if users like you and others contribute. :grinning:

I don't know, but each example includes text in comments. Here's one of the current 16:

I could add this text, but I'll wait to see if someone knowledgable about the forum knows if the comments in the macros get indexed.

I doubt that text within a macro file is indexed by the forum's search engine. Thanks for contributing the examples, they will be useful to explore and cherry pick.

Hi @gordonmeeyr. You're welcome; glad you find the macro useful.

You are probably right. Maybe @peternlewis or @ccstone know for sure.

Quite.

Done! @Sleepy, thanks for the suggestion.

I've updated the OP of Text Transformation EXAMPLES with the following:

  • EX 17: Remove trailing whitespace on lines [JXA]
  • EX 18: Extract nth line from a list [shell-head & tail]
  • EX 19: Extract nth line from a list [awk]
  • EX 20: Extract nth line from a list [sed]
  • EX 21: Extract nth (or -nth) line from a list [AppleScript]

For EX 21, the AppleScript was adapted from Get Line From List by @tiffle.

One thing about your adaptation EX 21, it's not really big on error checking so for example, there's no way to distinguish between the result returned for an empty list, and the result returned when you choose the blank line in a list that contains a blank line!

One other thing, the output from your routine when asking for the blank line in a list containing one is this:

Using AppleScript:
- Get nth (or -nth*) line of a list.
- If the list is empty or n is out of range, null value will be returned.

*counting from the bottom.
┈┈┈┈┈┈┈┈┈ ( input ) ┈┈┈┈┈┈┈┈┈┈┈┈
Line 1
Line 2

Line 3
Line 4
Line 5
⎯⎯⎯⎯⎯⎯⎯⎯⎯ ( output ) ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

┈┈┈┈┈┈┈┈┈ ( output + │ per line ) ┈┈┈┈┈┈
┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈

So, shouldn't the last output be this rather than just nothing?

|

Yes, as written there is no way to distinguish the circumstances you stated. I've updated the local_Description to clarify the logic as written:

Keyboard Maestro Export

Of course, one could change the logic if it was necessary to distinguish.


Actually no. The problem was that when the output was null, it was not properly displayed.

In the output section, I added the necessary logic to address this situation.


@tiffle, your questions prompted me to check other edge cases. During that check I discovered an anomaly with:

For each (The lines in SOME_VARIABLE)

Even with Include blank lines checked, the action does not return the last whitespace-only line. (@peternlewis, is this by design? The second-to-last, third-to-last, etc., whitespace-only lines are returned.)

In the output section, I've added the necessary logic to address this anomaly.

Hey @_jims! Thanks for adapting my subroutine and including it in your macro! I can see you’ve put in a lot of effort and that is of benefit to all us users of KM since the manipulation of text is a very common need. Having your macro as a reference is a brilliant resource :clap:

@tiffle, always glad to steal your stuff. :wink: I do try to include the proper attribution when I borrow ideas. This macro makes it easier: Forum Username Tool

Thanks for commenting and, yes, I hope others find the macro useful as well. I've been the grateful recipient of so much information on this forum; it feels good to occasionally return the favor.


Thanks also for your above comment regarding the output inconsistency. :+1: