Looking for an elegant way to copy the first line of a text to the clipboard, without moving the cursor

This is part of a larger macro which copies a text (a Bear Note) and saves as RTFD with the first line as a title.

The macro works fine but it slows my workflow because it moves the cursor to highlight/copy the first line and takes time to find and come back to where I was working in the text before the save.

thanks in advance for your time and help


Doesn't Bear's "Export Notes..." already do this? I've just tried, and the suggested file name is the first paragraph of the selected note. Obviously, if you use long titles/first paras in your Notes you'll want to trim that down somehow.

1 Like

thank you for your post.

I have been using Bear for many years and have written quite a few Keyboard Maestro macros specifically for Bear.

I often write the draft of document in Bear because of all the advantages of Markdown.

I wrote a KM macro which automates the menu export including adding date time stamp to file, specify directory etc.

When I work, I make a habit of exporting regularly

Recently, I was working on a long and complex document, and suddenly everything disappeared, a know sync problem. I learned that I have to backup (export) the note I am working on more frequently, and then grew tired of the time the export process takes even with automation because of the time it takes to save to directory, give a name etc.

I wanted to create a single current note backup process which was
1- very fast
2- transparent (in the background)

The result if the following macro. It is extremely fast and transparent (works in the background) which much improved my workflow speed. It's night and day compared to note export in the menu.

The only residual problem as explained above is to extract the title without moving the cursor which causes me to lose track of where I was in the text at the time of backup

Backup SINGLE - for forum export wip$ copy.kmmacros (33.6 KB)

Note that the filename contains the keyword BearJLRapidBackup. It's simply a way for me to do a search and delete of these temp files. I may write a Hazel rule to do so.

thanks again

I think that it's a question of finding this regex.


OK -- looks like you've two problems here:

  1. How do I copy an entire Bear note to the clipboard without losing my place in the note?
  2. How do I get the first line of the clipboard contents?

Really easy way to do the latter is:

...and you can then strip out any Markdown characters.

If you'd prefer a regex then you play on the fact that KM's RegEx search only returns the first match it finds. If you always start your notes with a heading (of any level) you could use:

...but if you are as inconsistent as me(!) you may prefer to grab the entire first line and process later:

The first problem is trickier -- I can't see any way of getting "current insertion point" out of Bear. Best suggestion I can give is:

  1. At the insertion point, add some never-used text like "=insertionPointHere="
  2. "Select All", "Copy" to clipboard, "Search..." the clipboard for that string and
    "...Replace" with nothing to delete it
  3. Back in Bear, "Undo" -- which should get you back to where you were before you added the text, insertion point in the right place

A bit of visual distraction, but maybe not too bad. I'd love to know whether that works for you!

thank you but it's not working.

A file is created with ?? in the filename instead of BearNoteTitle extracted below

There must be something I don't understand.

is this correct, just to extract the title (first line) which I will put in the path later?


You've missed the ^ symbol -- should be ([^\n]+)

Your pattern means "find one or more new-line characters" -- what you want to find is "one or more characters that are not new-line characters", which is what the ^ does.

1 Like

As long as Bear follows the Mac's standard keyboard shortcuts for text, you can get the first line without losing your place by

  1. โ‡งโŒ˜โ†‘ to select all text from your cursor back to the beginning of the document.
  2. Copy (you can use โŒ˜C, but Keyboard Maestro has a direct Copy action).
  3. โ†’ to unselect text and put the cursor back where you started.

There's a slight flash when the selection is made/unmade, but it's not too distracting.

Now you just have to get the first line of what's on the clip. There are lots of ways to do this, but my choice would be a shell script of head -n 1.

Here's a screenshot:

I put the first line into the variable Local_FirstLine and also deleted the text that was put on the clipboard during the macro. That puts your clipboard back to the state it was before the macro was run.

Works perfectly. Sorry for the bungle.

a very nice alternative. thanks very much

That's a nice way of doing it if all @ronald needs is the first line. But -- and perhaps I'm reading between the lines too much -- I think that, at some stage, he's copying the whole document and I can see no easy way to get back from a "Select All". Hence the "create your own text placeholder" approach.

It would never have occurred to me that exporting a document required selecting all the text. My impression was the @ronald wanted the first line so he could build a string to use when filling in the Save As of Export dialog box. But if selecting the whole document is necessary, your placeholder solution makes the most sense.

when I use your method, everything works fine at each step using debugger including the variable which I can see with display text after your shell script, except for the last step which is write to file. Do you see any reason for this ?
thank you

This works fine

This last step stopped working.

This is the path which always worked. It acts like it no longer recognizes the title variable.

~/Documents/Dropbox/JL IMAC backup of important systems files/Bear Notes/%Variable%BearNoteTitle% BearJLRapidBackup %ICUDateTime%EEE, d-MMM-yyyy h-mm%.rtfd

thank you

I did not really have a problem because I

1- copy all โ†’ extract title โ†’ variable which I put in path at the end
2- copy all โ†’ clipboard to file

that way the cursor no longer comes into question

@drdrang @Nige_S

this is the macro which does not work (only the last write file action does not work).
thank you


What are you copying twice? Just copy all, extract the first line to a variable, then write the clipboard to a file using that variable in the file name.

You need to output the file path and check it, making sure you haven't included eg trailing returns -- you have set "Trim Results" in the shell script action, haven't you?

You'll probably find the "For Each" method of grabbing the first line both quicker than shelling out (less overhead) and easier to troubleshoot.

An image really doesn't help -- important information is hidden when it extends past the dialog box, we can't see your action options, etc.

you are very patient. Thank you.

Back to your method, using regex

I'm at my wits end. I am sending the macro just in case something obvious strikes you

Backup SINGLE - Nige_S Method for forum upload wip$.kmmacros (31.5 KB)

What's your account short name? Is it "jronald" as in the comment, or "ronald" as in the path used in the write action? In fact, skip the full path and use the abbreviated version, then you can use the same script with multiple accounts/computers:

~/Documents/Dropbox/JL IMAC backup of important systems files/Bear Notes/%Variable%BearNoteTitle% BearJLRapidBackup %ICUDateTime%EEE, d-MMM-yyyy h-mm%.rtfd

If you're using different backup folders for different machines, leverage the %MacName% token -- and name your computers and backup folders appropriately!

1 Like

OK, thank you.

Using a tilde in the pathname , everything works perfectly now.
If you play darts or enjoy axe throwing, I will send you my picture for the target.
Thank you so much !!!!!!