How can I quit Terminal after deleting files?

The attached macro runs Terminal to delete files in Downloads (chess games I've played). It quits the Terminal but fails to delete the files. If I remove the Quit action, the files are deleted, but the Terminal window remains open. I haven't found a way to make both actions execute.

What's causing this, and how can I fix it?

+ delete games.kmmacros (4.3 KB)

This is a very wild guess as I'm not terribly experienced.
I have a couple of apps (using Monterey) that I have to hit ⌘ Q a second time.

What happens if you add another 1.5 second pause and another Quit?

Thanks for the suggestion, but adding another pause and quit doesn't do anything. The first Quit executes, but the rm command does not (same as before).

Easiest fix is to not use Terminal!

Try using an "Execute Shell Script" action instead, putting your command in that -- the macro will then delay until the script step completes which (unless you've set it to "asynchronous") will be after the file deletion has completed:

Also worth noting that the "Delete File" action has a "Delete" option which similarly completely deletes a file without warnings and without using the Trash. So you could keep the whole process within KM, without the "Execute Shell Script" step, by creating a file Collection then doing For Each on that.

1 Like

The first thing you should try is adding a return to the end of the string being typed into terminal, like this:
KM 0 2022-07-10_20-28-03

Thanks, that seems to work! It’s odd, though, since the return wasn’t needed until I added Quit.

Since it works (even for reasons that are unclear) maybe you could mark it "solved" for the benefit of others!

How would I do that? Besides, as a mathematician, I was trained not to consider a problem solved until I understood it.

Well, as one mathematician to another - this isn’t mathematics :wink:

There’s a checkbox at the bottom of each reply/post in the thread - just check the one that’s on the solution. It’ll end up looking like this:

Mathematics is not the only field in which it’s useful to understand problems and solutions.

But thanks again!

1 Like

Well, my suggestion was based on the usual operation of the command line (terminal) where a command needs to be followed by a return for it to be acted upon. If there is something else happening then perhaps someone else can explain.

It didn’t need one until I added Quit (which isn’t a Terminal command). Someone else could offer an explanation, but I don’t know if that will happen after I’ve marked the problem solved.

The Quit action terminates the shell, so if you have a command that is still waiting for a return, it won't run and so the files wouldn't be deleted.

As for why it worked for you before you added the Quit action I can't explain because when I run your macro without the Quit action, terminal just sits there waiting for me to type return.

BTW - this post is evidence that something marked solved can still be contributed to.

It can, but you’re responding to my responses to you, not to an unsolved problem. When I run the macro (without Quit), Terminal deletes the files and waits for me to close it. I’ll post a video later.

Anyone can respond to any post at any time provided the thread has not been locked - and this discussion is most certainly not locked!

In which case you already have a return character at the end of what your macro is typing into terminal. To make sure I’m wrong about this, please can you post the actual macro that works (I.e. without the Quit) so I can have a look?

Anyone can, but will they, if they see "solved" in the original entry?

I didn't realize it, but I had a physical return in the box (not a return character at the end of the line) ... and now we have an explanation! Thank you.

I do sometimes find myself wishing KM text boxes had the equivalent of "Show invisibles", to make these things easier to spot...

Nice that the problem is solved, but I'd still urge you to do this as an "Execute Script" action instead of within Terminal -- you'll get rid of the needless pauses and remove the chance (however slim, in this case) of Terminal being told to Quit before your command has completed. As an example, try the following (don't worry, du is totally non-destructive, we're simply trying to get the disk space used by your home folder):

Too Soon! Test.kmmacros (2.9 KB)

Unless you've got a teeny-tiny home directory and a very fast machine, you'll see the problem you could have...


One of these, you mean?

Screen Shot 2022-07-14 at 20.41.02

I'd have to learn one of those languages, I suppose?

Shell scripts can be one-liners. For example see @Nige_S' supplied above.

The Keyboard Maestro shell script wiki page is important reference material.

1 Like

As shown earlier, it's the "Execute a Shell Script" action.

You already know enough to do what you need -- you're typing it into Terminal!

"Execute a Shell Script" is almost the same as as "Launch Terminal, type in the command, remember to press the Return key, wait for the command to complete, Quit Terminal". The action defaults to using the bash shell while you are probably using zsh in Terminal, the "shell environment" will be slightly different, and it's less easy to interact with the command (eg to type in a password), but most of these differences can be overcome. And most of the time they aren't important -- commands provided with your OS (rm, ls, and a whole host more) run the same regardless. It's only when you start installing your own packages that things like $PATH matter, and if you're doing that you probably know enough to cope.

The benefits are that you don't have to launch and manipulate another application, don't have to mess with "Pause" actions, don't have to deploy fancy "wait" conditions to make sure the process completes before your macro continues, that you can use the results of the shell command later in the macro, and probably more!

If you are already using a shell via Terminal it's a very short step to using the "Execute a Shell Script" action in KM, and you'll open up a world of possibilities...