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.
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]
EX 22: Condense Unix-style config file [shell-native Bash]
EX 23: Remove HTML comments [shell-sed]
EX 24: Remove HTML & Unix-style comments [shell-sed]
EX 25: Replace specified lines [search and replace–regex]
EX 26: Replace words retaining case [JXA]
EX 27: Remove whitespace-only lines [regex]
EX 28: Remove whitespace-only lines & terminating \n [regex]
EX 29: Extract the last line containing specified text [regex]
EX 30: Remove leading/trailing whitespace on lines + convert whitespace-only lines to newlines [shell-native Bash]
EX 31: Remove leading/trailing whitespace on lines + whitespace-only lines [shell-native Bash]
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.
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.
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?
@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
@tiffle, always glad to steal your stuff. 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.