[FILE] Process Lines in Text File

MACRO: [FILE] Process Lines in Text File

DOWNLOAD:

[FILE] Process Lines in Text File.kmmacros (10 KB)

VER: 1.0 Last Update: Thu, Feb 11, 2016
Author: @JMichaelTX based on example by @ccstone

PURPOSE: Demo How to Select, Read, and Process each line in a file

I was inspired by the post by Chris (@ccstone) below to write this macro, based in large part on his post.

Since there is no built-in KM Action to choose a file (or folder), I thought it would be helpful to share a simple AppleScript to perform that function.

This macro is a complete macro, from selecting the file to processing each line of that file. You need only add the Actions you want to be applied to each line of the file.

If you find any bugs or issues, or have suggestions for improvement, please feel free to post below.

8 Likes

Nice, -
How would I get those lines into variables.
ie. - there’s 3 lines of data in the file.

abc
def
ghi

I’d like to end up with
Var1 = 'abc’
Var2 = 'def’
Var3 = ‘ghi’

I can’t figure out how to ‘increment’ the variable name that the data is stored to as it 'loops thru the ‘for each’ step

Set variable Index to 1

In the loop:

Set variable “Var%Variable%Index%” to text "%Variable%SingleLine%"
Set variable Index to calculation Index+1

2 Likes

What Troy wished to do I would like to as well, and I have tried to insert:

Set variable Index to 1

In the loop:

Set variable "Var%Variable%Index%" to text "%Variable%SingleLine%"
Set variable Index to calculation Index+1

In the appropriate places in the Macro made by JmichaelTX, but I am doing something clearly wrong. I suspect the problem concerns this variable:

Set variable "Var%Variable%Index%" to text "%Variable%SingleLine%"

