How Do I Know I've Reached the End of File (EOF) in a File I’m Editing with KM?

Wow! Thanks again for your patience and testing on this.
I'm not concerned about 2 vs 3 lines of code.
So, if you don't mind, please tell us which you would prefer.

One thing I like about both it that it makes it easy to separate creating the reversed file from updating the original source file. We can easily make that an option in a KM macro.

One of the things I liked about the sed solution was renaming the original file.
I'd like to do that with either the new tail or perl solution.
What I'd like is to rename "my source file.txt" to "my source file [ORIG].txt"
I know how to do that in KM, but would like to include that in the shell script if you can help me.

So here is what I have now for the KM Execute Shell Script:

tail -c1 "$KMVAR_Local__FilePath" | read || printf  "\n" >> "$KMVAR_Local__FilePath"
tail -r "$KMVAR_Local__FilePath" > /tmp/file-reversed.txt
mv /tmp/file-reversed.txt "$KMVAR_Local__FilePath"

One thing about using the perl solution is that I think we need to include a shebang and path to perl. I don't know what is standard with perl, but would like to have a solution that works for all users without requring the user to make any mods.

I have this for one of my perl KM scripts:

#!/usr/local/bin/perl

use 5.010;
use strict;
use warnings;

Thanks for your help.

EDIT 2020-08-10 16:06 GMT-5

I've changed my KM script to make it easy for the user to use any KM variable he/she wants:

read sourceFilePath

tail -c1 "$sourceFilePath" | read || printf  "\n" >> "$sourceFilePath"
tail -r "$sourceFilePath" > /tmp/file-reversed.txt
mv /tmp/file-reversed.txt "$sourceFilePath"

To keep a copy of the original file, add a line like

cp "$sourceFilePath" "$sourceFilePath".orig

just above the first tail command.

As for the Perl path, I’m just using the Perl that comes with macOS, which is at /usr/bin/perl. I’m pretty sure Keyboard Maestro runs in an environment in which /usr/bin is in the $PATH.

PS: Clever use of read!

Thanks for your reply.

Actually, KM does NOT provide any path, nothing related to what you get in Terminal.

I hate to be picky, but I don't want to change the extension of the source file -- just rename to append "[ORIG]" to the name, before the extension. If that is too much trouble, I can do it in KM.

Well, I've uploaded my probably final and very satisfactory solution, but I don't know how to get it into this topic. I think it's still sitting here somewhere. What action is required to get it here? I have the name of this thread in the box on top of it. I don't see any button or anything to send it here.

1 Like

See How to upload your macro .
There is a section on the bottom of the page that shows you how to upload into an existing post.

From here:

In essence, the default path in a Keyboard Maestro Execute Shell Script is the base path for the system:

/usr/bin:/bin:/usr/sbin:/sbin

You can test that by running a shell action with

echo $PATH

As for the renamed original file, I wasn’t suggesting you change your your naming system. I just didn’t want to go through the hassle of inserting text into the middle of a file name in a shell script. It’s something I’d rather not think about before dinner.

reverse file lines order Macro (v9.0.6)

reverse file lines order.kmmacros (6.5 KB)

1 Like

It looks like you got your macro uploaded, along with a whole bunch of unneeded stuff, which I have deleted.

I appreciate your editing it. In the process of trial and error, I created two topics consisting of my macro and with its name. I'm supposed to cancel them, but I haven't been able to find out how. How would I go about it?

I deleted one of them. Do you want me to delete the other, since you have a copy of your macro in this thread?

Yes, but I'm still curious how to delete a topic. Maybe I can't. Of course I have it on my computer, which is what I care about most.

The process of uploading to an existing topic is a bit confusing.
Here are the instructions from the wiki:

To MOVE to an existing topic, then AFTER the new topic is created:

  1. Select ALL in the new topic
  2. CUT the selection
  3. Cancel/Abort the new topic
  4. Goto the existing topic
  5. Create a reply
  6. PASTE into the reply block

What is not clear here is that it is referring to the EDIT area that is created when you do the original upload. The point is to copy just the text in the EDIT area, and then CANCEL the new topic. So then there is nothing to delete.
Then you paste the copy into an existing topic.

Maybe this video will help:
How To Upload Macro to Existing Post in Keyboard Maestro Forum

It's the Cancel/Abort I haven't figured out how to do. OH, I see where you explain it. I don't remember an EDIT area. If there's a next time, I'll look for it. The video was really helpful in showing me the EDIT area.

Don't worry about that. It is easy for me to do in KM.

Wow, thanks! I had no idea The Finder’s selection was an option anywhere. That’s simple and elegant. made a token for it, but it looks like not necessary.

I happened to have the opportunity today to use the clipboard version and it worked perfectly, with a couple of keystrokes to get the original into the clipboard and paste the reordered back to the file. This will save me those couple of keystrokes.