How to get output of shell script "wc" command

The answer to this should be obvious, but I can’t guess what it might be.

This script returns a wordcount from a PDF when run in the terminal:

ps2ascii '/Users/myname/Desktop/eample.pdf' | wc -w

But this AppleScript always returns 0 words (testing under Sequoia):

on open f
	if (count of f) is greater than 1 then
		activate
		display dialog "One PDF only, please." buttons {"OK"}
		error number -128
	end if
	set pdfPath to POSIX path of f as string
	if pdfPath does not end with ".pdf" then
		activate
		display dialog "I only work with PDF files." buttons {"OK"}
		error number -128
	end if
	set wordCount to do shell script "ps2ascii" & space & quoted form of pdfPath & space & "| wc -w"
	display dialog pdfPath & return & return & "contains" & return & return & wordCount & space & "words." buttons {"OK"}
end open

What am I doing wrong??

And to answer my own question in case it helps anyone else:

on open f
	if (count of f) is greater than 1 then
		activate
		display dialog "One PDF only, please." buttons {"OK"}
		error number -128
	end if
	set pdfPath to POSIX path of f as string
	if pdfPath does not end with ".pdf" then
		activate
		display dialog "I only work with PDF files." buttons {"OK"}
		error number -128
	end if
	set wordCount to do shell script "wc -w <<< ps2ascii" & space & quoted form of pdfPath
	display dialog pdfPath & return & return & "contains" & return & return & first word of wordCount & space & "words." buttons {"OK"}
end open

But the results aren’t accurate. So this alternative is what I should have used in the first place:

Generally speaking, environment variable in Terminal.app is different from do shell script’s.

“ps2ascii” command seems not visible in “do shell script” environment.

do shell script “which ps2ascii” will rise error.

The fundamental solution is to write the PS2ASCII command in a full pass such as “/opt/homebrew/bin/ps2ascii”.

I wrote about it in this book.

https://piyomarusoft.booth.pm/items/3892616

As my second post shows, ps2ascii works in a “do shell script” environment, but needed a different syntax.

FWIW, with recent versions of macOS, a shortcut might be considered. The following is easily tailored to meet specific needs, and this could (probably) include performing OCR if the PDF contains an image with text.

PDF Word Count.shortcut (22.4 KB)

@peavine - That is extremely elegant. I haven’t written any Shortcuts, but I see how easy they are. I tend to prefer the flexibility of AppleScript, and have one question:

Does anyone know the command-line equivalent of the step that gets the text from a PDF file? I can’t find any hint online of what the built-in tool might be. Everything seems to require brew or a download.

Thanks for any insight.

emendelson. I don’t know of any command-line tool that will get text from a PDF, but I’m not very knowledgeable in this area. This is easily done with ASObjC, though.

@peavine …and you posted the method here: