Truncating a variable by text size, or line length

I’ve got a variable that keeps a running list of recently created file names (created using another Macro), Each new entry is separated by a carriage return.

I have it set up so the most recent entry is at the top of the list, and I’d like older entries (at the end of the list) to drop off.

How can keep this variable to the last 20 entries? IE - the top 20 lines of text in the variable. I think the solution lies in the “Search Variable” but the expressions are new to me. I’d also be happy enough lopping off the end of the text by retaining the top 500 characters or so, but if I could crop by dropping the line at the bottom that would be neater.

Thanks for any help!

1 Like

Here's an example / proof-of-concept that should get you going.
Adapt as you need to for your needs.

Please let us know if this update works for you.

2017-08-31 16:07 CT

###I think Ver 2.0 will fix the issues noted in the below posts:

  1. If the Source list has fewer lines than the max lines allowed, THEN ALL lines are retained.
  • The RegEx now allows any of these at the end of the line:
    • New Line \n
    • Return \r
    • End of String $

I suspect the "formatting" issue you referred to is that you had RETURN character rather than NEW LINE character at the end of some/all of your source data.

###Example Results

####Note that now the Source Data (GLV__SourceStr) is updated with the results.

Where Number of Source LInes (9) is < GLV__NumOfLinesToGet (20)

Where Number of Source LInes (9) is > GLV__NumOfLinesToGet (3)

###RegEx Pattern

For details, see

###MACRO:   Get (Extract) Multiple Lines in Variable @Example

~~~ VER: 2.0    2017-08-31 ~~~

Get (Extract) Multiple Lines in Variable @Example.kmmacros (6.4 KB)
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.

###Use Case

  • Limit List to Max Number of Lines



NOTICE: This macro/script is just an Example

  • It is provided only for educational purposes, and may not be suitable for any specific purpose.
  • It has had very limited testing.
  • You need to test further before using in a production environment.
  • It does not have extensive error checking/handling.
  • It may not be complete. It is provided as an example to show you one approach to solving a problem.


  • Carefully review the Release Notes and the Macro Actions
    • Make sure you understand what the Macro will do.
    • You are responsible for running the Macro, not me. :wink:
  • Assign a Trigger to this maro.
  • Move this macro to a Macro Group that is only Active when you need this Macro.
  • ENABLE this Macro.
    • ALL Actions that are shown in the magenta color


  • While I have given this limited testing, and to the best of my knowledge it will do no harm, I cannot guarantee it.
  • If you have any doubts or questions:
    • Ask first
    • Turn on the KM Debugger from the KM Status Menu, and step through the macro, making sure you understand what it is doing with each Action.


Thanks very much this is incredibly helpful and works like a treat!

1 Like

Hmm, this seems to work great for smaller lists, but when I get over 9 the Macro fails. At first I thought it was because the list didn’t contain enough lines, but it turns out that isn’t the case. I can’t figure out why it fails on larger numbers but I’m still looking.

If it fails with specific data, post the specific data. Or even, paste the specific data into @JMichaelTX’s macro and verify that it fails and post that.

Here’s an example of data that it fails here on:

Clip 08.jpg
Clip 31.jpg
Clip 31b.jpg
Clip 32.jpg
Clip 40.jpg
Clip 40b.jpg
Clip 53.jpg
Clip 54.jpg
Maui Clip 53b.jpg

Its something to do with a text formatting issue I think. If I paste these lines first into the macro directly, it will fail. However if I copy and paste them BACK from this form I’m entering this into, it works. In other words this text I’ve pasted above DID fail when I pasted it into the Macro. But then I pasted in here, and for some reason thought I’d try selecting it BACK here form this form and then pasting it into the Macro, and it works. So this will not fail for you if you copy and paste it from here.

I’ve gone back and forth a bit with some lines trying to remove text formatting with partial success, but I guess I need a way to strip any formatting out of the text is being parsed if that is indeed the issue.

This is the error I keep seeing:

I also get the same error I get if I enter a number that is larger than the number of lines. Example - 12 lines of text and I ask it to strip the first 20 I get the same error.

Normally I’m asking it to strip something like the first 5 lines, with 12 or so lines of text to work with.

Right, I was surprised when you said “when I get over 9 the Macro fails”.

The regex, as written, would fail to match anything less than the specified number of lines. Currently the regex matches exactly the specified number of lines, so if the source has less than that, it would fail.

Change the section in {} to be {0,%Variable%GLV_NumOfLinesToGet%}. That will match up to that many lines.

With that change, your input data works with GLV_NumOfLinesToGet set to 2 or 4 or 12 in my tests (so numbers smaller or larger than the number of lines).

If you find a case that still fails, post the number and the data.

Thanks for the continued interest. I’m still getting fails, with the first being an empty list (dropping everything) when using {0,%Variable%GLV_NumOfLinesToGet%}. But lets not worry about that - I can disable the fails when the number is wrong. The bigger issue remains, and is still something to do with the list’s text formatting - I think.

When I paste lists in here (in this forum reply), it loses some formatting that is causing me trouble. So the only way I know I can replicate this over the web for other people is for you to run this applescript (please forgive the sloppy code - I’m not a programmer and just squeak by). Run this, and select a folder, then paste the results as the list to parse. Thats where I get fails consistently.

Now - if I take the results of this applecsript and paste it into a fresh textedit document, then turn that into plain text, THEN copy that plain text and paste into the list source, the macro works. So there’s SOMETHING in my list thats tripping up the macro.

(from QK)

--file names

tell application "System Events"
	display dialog "This will prompt you to choose a folder, and it will copy the names of all the files in the folder you select to the clipboard." & return & return & "Please choose the top level folder of your library at the next prompt." with icon 1
	set x to choose folder with prompt "Please select your top level library  folder......"
	tell application "Finder"
		set fn to the name of every file of folder x as list
	end tell
	set AppleScript's text item delimiters to ""
	set nn to ""
	repeat with i in fn
		set i to i as text
		set nn to nn & i & return
	end repeat
	set nn to nn as Unicode text
	set the clipboard to nn
	display dialog "File names copied to the clipboard." with icon 1
	return nn
end tell

I can’t spend much time looking at this today. FWIW I did make a workaround with applescript that is doing the job, but I would like to get to the bottom of this inside KM as I’m sure it can do it.

FYI the display is odd on the web pasting in this applescript code.

I have just made an update to the macro in my above post that should fix your issues:

I used your example data in developing and testing this revision.

Please let us know if this update now works for you.

###How To Post Example Data

When you post example data, please put the data in a code block, between lines of triple backquotes:

– Your DATA Here

1 Like

Thanks for this latest version. I can confirm this works on the data that was giving me issues before.

Thanks again for ALL the input. I’ve never dealt with RegEx before and wouldn’t have gotten to the result on my own.