Making a Fully Background Macro

Hi all,

Using Keyboard Maestro, I'm trying to create a simple log file macro to keep track of various bits of data.

For now I set up a simple text document (-test1.txt ) to collect the info.

What I have below does work but it is not ideal. Currently the macro will open the text file in BBEdit - and the Applescript will paste in the data in the keyboard maestro variable 'clip1', then System Events will simulate "Save File".

So basically that is what I want however, since it is not happening in the background, this macro will operate in the foreground for a few seconds and possibly interrupt my active workflow.

The ideal thing would be if this basic idea could operate as a background process so as not to interfere with the user workflow.

  • EX: I have tried a few things but so far unsuccessful at making this a fully background process. Currently in order to save the document - it seems to need to be in the foreground.

If anyone has any ideas how to make this it's fully background process, I would love to hear them.

Thanks in advance - DAVE


set variable clip1 = "This is a test."
open ~/Desktop/-test1.txt (with BBEdit)

Execute Applescript:

set myKMVar to "clip1"
set myASVar to "TBD spank"

tell application "Keyboard Maestro Engine"
   set myASVar to getvariable myKMVar
end tell

set mydata to return & myASVar

tell application "BBEdit"
   -- activate
   select insertion point before first line of front window
   set the selection of text window 1 to mydata
end tell

tell application "System Events"
   keystroke "s" using {command down} -- save command from menu
end tell

Perhaps take a look at the “Append Text to File” action? It should do what you want without having to use a text editor application.



HI Kevin - YES that totally did the trick - THANKS!

1 Like

Hey Dave,

Well... You've taken an exceedingly simple task and made it somewhat complicated due to prepending the new text in the note file.


I've done that here with BBEdit, and I've made the macro respect the visible state of BBEdit.

The note file will be opened if not already open.

Prepend Text to Note File Using BBEdit v1.00.kmmacros (7.3 KB)

If you're willing to append your text to the note file then Kevin's (@KevinCoates) suggestion makes it super simple. It can be done with one action depending upon your source text.

Write or Append a File.kmactions (693 B)

** This is an action – it will import directly into the macro currently being edited in the Keyboard Maestro Editor.

I can write a shell script or an AppleScript or use Keyboard Maestro actions to prepend text to a file, BUT this requires reading and writing the whole file – because that's how file systems work.

It can be done, but it's not as efficient as appending.

Nevertheless – it's still fairly simple to do with Keyboard Maestro.

Prepend Text to a File with Keyboard Maestro Actions v1.00.kmmacros (6.9 KB)



Great solution, Kevin!

@dealtek had a good SIMPLE solution, it just want not doing it in the background as he wanted.

Simple perhaps, but very fragile.

Here's a version that's similar but shored up a bit.


Prepend Text to Note File Using BBEdit v2.00.kmmacros (7.7 KB)

1 Like

@dealtek's original macro looks fine to me.
What specifically makes it look "very fragile" to you?

He did not mention having any issues which his macro.
He just wanted for it to run in the background.