Trying to run Applescript in automator (Quick Action) to use in Finder for converting PDF files to PDF/A

Hi, I am trying to create a quick action that would allow me to convert PDF files to PDF/A. Im using the ghostscript command to do this. I was successful with the script and in script editor, it works flawlessly, however, when I try to apply the script using Automator and invoke it using Quick Action in the Finder, the gs command does not fire up until I manually click the finder window (until then, the script is idle). This is the script:

on run {input, parameters}
	repeat with theFile in input
		tell application "Finder"
			set theFilesFolder to (folder of theFile) as text
			set filePath to POSIX path of theFile
			set theName to name of theFile
		end tell
		-- Extract the base name without extension
		set AppleScript's text item delimiters to "."
		set theNameWithoutExtension to text items 1 thru -2 of theName as text
		set AppleScript's text item delimiters to "" -- Reset to default
		-- Define the PDF/A output path
		set pdfaPath to theFilesFolder & theNameWithoutExtension & "_PDFA.pdf"
		set quotedPdfPath to quoted form of filePath
		set quotedPdfaPath to quoted form of POSIX path of pdfaPath
		set gsPath to "/opt/homebrew/bin/gs"
		-- Convert the PDF to PDF/A-2b using ghostscript
		set gsCommand to gsPath & " -dPDFA -dBATCH -dNOPAUSE -sProcessColorModel=DeviceRGB -sDEVICE=pdfwrite -dPDFACompatibilityPolicy=2 -sOutputFile=" & quotedPdfaPath & " " & quotedPdfPath
			do shell script gsCommand
		on error errMsg
			display alert "Ghostscript Error" message errMsg
		end try
		tell application "Finder"
			update theFilesFolder -- Refresh the folder view
		end tell
		-- Optionally, you can remove the original PDF file here if needed
		-- do shell script "rm " & quotedPdfPath
	end repeat
	return input
end run

Can someone suggest what to do here? As you can see, I tried implementing a refresh command within the script, but to no avail. I’m desperate. Of course, Automator has all the permissions.

Blind thoughts:

  • Try activating the Finder at various steps in the script.
  • It may help us if you can find out where exactly it hangs. That would require you doing some kind of logging, e.g. with the macOS NSLog function (no idea how to issue that from Applescript, though) or writing bits to a file at various places in the code. Or just place a “display dialog” right before the “do shell” to see if that appears without you doing the extra click - if it doesn’t appear, move the line further until it appears, and then you know which line causes the hold-up. Share the knowledge here.
  • Do you have other macOS versions where you can double check the behavior? Maybe it’s a bug in macOS - that might also help us to figure out a work-around.
  • When it’s stuck - is the Finder frontmost then, and is the window where you ctrl-clicked the service, still active (check the window title bar and its buttons)? And do you see a slow-spinning gear icon in the menu bar indicating that a script is running?

For me, your script works fine.

I would look at the automator part though. As I use Sierra, I’m not familiar with ‘quick actions’ but if I create a ‘service’, it’s a pain. For some reason, automator kept making it a text service — even when set to accept ‘files or folders’— and of course, it wouldn’t then take a file. I had to monkey around to get the service to show up under ‘Files and Folders’ in the Services system preferences. Once I did that, the service worked fine and the pdfs were converted.

You might have a different issue that’s more appropriate to your OS version but I’d try and make sure that the automator part of the process was working as needed.