Applescript run as a Quick Action errors out when run inside a Shortcut.

I have a Quick Action which flattens a PDF via Applescript. The code works extremely well--I right click in finder and the PDF is flattened, annotations are burned in, no other applications are opened, and the action completes in less than 2 seconds.

Here is the Applescript code:

I have a Shortcut which completes several operations on already-flattened PDFs. Presently 1) I run my Quick Action by right-clicking a PDF in Finder in order to flatten it, and 2) then right-click that save PDF in Finder to run my Shortcut on that now-flattened PDF.

Ideally I'd like to add the Applescript code which flattens the PDF to the beginning of my Shortcut for the sake of convenience, and because sometimes I run my Shortcut having forgotten to flatten the PDF first.

However I'm finding that the Applescript code, when placed into a Run Applescript action in Shortcuts, gives this error message:

The exact same code when placed into a Run Applescript action in Automator and then run as a Quick Action by right-clicking the PDF in Finder, does not give an error and works perfectly.

Does anybody have an explanation (and possible solution) for why this is the case?

Answered by DTS Engineer in 872644022

That's an interesting technique for running Swift code from the 'do shell script' AppleScript command. This would probably run faster if you simply compiled the swift to a command line tool, but this is certainly easier to maintain. It's hard to tell what the problem is given a picture of your code, but I tried typing it in. I was unable to reproduce the problem. Maybe the alternate file name handling fixed the problem. Here's the result ~

set chosenFile to choose file with prompt "Please select a PDF file:" of type {"com.adobe.pdf"} without multiple selections allowed
set inputfile to POSIX path of chosenFile
set tempFile to inputfile & ".tmp.pdf"

do shell script "xcrun swift - <<'SWIFTCODE'

import Foundation
import PDFKit

/* get the file names, removing the quotes added by 'quoted form...' */
let inputPath = \"" & text 2 thru -2 of quoted form of inputfile & "\"
let outputPath = \"" & text 2 thru -2 of quoted form of tempFile & "\"

/* print out the file names */
print(\"in='\\(inputPath)' out='\\(outputPath)'\")

/* open the PDF document */
let inputURL = URL(fileURLWithPath: inputPath)
guard let pdfDoc = PDFDocument(url: inputURL) else {
	print(\"Error: could not read PDF '\\(inputPath)'\")
	exit(1)
}

/* save the flattened PDF */
let outputURL = URL(fileURLWithPath: outputPath)
let options: [PDFDocumentWriteOption: Any] = [.burnInAnnotationsOption: true]
let success = pdfDoc.write(to: outputURL, withOptions: options)

/* report errors */
if !success {
	print(\"Error: could not write PDF '\\(outputPath)'\")
	exit(1)
}

/* report success */
print(\"Success\")
exit(0)

SWIFTCODE"

Does anybody have an explanation (and possible solution) for why this is the case?

I'm not sure of the exact change that happened but, basically, shortcuts isn't providing EXACTlY the right format that sh expects. That's what these error are actual saying:

sh: -c: line 0: syntax error near unexpected token '('
sh: -c: line O: 'xcrun swift - << 'SWIFTCODE'
'/Users/

Thats is, "sh" is the shell that's actually executing the script and it's saying that it didn't like what you gave it. In terms of sorting this out, I would actually start by going back to a much simpler script that used "echo" to print out the values of inputFile and tempFile, so you can see exactly what you're dealing with. Alternatively, you might shift to Shortcuts "run shell script" action, which would remove the additional layer of indirection that AppleScript is adding.

__
Kevin Elliott
DTS Engineer, CoreOS/Hardware

That's an interesting technique for running Swift code from the 'do shell script' AppleScript command. This would probably run faster if you simply compiled the swift to a command line tool, but this is certainly easier to maintain. It's hard to tell what the problem is given a picture of your code, but I tried typing it in. I was unable to reproduce the problem. Maybe the alternate file name handling fixed the problem. Here's the result ~

set chosenFile to choose file with prompt "Please select a PDF file:" of type {"com.adobe.pdf"} without multiple selections allowed
set inputfile to POSIX path of chosenFile
set tempFile to inputfile & ".tmp.pdf"

do shell script "xcrun swift - <<'SWIFTCODE'

import Foundation
import PDFKit

/* get the file names, removing the quotes added by 'quoted form...' */
let inputPath = \"" & text 2 thru -2 of quoted form of inputfile & "\"
let outputPath = \"" & text 2 thru -2 of quoted form of tempFile & "\"

/* print out the file names */
print(\"in='\\(inputPath)' out='\\(outputPath)'\")

/* open the PDF document */
let inputURL = URL(fileURLWithPath: inputPath)
guard let pdfDoc = PDFDocument(url: inputURL) else {
	print(\"Error: could not read PDF '\\(inputPath)'\")
	exit(1)
}

/* save the flattened PDF */
let outputURL = URL(fileURLWithPath: outputPath)
let options: [PDFDocumentWriteOption: Any] = [.burnInAnnotationsOption: true]
let success = pdfDoc.write(to: outputURL, withOptions: options)

/* report errors */
if !success {
	print(\"Error: could not write PDF '\\(outputPath)'\")
	exit(1)
}

/* report success */
print(\"Success\")
exit(0)

SWIFTCODE"
Applescript run as a Quick Action errors out when run inside a Shortcut.
 
 
Q