Writing to file creates a deleted copy if file already exists

Hello,

In the latest version of KM 11.0.2, when my macros write data to files (some every minutes), instead of replacing the contents in the existing file, it first deletes the file (appending the date to its name), and then write data to a new file — thus filling my trash.

I don't recall this behavior in past versions of KM. Is this new, and is there a way to disable that behavior, and simply replace any data in the existing file with the new data being written?

Thanks!

Can you post the macro, or at least the relevant bits that write the data to the file?

-rob.

Interesting -- happening here, too (KM 11.0.2, macOS 14.3.1). One simple action will demo it, just run from the cogwheel's "Try Action" when your clipboards loaded with text and watch your Trash fill...

image

Not happening in Terminal with, for example:

echo "Here's is some stuff" > ~/Desktop/testOverwrite.txt

I was going to reply to Rob that it happens with every single "Write to file" action, in every monitoring macro I've build over the years. But Nige, demonstrated it already.

For example, I have a macro that writes an "I'm alive" file to a Resilio Sync'd folder for every Mac that should be running KM, which another macro checks and would alert me if KM was down on any machine. Here's what the trash on my current MacBook Pro looks like now:

Wow, that's wild! I guess I never use that action to overwrite an existing file. Looking through my macros, they mostly are write-once, or if I save over a previous version, I do it in the shell, which doesn't have the issue.

This seems to be a Keyboard Maestro change, @peternlewis ? Something to make it easier to recover accidentally lost data? Maybe there's a hidden pref to disable that behavior. If not, it seems ...

tell application "Finder"
	empty the trash
end tell

...will become an often-used AppleScript action in such macros :).

-rob.

Hopefully @peternlewis can introduce a way to disable that behavior. Emptying the trash every time wouldn't be ideal. :slight_smile:

You could convert all the writes to shell script actions, but that's probably a ton of work to avoid something that's hopefully easily avoided :). I did look through the command line preferences wiki, but didn't see any entries related to deleted file versioning.

-rob.

It might not be a KM change -- it may be that Apple's changed the API that KM is using to write the data out.

@dafacto, why are you writing out to "new" files so often? Could you make it more like a log file and "append" instead? Or perhaps precede your "Write" action with a "Delete a File" action to stop the Trash filling?

Ah, good point. I was curious if it might be related to the change that makes it tougher to write to files in the user's home, so I set up the test to an external volume. Same result.

-rob.

It felt more like an OS-level change than an application one. Except I've just remoted in to a 10.14 machine and it's happening there too :frowning:

I'm writing data into shared/sync'd files, that need to be read by other computers.

I could write a delete file action; but it'd be a lot of work, since I have a lot of these write actions accumulated over time. :frowning:

I feared as much (on both counts).

Since it does look like a change in KM behaviour, I think the best you can do for now is to set up a scheduled "Delete" of the files from your Trash. Check the pattern carefully against what's in your Trash (try it with ls first), but something like

#!/bin/zsh
/bin/rm /Users/yourShortName/.Trash/*\ [0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9].*

...in an "Execute a Shell Script" action should work if your deleted files have the same name format as mine. You could nail it down even tighter if they always have the same extension.

What I ended up doing, though it was a bit of work, was searching for all "Write" actions, and preceding them with a "Delete" action. :man_shrugging: At least that solved the problem.

1 Like

This is not new behaviour, it remains unchanged since version 8.1.

As described in the documentation, Keyboard Maestro uses the system NSFileWrapper to write the file. NSFileWrapper will overwrite anything, including directories. So yes, Keyboard Maestro actively trashes the file before writing the file, since otherwise it could do extensive damage. The alternative would be disallowing overwriting entirely.

Why would I introduce a way to have it inadvertently delete data? If you want to delete the file yourself, you can use the Delete a File action.

1 Like

I'm confused - you've never had this happen, so why would you now need to empty the trash frequently?

Personally, I never empty my trash. It currently has 1500 items in it. I have free disk space so why would I bother/risk deleting it? If I run out of disk space then I might, but more likely I'd use a tool to find where the disk space is and actively delete that.

Sorry, I was trying to say that if someone is using that action, and they didn't want their trash filling up, that script would be one way to do it.

-rob.

1 Like

I would never advise someone to empty their trash regularly (or indeed, ever, not until they run out of disk space).

I worked with a few people who could not stand the site of the full trash can icon. I said they could just hide the Dock, but they still said it bothered them when it appeared. They emptied their trash incredibly regularly.

Personally, I wouldn't do it, and I'm like you–my trash can accumulates tons of files.

-rob.

I used to paste a custom icon of a regular trash can over the trash icon. I'm not sure if that will work in modern systems - this was back in the day when it was an icon on the Desktop.

2 Likes

Wow, that's amazing that it's been there since 8.1! I've never noticed it before! As you said, it's not really a problem having these files accumulate in the trash. But since I never noticed it, I just assumed it was a new behavior, and if possible was hoping to disable it! :slight_smile: