Get Preview Window Path

I'm not quite sure where this action/script came from (the AppleScript looks more complex than one I would write), but I use it all the time and its very handy. It gets the path of the front window in Preview. It should work in pre 10.9, but 10.9 or later is much easier since Preview regained its AppleScript dictionary, so the simple AppleScript:

path of document 1 of window 1 of application "Preview"

is all you really need, but packaged as a plug in is still a bit nicer.

Get Preview Window Path.zip (17.1 KB)

2 Likes

Nice, thanks. I know this could be used to display the path, but are there some other uses for it?

My most common use is to rename the file, and then file it away either into a folder in the Finder or more often into Yojimbo.

Or something more complicated to file my Optus bills:

1 Like

Peter,
How do I add the contents of the zip file you’ve attached into Keyboard Maestro to activate the action/script?

Download the zip file, and drop it on to the Keyboard Maestro app.

Thanks Peter, I unzipped and then scratched my head!

Yes, Keyboard Maestro uses .zip as a package format for Third Party Plug In Actions as well as Status Menu Icons. I could use a different extension, but that is similarly confusing when it’s just a zip file.

FWIW a Yosemite Javascript variant

Path of front Preview document.zip (4.6 KB)

(function () {
	var	ws = Application("Preview").windows(),
		ds = ws.length ? ws[0].documents : [];
	return (d = ds.length ? ds[0] : null) ? d.path() : "";
})()

Can this be applied to Skim, a PDF reader and Highlights, an app that displays notes in PDF files?

Skim version here:

(I'll have a look at Highlights)

Highlights doesn't have a scripting dictionary, but you should be able to get the path of its front document with this more generic plugin

That looks like something I wrote long ago and scarfed the vanilla decode routine from here, but I won't swear up and down.

You can make it generic to the front application easily enough:

tell application (get path to frontmost application as text) to set frontApp to its name
tell application "System Events"
  tell process frontApp
    set thefile to value of attribute "AXDocument" of (first window whose subrole is "AXStandardWindow")
  end tell
end tell

if thefile ≠ missing value then
  urldecode(POSIX path of POSIX file thefile)
else
  error "No file has been saved!"
end if

on urldecode(theText)
  # Content omitted
end urldecode

More recently I'd shell out to Perl, Python, or Ruby to do the decode, but at this point it makes more sense to use JXA.

-Chris

I was trying to use the Skim macro. I couldn’t find find the zip work. Dragging the zip into the editor, KM doesn’t respond.
I never felt this stupid.

Hey @Desalegn,

What makes you think you can drag onto the editor?

That doesn’t work, and as far as I know never has.

Dragging to the Keyboard Maestro icon in the Dock works fine.

With .kmmacro and .kmaction files you can double-click them in the Finder or drag and drop them to the Keyboard Maestro editor’s Dock icon.

I usually use the drag and drop method with the keys held down, because this ensures the imported items are disabled. (Not necessary for plug-ins.)

-Chris