4

OS X's ability to rename or move a currently-open file right inside the application is really handy:

enter image description here

To take it one step further, is there a trick to actually delete the current file inside the app also? (Obviously 'closing' the file would need to be the second half of the process.)

It would save a lot of time to not have to close the file, manually locate it in Finder, then move it to Trash - but but just delete it right there and then.

I have tried dragging the 'file' (with the file icon to the left of the file name in window title) to the Trash icon on the Dock, but that doesn't actually move it to Trash.

The best I can think of is to add ~/.Trash directory as an item in Finder Favorites, then change the 'Where' in the above screenshot to the created 'Trash' Favorite shortcut, followed by a Cmd+W to close it.

It's a little clunky, but it's not bad.

So is there a nifty Automator workflow / AppleScript (which can then be easily assigned to a hotkey), to make the above process quicker? A single AppleScript that could perform both the step to move current file to 'Trash' directory and then the subsequent Cmd+W keypress after a 200ms gap e.g., would be ideal.

1

I use this AppleScript:

tell application (path to frontmost application as text)
    set f to POSIX file (path of document 1) as text
    close document 1
end tell
tell application "Finder" to move f to trash

A hackier version that works with a few more applications:

try
    tell document 1 of application (path to frontmost application as text)
        set f to path
        close
    end tell
on error
    tell application "System Events" to tell (process 1 whose frontmost is true)
        value of attribute "AXDocument" of window 1
        set f to do shell script "x=" & quoted form of result & ";x=${x/#file:\\/\\/};x=${x/#localhost/};printf ${x//%/\\\\x}"
        keystroke "w" using command down
    end tell
end try
POSIX file f
tell application "Finder" to move result to trash
  • It seems it is not working anymore, at least in macOS High Sierra. Do you know how this AppleScript should look like now to still be valid? – Michał Kalinowski Dec 17 '17 at 22:58
0

Try this AppleScript in an Automator service (you can assign a hotkey to any service):

on run
    set x to path to frontmost application as string
    tell application x to if exists document 1 then
        try
            try
                set p to path of front document -- some app use --> path of document
                p
            on error
                set p to file of front document -- some app use -->  file of document
            end try
            my moveToTrash(p)
            close front document
        on error
            my speakText("can't delete this document")
        end try
    else
        my speakText("You have no open document in that application")
    end if
end run

on moveToTrash(p)
    if p starts with "/" then -- some app return posix path
        set tFile to p as POSIX file as alias
    else -- some app return HFS path
        set tFile to p as alias
    end if
    tell application "Finder" to delete tFile
end moveToTrash

on speakText(t)
    say t
end speakText

I tested the script on these applications ("BBEdit", "TextWrangler", "TextEdit", "Preview" "QuickTime Player", "Pages", "Numbers", "Keynote", "Automator", "AppleScript Editor", "Xcode") on Mavericks

This script will not work some non-apple applications.

To assign a keystroke combination to the newly created service:

Open the System Preferences application and navigate to the Keyboard preference pane, and select the Shortcuts tab. From the list on the left of the preference pane, select the Services category. A list of the installed services will be displayed to the right. Scroll to the last category titled General, and locate the service you just created. Double-click to the far right of the service name to activate the keystroke input field and then type the key combination you wish to assign to the service. Close the System Preferences application.

You must log in to answer this question.