How to get rid of PDF permissions issue

Whenever I try to open outpath.pdf file that I create like this:

-- joining PDFs
do shell script quoted form of ("/System/Library/Automator/Combine PDF Pages.action/Contents/MacOS/join") & space & "-o" & space & quoted form of outpath & space & quoted form of frontpdfpath & space & quoted form of temppdfpath & space & quoted form of backpdfpath

tell application "Preview"
    activate
    open outpath
end tell

I always get this error message:
The file “outpath.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.

Since this is a PDF the I create on the fly, it’s not something that I can do permanently. I already changed the permissions of the folder containing the file and didn’t help. Is there something else I can do?

Basically the issue doesn’t occur if I manually open the file at least once, close it and then run the script again. Any ideas on why this is happening? and how to fix it?

Well, what are the permissions of the file? And does it have any extended attributes set?

These are the permissions. I’m logged in as administrator. So I don’t see why the file wouldn’t get opened with an Apple Script but would manually.

Screenshot 2023-04-12 at 14.17.46

I’m not sure, how can I give full access?

Here is one article about this error: How to fix the "jpg file couldn't be opened because you don't have permission to view it" error | Stellar.

It is about jpg files, but they are image files like pdfs.

Most likely, you have to delete following files in your account’s Library folder:

  • Containers/com.apple.Preview
  • Saved Application State/com.apple.Preview.saved state
  • Preferences/com.apple.Preview.LSSharedFileList.plist
  • Preferences/com.apple.Preview.SandboxedPersistentURLs.LSSharedFileList.plist
  • Caches/com.apple.Preview

and to run First Aid with Disk Utility.

A few comments:

  • The open command is broken in a number of macOS apps, and it’s possible that is one issue. The last paragraph in the OP’s original post would tend to indicate that this is the case.

  • The reported error message references permissions, but the issue may have nothing to do with permissions in the sense we use the term.

  • In the OP’s script, it appears that the outpath variable has a POSIX path as required by the do shell script line, but this variable apparently is not redefined to use a path that works with Preview.

To illustrate some of the above, the following script works as expected on my Ventura computer:

set outpath to "Macintosh HD:Users:Robert:Desktop:Test.pdf" as alias
tell application "Preview"
	activate
	open outpath
end tell

The following script throws a permissions error message even though the issue has to do with the path used:

set outpath to "/Users/Robert/Desktop/Test.pdf"
tell application "Preview"
	activate
	open outpath
end tell --> The file “Test.pdf” couldn’t be opened because you don’t have permission to view it.

Thanks but that didn’t work either. I found the solution though.

Adding this before opening the file:

set sd to path to startup disk
tell application "Preview"
    try
         close sd
    end try
end tell

I have no idea why this works, but it works.

This trick is familiar on MacScripter. In fact, it was once invented to bypass access restrictions by non-Apple applications. For example, this trick has to be used sometimes with MicroSoft applications.

But Preview.app is Apple’s pre-installed app, so if its permissions are violated in some way, that’s bad, and I’d rather fix the problem drastically. That is, I would prefer to restore the violated Preview.app permissions. How to do this is shown in my previous post.

Also, you are concatenating 2 pdf files using a utility that most likely does not return the result to the script. This will lead to desynchronization, since you are immediately trying to open a file that is not yet ready.

Also, it is not clear from your script what variable outpath is.

Thanks for your comments. And you’re totally right about the desynchronization. I just handle it with a delay of 4 seconds. Is there a better way?

Problem is that I’d like to do this automatically, with no user intervention (no need to choose file). Is there a way to know when the file completed its creation to be able to open it?

It’s always helpful to have a full script to work with, even if it has issues. Both code sections 1 and 2 below worked as expected (without a delay) on my Ventura computer.

set theFolder to POSIX path of (path to desktop)
set frontpdfpath to theFolder & "Test One.pdf"
set backpdfpath to theFolder & "Test Two.pdf"
set outpath to theFolder & "Merged PDF.pdf"

do shell script quoted form of ("/System/Library/Automator/Combine PDF Pages.action/Contents/MacOS/join") & space & "-o" & space & quoted form of outpath & space & quoted form of frontpdfpath & space & quoted form of backpdfpath

-- code section 1
set thePDF to POSIX file outpath
tell application "Preview"
	activate
	open thePDF
end tell

-- code section 2
-- do shell script "open " & quoted form of outpath
1 Like

The following code section will delay the script until the merged PDF file is found.

set theFile to "/Users/Robert/Desktop/Merged PDF.pdf"
set theFile to POSIX file theFile

repeat with i from 1 to 100
	try
		set fileExists to theFile as alias
		exit repeat
	end try
	delay 0.1
end repeat
if i = 100 then display alert "The merged PDF was not be found"
1 Like