How to Create a Macro with Incremental Repeat Actions After Each Task Completion?

Hi everyone,

I’m working on a Keyboard Maestro macro to automate clearing unwanted files in the “Show Package Contents” menu of multiple files in a folder in Finder. Here’s what I have so far:

  1. The macro selects the first file in the folder by selecting Finder and pressing the down arrow key.
  2. It then correctly enters the package contents and removes the unwanted files.

However, I need help with the following:

After processing the first file and returning to the original folder, I want the macro to automatically select the second file by pressing the down arrow key twice. For each subsequent return, it should press the down arrow an additional time.

In other words, after each file is processed, the macro should increase the number of down arrow presses by one for each new file. So, if it has already processed one file, it should press down arrow twice for the second file, three times for the third file, and so on.

Can anyone guide me on how to set up a repeating action in Keyboard Maestro where the repeat count increases by one each time the macro runs?

Thanks in advance for your help!

You need to store the "number of repeats" in a global variable (most variables are global, of course.) You will also have to come up with a way to initialize the variable before the first value, and probably you need to clear the variable after your last time. Here's how I would do it, using a "Try action." I would create a global variable, called GlobalNumber. If that variable is empty, it will initialize it to 1 (whenever the "+1" action fails due to the variable being empty), otherwise it will increment it.

In this example I just play a sound rather than press a Down Arrow key. You can adapt it to your needs.

I'm providing only this part of the solution because I'm not sure I understand your description of "what you have so far." I think it would be helpful if you posted what you have so far. Then I could probably have modified your macro to incorporate this code.

Thanks! I'm not too familiar with setting these variables, but this indeed seems like the way I'd have to go. I am uploading my Macro here if you'd like to take a look.
WIP .kmmacros (31.4 KB)

Interacting with the Finder via keystrokes might not be the best way to do this. I don't know what you're trying to remove, but I'll give you an example of how you can delete a named file within the package contents of all files in the current Finder selection. For this example, I'm going to delete a plist file within the Resources folder of some Logic Pro project files:

Delete Resource Files for Selection.kmmacros (39 KB)

Macro screenshot

Of course, you'll need to adjust the path to suit your needs, but it's a very simple and reliable approach.

Thank you, this looks incredible! Can you walk me through how to set the path variables to test this?

For clarification, I am trying to delete the project file backup files of multiple Logic Pro projects at once (essentially, run the "clean up" command on multiple projects in one go to reduce file sizes).

Give me an example path of a file or folder that you typically want to delete.

You probably know this, but deleting the backups is not the same as a proper cleanup. It won't delete unused audio files, for example, which is typically what will reduce the most project bloat. What it will do is remove backups files, which take up negligible space.

Nah, the backups by far take up the most space for me. I am rarely dealing with audio files though, so of course that would be the case. I can bring a project down from 2GB to 200MB by deleting those backups.

If you do have tricks for more extensive batch clean-ups though, that'd be awesome.

I made a "TEST" folder on my desktop, so, /Users/[MY USER]/Desktop/TEST

What do you mean by "backups"? Do you mean the alternatives? Here's the package contents of one of my projects:

Sorry, I didn't phrase my request very well. What I meant, was: tell me what exactly in the package contents you're trying to delete. There's no need for an explicit project path, as we'll be dealing with the Finder selection.

Not exactly, the "Project Files Backups" are within the "Alternatives" folders. The exact path would be: [Logic Project]/Alternatives/000/Project File Backups. It is the "Project File Backups" folder I intend to delete, as I believe the other files at the root of these "Alternatives" folders include necessary files for the project to open and run, and are not backup saves.

I rarely create project alternatives, so directing just into the "000" folder should suffice.

Ok that's pretty simple then. Assuming you want to delete the entire backups folder, you can do this:

Delete Logic Project Backups.kmmacros (39 KB)

Macro screenshot

You should test this on a project that you have already backed up, but if anything goes wrong, the removed files can be recovered from the trash.

SOLVED! This is brilliant and accomplishes the job with ease and efficiency. Thank you so much!

1 Like

Glad to hear it. Have you checked out the Logic Megathread yet? I really need to clean up the thread, but for now:

Thanks! Yes. I have checked it out -- would you recommend that new LP macro requests go there, as opposed to new posts? (I have started one or two new posts since, but can migrate them to the megathread)

1 Like

That thread was meant as a repository. You can do whatever you like but if I were you I'd start fresh threads to keep it clean.