Detecting whether a file is selected or about to be renamed

Coming from Windows I'm used to delete files simply with the BACK-key.
I can setup a key for that in Finder. But then I cannot rename files anymore. They get deleted. Mh.
Somehow I need to detect if the file in Finder has a cursor in its name or not.
To make it even more secure I want to make sure I can only delete files with me as owner. How do I get the path of the selected file in a variable?
Thanks for any clues.

On mac you can delete files with cmd+delete. If you want to check for the file owner first, you can do something like this:

Delete file if owner.kmmacros (2.8 KB)

Quick tip: To rename a file in the Finder on macOS, select it and press return. Doesn't work if multiple files are selected though.

For example:

Path(s) of any file(s) selected in Finder.kmmacros (3.9 KB)


return Application("Finder")
    .selection()
    .map(
        x => $.NSURL.alloc
            .initWithString(x.url())
            .fileSystemRepresentation
    )
    .join("\n");

Wouldn't using the KM token %FinderSelections% be easier than calling on JXA?

1 Like

Thanks for all your suggestion to get the path of selected files. KM is a real powerful tool!
But my first problem still is: If I rename a file (cursor in filename) BACK should delete a char in file name as usual, if I only selected a file BACK should delete the file. Any ideas?

You seem to be asking a question about the macOS Finder interface and not KM. If I'm correct, then I think @Wseries answered your question above: After you select the file by clicking it, press Return, which then enables you to use your cursor keys to move within the name and add/delete characters.

Yes -- realise that macOS and Windows work in different ways, and embrace that difference. ⌘-Delete for file deletion helps avoid accidents.

But if you really want to do this, you'll have to determine if a name field is "active" in the Finder -- if it is then you are in "renaming" mode and Backspace can behave normally, if not then you are in "item" mode and you can make the backspace key delete the item(s).

If you want to be "secure", work in a Standard User account and only use your Administrator account when you need to do some admin. But most people don't go to that extreme, and it's quite easy to check an item's owner.

This macro has been tested -- but, given that a deleteing things is serious business, you should run your own checks! And you should put the macro in a Group that is only active when the Finder is frontmost (as in the download) to avoid unwanted behaviour of the Delete key in other apps...

Delete Finder Item with Backspace.kmmacros (10.7 KB)

Image

I think most of the actions are obvious, but ask if anything is unclear.