Abhi
Abhi

Reputation: 1147

How do I get the path of a document open in Preview using AppleScript?

I have tried this but it doesn't work:

tell application "Preview"
    set myfile to path of document 1 of window 1
end tell

I have verified that my Preview is scriptable (NSAppleScriptEnabled).

Upvotes: 5

Views: 2227

Answers (4)

Chris Redford
Chris Redford

Reputation: 17778

To get the path to the current document accurately even if Preview is in full screen mode, use:

tell application "Preview" to return path of document of front window

Upvotes: 1

user137369
user137369

Reputation: 5706

Simplest way is:

tell application "Preview" to return path of front document

Or, to fit your example:

tell application "Preview" to set myfile to path of front document

Upvotes: 1

mcgrailm
mcgrailm

Reputation: 17640

I did a little research and found this

 tell application "System Events"
    tell process "Preview"
        set thefile to value of attribute "AXDocument" of window 1
    end tell
 end tell

-- macos comes with php installed you can decode this file name using php
return do shell script "php -r 'echo urldecode(\"" & thefile & "\");'"

which i found on macscripter

Upvotes: 8

RyanWilcox
RyanWilcox

Reputation: 13972

On my 10.6 system Preview doesn't have an Applescript dictionary. This is something that scriptable applications need. While Preview SAYS it can do Applescript, it really can't.

It has no way to translate the "path of document 1 of window 1" into anything meaningful.

Usually people use GUI Scripting to interact with Preview, or they find another way.

Upvotes: 2

Related Questions