AppleScript – Pages export to PDF fails due to Sandbox permissions I'm trying to create an Automator workflow to export selected Pages documents to PDF. So far, everything goes well, except for the final export step.

on run {input, parameters}
    repeat with _document in input

        tell application "Finder"
            set _directory to get container of file _document

            set _documentName to name of _document
            if _documentName ends with ".pages" then ¬
                set _documentName to text 1 thru -7 of _documentName

            set _PDFName to _documentName &amp; ".pdf"
            set _incrementIndex to 1
            repeat until not (exists file _PDFName of _directory)
                set _PDFName to ¬
                    _documentName &amp; "-" &amp; (_incrementIndex as string) &amp; ".pdf"
                set _incrementIndex to _incrementIndex + 1
            end repeat

            set _location to (_directory as string) &amp; _PDFName
        end tell

        --set _location to (POSIX file of _location)

        tell application "Pages"

            activate
            open _document

            with timeout of 1200 seconds
                export front document to file _location as PDF
            end timeout

            close front document

        end tell

    end repeat
    return input
end run


This gives me a Sandbox permissions error. 

error   17:49:38.117966 +0100   sandboxd    SandboxViolation: Pages(3846) deny file-write-create /Users/brunoscheele/Desktop/Test.pdf
Violation:       deny file-write-create 


After reading up on it here, it looked like I could fix this by changing _location to a POSIX file.

So I added;

--set _location to (POSIX file _location)


However, that then gives me the error: 

Pages got an error: "macOS:Users:brunoscheele:Desktop:Test.pdf" could not be interpreted as a file URL.

Using export front document to (POSIX file _location) as PDF gives me the same error.

Does anyone know how to set the _location properly, so you don't run into permission issues?
You are very close. Edit your export line to look like this:

export front document to file _location as PDF


It's the word "file" that you were missing.

Note: I tried the script again and it did not work, even with the word "file" added. However, while experimenting I found that if I changed this line:

set _location to (_directory as string) &amp; _PDFName


to 

set _location to (_directory as string) 


then the script would of course error. But then, if I undid the change, so the line again looked like this:

set _location to (_directory as string) &amp; _PDFName


the script began to work! I am using Pages 6.0.5, macOS 10.12.2.

I can't say what is causing this behavior.