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?
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.
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.
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
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"