4

I have recently moved a large research collection of PDF files, from a Snow Leopard 10.6.8 TimeMachine disk, to my new rMBP (to the internal flash storage). I did this manually, not through the TimeMachine interface. The rMBP runs Mavericks 10.9.3.

When I double click the PDFs, they open without a problem in Preview.

But, when I write a small AppleScript to open one, for example:

tell application "Preview"
    activate
    open "/Users/me/Desktop/test.pdf"
end tell

I get the following error on executing the AppleScript:

The file “test.pdf” couldn’t be opened because you don’t have permission to view it. To view or change permissions, select the item in the Finder and choose File > Get Info.

I look in Get Info for test.pdf. In the "Sharing and Permissions" tab, it does not matter what I set permissions to, even if all users can Read & Write the file, I still get the error.

Conversely, if I download a PDF from the internet, straight to the desktop, and execute the applescript to open it, it opens in Preview without a problem.

(p.s. I need to open PDF by AppleScript, as part of a larger system I have, I am not just doing it for fun!)

  • 1
    try using "sudo" – Ruskes Jun 1 '14 at 18:28
  • 1
    Have you tried using something like do shell script open -a Preview /Users/me/Desktop/test.pdf? – Édouard Jun 2 '14 at 5:31
2

for me do shell script.. failed, but this one below worked.

set filepath to POSIX path of "Users:student:Desktop:Yourfile.pdf" try set command to "open " & quoted form of filepath do shell script command end try

Note: Instead of ":" for file path I used "/", a modification one can try.

Source of Code: MacRumors

  • 1
    I confirm that this solution also works for me. – edison1093 Dec 11 '14 at 12:04
  • @edison1093 could you rate up the answer? if the solution worked. – curiousMonkey Dec 12 '14 at 10:51
  • If you want to overwrite the file or similar, I got it to work like so: set file_path to POSIX path of "Users:student:Desktop:Yourfile.pdf" tell application id "com.apple.Preview" activate set theDocument to open quoted form of file_path save theDocument in file_path – 56kNULLMODEM Mar 16 '15 at 1:00
0

The comment provided by Edouard fixed this problem. Bizarrely, using the command line to launch Preview bypasses the permissions issue.

Here is the modified AppleScript, made robust to PDFs with spaces in their names:

    do shell script "open -a Preview " & quoted form of "/Users/me/Desktop/this has spaces and 'punctuation'.pdf"
0

I am now having a similar problem with Preview. I downloaded some files from Apple. I can use Quicklook in the Finder to see their content but not open with Preview despite being able to set the permissions in Get Info to Everyone Read+Write.

I checked and other files that were OK in the same folder are now experiencing this problem. Looking in Terminal showed what changed, although I don't know why

drwxr-xr-x    5 andydent  501       170 22 Jul 00:21 Android
drwx------  182 andydent  501      6188  2 Sep 23:08 Apple various
drwxr-xr-x@   5 andydent  501       170 23 Dec  2007 AppleScript

The Apple various directory permission seemed a bit odd although as owner I didn't think the missing permissions would make a difference.

Changing the permissions on the directory didn't work.

Copying the file to somewhere else didn't work.

Using sudo open iTunesConnect_Guide.pdf didn't work - I still got an error dialog inside Preview.

However when I quit and relaunched Preview - it was then able to open these files with no problems. It might be that simple - just quit the app.

I have been experiencing weird Finder slowdowns on my system with the spinner going for many seconds on opening a directory. I don't know if the two issues are related. I also wondered if this was somehow related to the relaunch setting re-opening windows.

  • As mentioned by Andy Dent, just quit and restart preview works for me on Mac OS X Mavericks 10.9.5 – zyxue Dec 5 '14 at 16:57
0

This problem is caused by Apple's sandboxing of Preview (and other apps). Preview does not receive an entitlement to access the PDF if you just pass it a string to open. From Apple's release notes:

Compatibility Notes

When sending commands to a sandboxed application, such as TextEdit in OS X Mountain Lion, parameters that refer to files must be of an explicit file-like type and not a bare string, or the target application will not be able to access the file. For example, file "Macintosh HD:Users:me:sample.txt", POSIX file "/Users/me/sample.txt", or the result of choose file would all be acceptable, but the string "/Users/me/sample.txt" would not.

Why does this work for the file in your Downloads folder? When you open a file manually, the application retains the entitlement to open it (in Recents, for instance). Therefore, if you try any file you've opened already, odds are it will work, but it won't work on new files.

To fix, change your code to read:

tell application "Preview"
    activate
    open POSIX file "/Users/me/Desktop/test.pdf"
end tell
0

Restarting preview also solved the problem of not being able to open files even from the finder due to this permissions problem, as Andy Dent suggested.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .