This could surely be made to be more efficient logically, but here's one way. This is designed to work on lines that have been copied to the system clipboard before running the macro.
That's a long macro. Why not use a shorter one, like this:
There are many ways to solve this. I didn't validate if this method handles special cases like trailing spaces. If this doesn't work tell me what's wrong with it and I'm sure I can fix it.
**Requires: KM 8.2.4+ macOS 10.11 (El Capitan)+**
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))
#### DOWNLOAD Macro File:
<a class="attachment" href="/uploads/default/original/3X/4/2/42d8f40e84baf506580afb38bd66f17eec67a551.kmmacros">Reverse Lines [Example].kmmacros</a>
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**
---
<img src="/uploads/default/original/3X/6/3/6327c6fc2e00c74fa8fbddac3faa23a9d81d35bd.png" width="614" height="1128">
---
### JXA Script
```javascript
(function myMain() { // ~~~ automatically executed when this script is executed ~~~
var ptyScriptName = "Reverse Lines in String"
var ptyScriptVer = "1.0"
var ptyScriptDate = "2019-09-25"
var ptyScriptAuthor = "JMichaelTX"
// --- SET CURRENT APP VARIABLE NEEDED FOR DIALOGS & StandardAdditions.osax ---
var app = Application.currentApplication()
app.includeStandardAdditions = true
// --- KM ENGINE APP ---
var kmInst = app.systemAttribute("KMINSTANCE"); // needed for Local & Instance variables
var kmeApp = Application("Keyboard Maestro Engine")
var sourceStr = kmeApp.getvariable("Local__SourceStr", {instance: kmInst});
if (!sourceStr) {throw new Error('Source string MUST contain at least one line'); }
var sourceList = sourceStr.split('\n');
var revList = sourceList.reverse();
return revList.join('\n');
})(); // ~~~ function myMain() is automatically executed when this script is executed ~~~
```
My goodness you guys have some pretty great answers.. I got into KM from a visual designer perspective, 'ohhh look at this feature 'click on found image'.. nice!!' 99% of my solutions. I'm learning a ton from you guys and the forum! Thx! Will try them all.
(When it comes to processing text, bailing out to a shell script will almost always be βeasierβ because Unix has so many tools for text manipulation. But I put βeasierβ in quotes because you have to find the right incantation, which can tricky, but often not much harder than figuring out how to do it in Keyboard Maestro natively.)
Good one. Beautiful. I'll be honest: half the time when I answer a question I don't know the answer until I google it. And I don't always find the best answer when I do that, as you have shown in this case.
However I can make your solution 56.25% shorter, just drop the "/usr/bin/".
Appreciate the discussion everyone! In my testing, the output for the three command script cat -n | sort -rn | cut -f2- provided by Sleepy yielded perfect results whereas the single command provided by tjluoma misses a line break near the beginning of output. Screenshots of testing output below.