Saving a LibreOffice Document in the Background

I have a LibreOffice spreadsheet open all the time on my MacOS Mojave machine, running KM 9.2. I make entries on this from time to time while working, and normally never close it. Overnight the machine sleeps and wakes up when required in the morning.

I need to ensure that the spreadsheet is saved from time to time, so that work is not lost e.g. if we get a power cut - doesn't happen often but when it does it could be serious if the file is not saved!

There is an autosave function in the spreadsheet but I don't want to rely on it.

I have created a KM macro that saves the spreadsheet every x minutes, when it is active, and that works ok. All it does is to type Cmd-S to save at the appropriate time.

But it does not work if I go to the spreadsheet, make an entry and then leave the spreadsheet, all while the KM timer is still running.

I want the macro to work while the spreadsheet is hidden but still running, and the Cmd-S keystroke must of course apply to the spreadsheet, even if I'm working on a different app at the time the KM timer triggers the Save keystroke

In the KM Wiki it says triggers for a macro include whether the app is one or more of running, at the front or hidden. When creating the macro in KM 9.2 however, I only have trigger options relating to Launch, Quit Running or Activate/IsActive. Nothing about the app being Hidden.

When I make an entry in the spreadsheet & then go to another app, I think the spreadsheet is "running but is hidden". So I think my macro needs to run even when the spreadsheet becomes or is hidden. And as mentioned, it must send the Save keystroke to the spreadsheet evwen though ity is not being used.

Has anyone any suggestions as to how this macro can be made to work please?

Many thanks in advance for any help you can give

1 Like

Here is an untested idea:

Create a KM Macro with a Periodic Trigger (looks like that is what you have)
Macro Actions:

  1. IF/THEN LibreOffice is frontmost, THEN:
    • Pause Until IDLE function is > a short period like 1-2 seconds.
    • Select Menu "Save"
  2. ELSE:
    • Type Keystroke "⌘S" to LibreOffice in the background
      (an option in the Gear popup menu)
  • image

Again, I have NOT tested this, but it looks good to me.
Test using a LibreOffice test file that can be lost.

Let us know if this works for you.

Thanks so much for this Michael - I've worked with it and now got a macro that seems to do what I want. Further testing needed (of course!), but all good so far. I had not used "Type to in the background" before, nor had I discovered the IDLE function, so that's 2 new things in one day! And I'm sure there are many many more unknown possibilities.

This is how the macro looks, in case you or anyone else are interested (not sure if this is the correct way to upload a screenshot)

Screenshot 2021-03-16 at 12.07.53|690x702

1 Like

To upload any image, it is best to either paste the image into the Forum editor, drag/drop the image file, or use the upload button at the top of the editor window.

Thanks again Michael. The revised macro I created following your first response works well, but has revealed one further point which I did not mention in my original post, & I'm wondering if it can be solved.

The "Type Keystroke ⌘S to LibreOffice in the background" only works on whatever is the front window of LibreOffice when the macro runs, so if I have 2 spreadsheets open at the same time, and I'm working on the 2nd one (not the one I want to save automatically) or I'm using a different app, the ⌘S keystroke will only save whichever of the LO spreadsheets happens to be at the front of the LO stack at the time, whether or not LO has focus.

For other purposes, I can use "Title of the Front Window of the Application is [whatever]" as a condition, but I don't see that I can incorporate this when using the "Type ⌘S in the Background" command

Do you think it is possible to include a window title check in the Background command?

Many thanks again for your help

There is no "Background" command. If you mean the option of a Type Keystroke to send to an app in the background, then no. It will send to the frontmost window of that app.

Take a look at the KM Wiki article Window Related Tokens.
There are a number of KM Tokens that will provide window information on the FrontMost app.
I don't see any KM tools for working with windows of an app that is in the background.

If you want to get info, or control, the windows of an app that is in the background you will most likely need to use AppleScript or JXA.

Here is an example AppleScript to get a list of windows in the Excel app, which is in the background:

tell application "Microsoft Excel"
  set winNameList to name of windows whose visible is true and window type is window type workbook
end tell

You would need a similar script for LibreOffice. The properties may be different, so you will need to review the LibreOffice Scripting Dictionary. I do NOT have LibreOffice, so I can't help with that.

Thanks once again Michael - I've used Applescript in the past but it's a bit rusty at the moment! But I'll give it a whirl, and see if I can make it happen, though it may prove to be more trouble than it is worth! Perhaps I should just make sure I only ever have one spreadsheet open, or at least realise that if there are 2, the saving may not work as intended.

1 Like

Hey Tim,

LibreOffice is not AppleScriptable, although it's fairly amenable to UI-Scripting.

I wrote a script that brings a named target window to the front if it's not already, and the rest of the macro does as @JMichaelTX suggests – sends a keystroke to the app in the background.

The problem is the script is not reliable if LibreOffice is hidden.

The only way I've found to get the macro to work reliably if more than one window is open is to assign a keyboard shortcut to File › Save All (Ctrl-S) using the System Keyboard Prefs.


Having documents that are completely unsaved will cause the Save-As dialog to open, so you have that pitfall to deal with – but as long as you're careful I think you can get it to work.

I've left the AppleScript in the macro, just as an example.


P.S. It looks like LibreOffice can be scripted with several languages but only from inside the UI.

LibreOffice ⇢ Save Target Window v1.00.kmmacros (7.4 KB)

Perhaps it is time to test and reevaluate use of the built-in auto-save function:

I have found that, for the most part, that the auto-save works well.
Why not test it to determine if you can trust it?

BTW, you could also write a macro that does a SAVE when you change windows from your target spreadsheet.

Thanks Chris - that's very helpful indeed, & I'm most grateful! I shall now have a play with your macro, and try to incorporate it into mine to cover all I need


Thanks Michael - much appreciated. I'll follow up your suggestions