And have tried various combinations (such as without %Variable% and % but I cannot get the macro to work.

Any help would be gratefully received.

The macro as I have amended it:

Hey Victor,

How about providing the actual macro and a test file to read into it.

You can post the text file as a zip archive.

-Chris

You are using a Set Variable to Calculation action, but you are actually setting the variable to another variable, not to a calculation:

I would be surprised if that works. Try the Set Variable to Text action, as recommended by @peternlewis in his post above.

Tom,

Thank you for pointing out the error. I corrected it, and it still does not work.

I think the error message is;

2017-05-07 15:52:29 Execute macro ‘[FILE] Process Lines in Text File copy copy’ from trigger The Hot Key ⌃⌥⇧⌘T is pressed
2017-05-07 15:52:29 Set Variable to Text failed to set variable “VarIndex+1” (from “Var%Variable%Index%”)
2017-05-07 15:52:29 Set Variable to Text failed to set variable “VarIndex+1” (from “Var%Variable%Index%”) Macro ‘[FILE] Process Lines in Text File copy copy’ cancelled (while executing Set Variable ‘Var%Variable%Index%’ to Text ‘Var%Variable%Index%’).

Attached is the macro and the text file as suggested by CCStone.

Process Lines in Text File.zip (3.1 KB)

Hey Victor,

I got rid of some non-functional actions and renamed some variables in a way that makes more sense to me (but ymmv).

In any case I got it working.

You'll need to change the file path to match your system.

Process Lines in a Text File.ccs.v01.kmmacros (8.6 KB)

-Chris

Chris,

Thank you for the revised macro. However, it worked in the original version made by JMichaelTX, and my adaption and of course your version.

My problem remains that I cannot get what Peter Lewis suggested with:

Set variable Index to 1

In the loop:

Set variable "Var%Variable%Index%" to text "%Variable%SingleLine%"
Set variable Index to calculation Index+1

to work for me (as indicated in my previous post).

I would be grateful for any suggestions on how I interpreted Peter Lewis's words incorrectly.

@VWKM, sometimes it can be a challenge to get things just right.

I have fixed your Macro, but for educational purposes, let me first explain the problems and changes. Note: All of your original Actions are shown as dark grey (disabled), and my replacement Actions are shown in teal.

First, there was an error in @peternlewis' post:[quote="peternlewis, post:3, topic:2936"]
Set variable Index to 1
[/quote]

should have been:
Set variable Index to 0

Actually, a new index variable was NOT needed, since in my original macro I already had:

In any case, I made this correction:

Next, this Action has two problems:

  1. It occurs before the index is updated.
  • The "to:" field is wrong

The correct "to" field is shown below.

Next, you used "Set Variable to Text" for the "Index" variable.
It should be "Set Variable to Calculation"

Now you are ready to create the new "Var" variable, and assign the current line from the file to it:

I added this Action to log the results:


So, now, when I run your macro with my corrections, the result is:

<img src="/uploads/default/original/2X/d/d571be1c8454660bf96606ba1a2229dbe6b4f7c9.png" width="510" height="265">

Here's the corrected Macro:

###MACRO:&nbsp;&nbsp;&nbsp;[FILE] Process Lines in Text File -- @VWKM version with @JMichaelTX Corr

~~~ VER: 1.0&nbsp;&nbsp;&nbsp;&nbsp;2017-05-07 ~~~

####DOWNLOAD:
<a class="attachment" href="/uploads/default/original/2X/b/b132d17273f88af1facb72ae9cf86a4dfbaacac3.kmmacros">[FILE] Process Lines in Text File -- @VWKM version with @JMichaelTX Corr.kmmacros</a> (13 KB)

---

###ReleaseNotes

REVISED 2017-05-07  3:34 PM CT BY @JMIchaelTX
to correct problems in version by @VWKM

---

<img src="/uploads/default/original/2X/5/5b138254f972424c1d20bbca4de192b247986e1d.png" width="389" height="2000">
1 Like

@troy and @VWKM: I'm curious. May I ask you guys a question please?

Why do you want the lines from the file into separate KM Variables?

IME, this makes it harder to deal with the data, unless you always know that each line represents the same entity, like
Name = Line 1, Phone = Line 2, email = Line 3
Then I get it.

But you end up with Variables named "Var1, Var2, Var3". Not so useful. Even so, this approach is very unforgiving. Sometimes using RegEx can be a better solution.

If you don't mind sharing your purpose/intent of using KM Variables, we might be able to suggest alternate methods.

Hey Victor,

Ah, I see...

I didn't realize we could create variables dynamically this way, so I misunderstood the issue.

It looks like JM has you covered though.

-Chris

This is very powerful, but hard to understand at first (for most) and tricky to use. :wink:

I have trouble with the Macro
When I try it fresh out of the box, I get an error message:

Action Failed
Read File action failed because source is not a full path obj[7871]: Class FIFindferSync etc. etc.

I just tested this macro again in it is running fine with Keyboard Maestro 8.1.1 (8.1.1) on macOS 10.12.6.

This sounds like you may have selected a non-text file.
After you run the macro, please do this:

  1. Make a screenshot of the actual error msg, and post here.
  2. Open the KM Editor Preferences, and get the value of Variable "myFilePath", and post here.

Above is the error message.

I was correctly opening a text file. That was not my problem.

I found the "cause".

I was using the Try button in Editing mode to launch the Macro. That a resulted in the error. When I launched the Macro in the conventional way (not in Editing Mode but using the Status Menu), it worked fine.

I appreciate your very prompt response. Sorry for not having figured this out. I did not realize that the Try button could cause trouble in this way.

When launching using the Try button, the value of myFilePath is seen below, in case you are interested,

objc[9358]: Class FIFinderSyncExtensionHost is implemented in both /System/Library/PrivateFrameworks/FinderKit.framework/Versions/A/FinderKit (0x7fff92deeb68) and /System/Library/PrivateFrameworks/FileProvider.framework/OverrideBundles/FinderSyncCollaborationFileProviderOverride.bundle/Contents/MacOS/FinderSyncCollaborationFileProviderOverride (0x113b33cd8). One of the two will be used. Which one is undefined.
/Users/owl/Desktop/SomeTest.txt

The reason it failed when using the Try button was because the Try button only executes the current action, not the whole macro, so the previous actions that the selected action expected to have been run before it was executed had not yet been run (don't worry, you're far from the first one to assume that that button is for the whole macro rather than just the action). In KM8, the button to execute the entire macro is this one, at the top of the editor window:

5

First of all, I appreciate learning about the Entire Macro Run button and how it differs from the Try button. But if NO action is selected, the Try button seems to run the entire Macro.

So if you leave the Edit mode (click to deactivate the Edit button) and then click it again (activate the Edit button) there is no single Action that is selected. If you hit the Try button at this point, it seems to run the entire Macro. Since I did not understand/appreciate the Entire Macro Run button that you pointed out to me, I would go through this little dance often.

Currently, I still will get the error even if I use the Entire Macro Run button.

The sad thing is that I thought the issue was being in Edit mode. I had had success with invoking the Macro using the Status Menu. But when I retry the whole thing now, I get the error also when I use the Status Menu. I do not know what has "changed". Rarely, it will work. Confounding.

When I run the Macro: [KM]Display Macro Error that JMichaelTX provided to help with debugging, it also throws an error.

180320_1949Regex

So somehow something weird is going on with the state of my Mac in terms of this Macro. Most commonly it fails totally after I select the text file. Sometimes it will work and several windows will open up showing the various lines. Sometimes only the first window will open up showing the first line and then I will get the error. It is all bizarre.

I also get this same error when i run [KM]Display Macro Error
I run it immediately after an error, so i have no clue why. Or at least: it can’t match with the regex…

High sierra 10.3.3
KM 8.1.1

EDIT: Don’t want to go off topic. Just affirming rlivingston isn’t the only one :wink:

I assume you are referring to the macro at the top of this topic:
MACRO: [FILE] Process Lines in Text File

Just to be clear, that button is called "Run Selected Macro"
image

Please be sure you are selecting a TEXT file with multiple lines in it, like this:

qqqq
wwww
weee
errrr
rttttt
yyyyy
uuuu

If you get an error, please make a screenshot of it and post here.

OK, I can help with that, but not in this thread. That is a completely different issue. Please post your comments/issues about the Display Macro Error macro in its topic:
MACRO: [KM] Display Macro Error

But for now, to debug your issue with " [FILE] Process Lines in Text File", don't bother with the "Display Macro Error" macro.