5

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).

4 Answers 4

8

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

Sign up to request clarification or add additional context in comments.

5 Comments

unfortunately, this gives thefile urlencoded an AppleScript has no native urldecode function.
I up'd that too soon see my modification to the answer
@mb21 see comment and revision :)
The script does work here (4 years after the question was asked, on 10.8), but urldecode(...) doesn't seem to do anything, thefile is file://localhost/path/to/file.ext before and after - where it should be /path/to/file.ext so it can be interpreted as a POSIX path.
Instead of schell script getting the path can be done by adding use framework "Foundation" and return (current application's NSURL's URLWithString:thefile)'s relativePath as text
2

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.

Comments

1

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.