How Can a Carriage Return Be Removed From a KM Clipboard?

I need to remove a carriage return from the info inside a KM clipboard. I’ve tried the “find and replace” but that won’t help. There is something called “expressions” I could try, but I’m a beginner and can’t understand anything at that level. What can be done?

Welcome to the forum.

This will delete one carriage return from the clipboard and concatenate lines one and two (if there is a line 2).

Remove Carriage Return in Clipboard Macro (v9.2)

Remove Carriage Return in Clipboard.kmmacros (1.7 KB)

Thanks rcraighead!

I'm trying to wrap my brain around the expression use. And I’m close. But I read that one should show more, talk less about inquiries. So I will show you more precisely what I’m trying to accomplish, and perhaps that will simplify the script you created for my dilemma.

I use Terminal to copy a single file name out of a folder full of sequenced image files.

(the zeros can range from .0. to .0000000.)

Unfortunately, Terminal adds a carriage return to the clipboard. I then copy the system clipboard to a named clipboard “sequence filename”. Then I remove the zeros and .dpx resulting in a clean filename.

So, just 1 line of text with a carriage return at the end.

I don’t understand if you “line 1” and “line 2” represent the ever changing filenames I encounter. Does your script still apply? Do I have to customize it to my needs?

Thanks again, and sorry for my newbie ignorance.

I want to add screen grabs but I’m not clear on how to do that yet.

Filter text with the trim whitespace option might work.


No problem. I'm always learning something new with KM.

The "Set System Clipboard…" Action is just for test purposes. Yes, it is to represent anything you might have on your clipboard. I added a "Line 2" because I didn't know if the carriage return would be the last item in the clipboard.

I think you could adjust the Regular Expression to strip off the zeros as well as the carriage return then save the result to your named clipboard.

Here's an update:
Remove Carriage Return in Clipboard Macro (v9.2)
Edit: I realize "fileName" should be a local variable since it does not need to be saved for later used.

Remove Carriage Return in Clipboard.kmmacros (1.9 KB)

Actually your RegEx using \n will NOT remove "Carriage Return", which is actually \r.
\n will only match linefeed (LF) character.

To match either LF or CR, you can use \R


IAC, I agree with @vincent_ardern that the best solution to remove either LF or CR is the KM Filter action using the "Trim whitespace" option.


Fantastic! It works! I’m not even really sure why! But I’ll learn eventually.

Not only did your Macro expression get rid of that nasty carriage return, it also cleaned up all the sequence numbers and the extension.

You have completed the last piece of a macro that will save me hours of brain dead data entry every day. Sadly, my co-workers did not have any interest in asking management for Keyboard Maestro. Oh well, their loss.

Thanks again to all of you. I hope I can help others on this forum in the future like you all have helped me.

Hey @BeeKeeper,

I'm glad you've got something working for you.

Just for the sake of completeness please post how you're getting your specific file name from the Terminal.


1 Like

A folder with thousands of image files in it can take a long time to open up. So to quickly copy a file name off of one of the files, I use this macro.
After highlighting the folder in the Finder and hitting option/command/c (to get the path of the folder) The macro moves to Terminal to list the files inside. In this case, only the first one. Then it selects the result by highlighting it, and copying it. However, it captures a carriage return at the end that is problematic for me because I want to paste the info in between other text while entering data into Terminal scripts. Hence the solution provided by rcraighead earlier in this topic.

How to copy a filename from outside a folder, to the system clipboard, with Terminal.kmmacros (6.5 KB)

Now that your requirements are much clearer, we can offer a better solution.

Below is just an example written in response to your request. You will need to use as an example and/or change to meet your workflow automation needs.

This works very fast for me in a folder with 800+ files, but one of our Shell Script gurus may be able to provide a faster solution.

Please let us know if it meets your needs.


Example Output


Verify Results

Below is just an example written in response to your request. You will need to use as an example and/or change to meet your workflow automation needs.

Please let us know if it meets your needs.


UPDATED: 2021-03-08 18:07 GMT-6
Ver 1.1 Changes

  1. Shell Script to get file list sorted by Creation Date changed to use -tU:
    ls -tU -p "$folderPath" | grep -v / | head -n 1
    • The ls switches are a bit unclear until I found this, thanks to @ccstone:
      -U Use time of file creation, instead of last modification for sorting (-t) or long output (-l).
  2. Make sure the Finder is frontmost at the top of the macro.

MACRO:   Get Name of First File in a Folder [Example]

-~~~ VER: 1.1    2021-03-08 ~~~
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:

Get Name of First File in a Folder [Example].kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.




  • Get Name of First File in a Folder [Example]
    • Depends on the Sort field. See Shell Script Action to change.


  1. First, make sure you have followed instructions in the Macro Setup below.
  2. See the below "How to Use" Comment Action
  3. This macro is just an example written in response to your request. You will need to use as an example and/or change to meet your workflow automation needs.


  • 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. ??
      Make These Changes to this Macro
  1. Assign a Trigger to this Macro .
  2. Move this macro to a Macro Group that is only Active when you need this Macro.
  3. ENABLE this Macro, and the Macro Group it is in.
    • For more info, see KM Wiki article on Macro Activation
    (all shown in the magenta color)
    • SCRIPT Action: Confirm ls file sort by Creation Date (desc), or change


  1. KM 9.0+ (may work in KM 8.2+ in some cases)
  2. macOS 10.12.6 (Sierra)+

TAGS: @Example @ShellScript @Files

JMichaelTX, what you’ve presented here is something I didn’t even know was possible. I will try it out as soon as possible. Thank you!

I am new to KM and I’m finding as I go along that once I clear one hurdle, a new idea pops into my head on how to take it even further.

Thanks again to all who have given their ideas.


Hey @BeeKeeper,

You'll find that to be true as long as you use Keyboard Maestro...  :sunglasses:

When asking question on the forum tell us what your overall task is – don't just ask "How do I get file X?"

Frequently we'll have some input that will help you improve the whole task.

It'll take you a while before you know what you don't know, and we can help with that as well.



@BeeKeeper, I just updated the macro in my above post. Please make sure you use this version.

1 Like

Thanks. I’ll take some time to play with this.


I'm a novice compared to the others respondents on this thread. I've been where you are. KM continues to unfold as long as you wish to learn more. I struggled for a year before my employer took KM seriously. Now it is an integral part of our workflow. Well worth the time and effort.