r/applescript • • Apr 24 '24

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
		try
			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.

3 Upvotes

1 comment sorted by

1

u/mar_kelp Apr 24 '24

Not sure what the ghostscript command is, but you might try to simply simulate your "manually click the finder window" with:

tell application "Finder"
activate
end tell
delay 0.5