do shell script syntax problemS (variables, quotes etc...)

Hello,

I wish to perform a shell script via applescript to transform pdf to pdfa using ghostcript.

The commands are working well via terminal, but via AppleScript I cannot find the right way to “escape” special characters like $ and {…

Here are the terminal commands that are working:

Here is the applescript that is not working:

 
set original_PDF to quoted form of "/var/folders/8_/7tjvnf_51xxc45hptf3cxr680000gn/T/S10/original.pdf"
set original_PS to quoted form of "/var/folders/8_/7tjvnf_51xxc45hptf3cxr680000gn/T/S10/original.ps"
set output_PDFA to quoted form of "/var/folders/8_/7tjvnf_51xxc45hptf3cxr680000gn/T/S10/5-160418-1049_STD.pdf"
set cmd to "type="$(file -b original_PDF)" ; if [ "${type%%,*}" == "PDF document" ]; then usr/local/bin/pdf2ps -r150" & space & original_PDF &  space  & original_PS & " && /usr/local/bin/gs -dPDFA=2 -dBATCH -dNOPAUSE -sColorConversionStrategy=UseDeviceIndependentColor -sDEVICE=pdfwrite -dPDFACompatibilityPolicy=1 -dDFSETTINGS=/ebook -sOutputFile=" & output_PDFA  &  space  & original_PS & ";fi" 
do shell script cmd

The part that is not working is

Can someone help ?

Thanks in advance

I thought that I was able to make it work with ’ quotes but no !

As an alternative, as it is only the part that is detecting if the file is a pdf that is causing problems, I tried to do this part of the job with applescript but I receive “impossible to determine file type”. Any ideas what I am doing wrong ?


set original_PDF to quoted form of "~/Desktop/original.pdf"
set a to POSIX file original_PDF
set original_PS to quoted form of "~/Desktop/original.ps"
set output_PDFA to quoted form of "~/Desktop/output.pdf"
if file type of a = "PDF" then
	set cmd to "usr/local/bin/pdf2ps -r150" & space & original_PDF & space & original_PS & " && /usr/local/bin/gs -dPDFA=2 -dBATCH -dNOPAUSE -sColorConversionStrategy=UseDeviceIndependentColor -sDEVICE=pdfwrite -dPDFACompatibilityPolicy=1 -dDFSETTINGS=/ebook -sOutputFile=" & output_PDFA & space & original_PS
else
	set cmd to "cp " & space & original_PDF & space & output_PDFA
end if
do shell script cmd

To be able to reach the offending instruction I had to edit the beginning of your script as :

set original_PDF to "~/Desktop/a_library.pdf"
set a to POSIX file original_PDF
set original_PDF to quoted form of POSIX path of a

file type of a
--> error "Il est impossible d’obtenir file type of file \":~:Desktop:a_library.pdf\"." number -1728 from file type of file ":~:Desktop:a_library.pdf"

As you see, the passed pathname is wrongly built.

Just to check that my impression was right, I tested:

set a to ((path to desktop as text) & "a_library.pdf") as alias
set original_PDF to quoted form of POSIX path of a

file type of a
--> error "Il est impossible d’obtenir file type of file \"SSD 1000:Users:**********:Desktop:a_library.pdf\"." number -1728 from file type of file "SSD 1000:Users:**********:Desktop:a_library.pdf" 

which is logical because file type doesn’t belong to AppleScript itself.
It is a property defined, Standard Additions, Finder, System Events.
It’s not used by the system for years so very often it returns “”.
It’s what I got with :

set a to ((path to desktop as text) & "a_library.pdf") as alias
set original_PDF to quoted form of POSIX path of a
file type of (info for a) --> ""

So you would have to use this alternate scheme :

set a to ((path to desktop as text) & "a_library.pdf") as alias
set original_PDF to quoted form of POSIX path of a
# case 1, OK but info for is deprecated for years
type identifier of (info for a) --> "com.adobe.pdf"
# case 2, OK 
tell application "System Events"
	type identifier of a --> "com.adobe.pdf"
end tell
# case 3, fails because this dinosaurus named "Finder" doesn't know what is a type identifier
try
	tell application "Finder"
		type identifier of a
	end tell
on error errMsg number errNbr
	log errMsg & "  -  #" & errNbr
	(*Erreur dans Finder : Il est impossible d’obtenir type identifier of document file "a_library.pdf" of folder "Desktop" of folder "**********" of folder "Users" of startup disk.  -  #-1728*)
end try

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 15 décembre 2019 18:28:58

Pourquoi faire simple :rolleyes: …

but thanks very much for your answer and very clear/detailed explanations !

The problem here isn’t the file specifier at all. After all, as the OP sad, everything works as it should in Terminal. So, it should work with do shell script too…

It is not necessary to offer a person instead of his normal command the deprecated info for. Here, it is enough to escape (turn " to ") every nested double quote enclosed in outer double quotes.

Everyone can see the outer double quotes as left and right bounds of blue coloured sentences here:


set original_PDF to quoted form of "/var/folders/8_/7tjvnf_51xxc45hptf3cxr680000gn/T/S10/original.pdf"
set original_PS to quoted form of "/var/folders/8_/7tjvnf_51xxc45hptf3cxr680000gn/T/S10/original.ps"
set output_PDFA to quoted form of "/var/folders/8_/7tjvnf_51xxc45hptf3cxr680000gn/T/S10/5-160418-1049_STD.pdf"

set cmd to "type=\"$(file -b original_PDF)\" ; if [ \"${type%%,*}\" == \"PDF document\" ]; then usr/local/bin/pdf2ps -r150" & space & original_PDF & space & original_PS & " && /usr/local/bin/gs -dPDFA=2 -dBATCH -dNOPAUSE -sColorConversionStrategy=UseDeviceIndependentColor -sDEVICE=pdfwrite -dPDFACompatibilityPolicy=1 -dDFSETTINGS=/ebook -sOutputFile=" & output_PDFA & space & original_PS & ";fi"
do shell script cmd