My goal is to make the Delete key (without any modifiers) move selected files to the trash using OSX’s Finder, similar to Windows Explorer. I’ve managed to do it using a great utility application called Spark. Spark allows me to assign the Delete key to do something when in Finder only. I can have it type the Finder shortcut key (command-backspace), or activate the File > Move to Trash menu item, or run an AppleScript. In each case, the file is moved to the trash successfully. It works well … a little too well.
If I’m in the process of renaming a file and press the Delete key, the file is moved to the trash instead of the letter ahead of the cursor being removed. I can’t think of any clever way of working around the problem. The only thing I can think of is to use AppleScript to determine if the text editor is open in Finder in order for the user to rename a file … or, simply put, determine if the user is renaming a file in AppleScript. Something tells me this goes beyond the capabilities of AppleScript. Can it be done? Any other suggestions would be most welcome. Thanks.
UPDATE:
Thanks to adayzdone, I have a working script now. Create a hotkey for Finder in Spark, assign the Delete key to it, and use this script as the action:
tell application "System Events"
tell process "Finder"
if not (exists text field 1) then
tell menu bar 1
tell menu bar item "File"
tell menu "File"
click menu item "Move to Trash"
end tell
end tell
end tell
end if
end tell
end tell
UPDATE 2:
Here’s an updated script based on adayzdone’s cleaner version. I added a line to reissue the delete key when you’re renaming a file and press delete. It has a problem though: you have to press the delete key twice for every one character removed. Also, you can leave things in a weird state when done renaming the file: if you press delete to remove a file, it might not work … requiring a second key press of delete before things return to normal. I think the problem is from issuing the delete key from within the event being handled by Spark. I’ll try to find a solution.
tell application "System Events"
tell process "Finder"
if exists text field 1 then
keystroke "d" using {control down}
else
click menu item "Move to Trash" of menu 1 of menu bar item "File" of menu bar 1
end if
end tell
end tell
Try:
Another approach is: