Keyboard Maestro 8.2.4 “Delete Current File in Preview” Macro

2 Macros to Delete Current File in

I often have temporary files open in Preview that I need to do something with, and then I want to get rid of them.

Today I created two macros which are designed to delete the front-most file in Preview.

(Note: Technically, files are moved to the trash, not deleted, but you get my meaning.)

Here’s the first macro (it’s the more complicated of the two):

Press ⌘⇧D while in and it will prompt you to see if you want to delete the current document. It will show you the filename to help confirm that you are getting rid of the proper file.

If you confirm, it will close the document and move it to the trash. It works properly with tabs and multiple documents in the same window.

The second macro (not shown in the image) is mostly the same thing, except for two important differences:

  1. It used ⌘⌥D for a keyboard shortcut

  2. It will NOT prompt for confirmation, it will simply close and delete the current document.

I hope these are helpful! Tested on Mac OS X 10.13.6 with Keyboard Maestro 8.2.4

2 Preview Macros.kmmacros (6.4 KB)


Actually, I changed that first AppleScript block to this:

	tell document 1 of application "Preview"
		set f to path
	end tell
end try

return f

so I didn't end up with a POSIX path that I just changed back to a normal path with the shell script. (DERP!)

Here are the new macros:

Preview-Delete-Current-File.kmmacros (5.7 KB)

I like the thought of this, but I fear I would use it automatically too often and then suddenly want to undo which takes more time than just thinking properly in the first place! :drooling_face:

Why do you need the shell script ? Since you’re using AppleScript actions anyway, you can do the whole thing in one single AppleScript action.

Mostly because I suck at AppleScript and I'm really comfortable with shell scripts :slight_smile:

Here's the macro action combo that I use:

property A : a reference to system attribute "KMVAR_AppName"
property application : a reference to the application named A

set D to my application's front document
set f to D's «class ppth» as POSIX file

tell application "Finder" to delete f

close D

if not (exists my application's document 1) then quit my application

I have this inside a macro group that makes it available only to specific applications (I trigger the macro with ⌘⌫, so didn't want it interfering with Finder, but it's a good idea to cordon off a macro of this nature anyway). As you can probably infer from the code, it's a general solution for multiple applications that are scriptable and expose their NSDocument object to AppleScript.

If you want to include the alert dialog before confirming to proceed with the deletion, then the tell application "Finder" line can be changed to:

tell application "Finder" to if the button returned of ¬
	(display alert ("Really delete the following file?") ¬
		message f's POSIX path ¬
		buttons {"OK", "Cancel"} ¬
		default button ("OK") ¬
		cancel button ("Cancel")) ¬
		= "OK" then delete f

The two lines following that close the document and exit the application if there are no open documents remaining, but will only execute if the user pressed "OK" on the alert, as pressing "Cancel" causes AppleScript to throw an error (intentionally), which we want.

1 Like

Hey Tj,

This will work in pretty much any app, regardless of whether it is scriptable:

Finding the File Path for the Front Document in the Front Application