I have a shell script that works in OSX 10.5, but I also need a version that can work in 10.4. Here’s what I have, the purpose of the script is to simple count the number of pages in a PDF.
tell application "Finder"
set theItems to choose file with prompt "Select a PDF file."
set DocPath to (container of file theItems) as string
set RealDocName to name of theItems
set PDFfile to theItems as string
do shell script "/usr/bin/mdls -name kMDItemNumberOfPages " & quoted form of POSIX path of PDFfile & " | /usr/bin/grep -o '[0-9]\\+$'"
set pageCount to result
end tell
I wrote it for 10.5, but now I need it to work on a 10.4 based server we have. When I run it as is on the 10.4 machine I get an error “/usr/bin/mdls command not found” and " | /usr/bin/grep command not found"
I wrote a small foundation tool for this task, you can download it here, it’s simply named countpages. Just unzip it and place it on your desktop for a first test. If you are interested in the source code, you can read it here.
Below is a sample script, which makes use of the foundation tool, it collects all PDF files on the desktop and displays their page count:
on run
-- path to the foundation tool
set ftpath to quoted form of POSIX path of (((path to desktop) as Unicode text) & "countpages")
-- set ftpath to quoted form of POSIX path of (((path to me) as Unicode text) & "Contents:Resources:countpages")
tell application "Finder"
set pdffiles to every file whose name ends with ".pdf"
end tell
repeat with pdffile in pdffiles
set pdffilepath to quoted form of POSIX path of (pdffile as alias)
set cmd to ftpath & space & pdffilepath
set output to do shell script cmd
if output starts with "Error" or output starts with "usage" then
tell me
activate
display dialog "Sorry, an error occured:" & return & return & output buttons {"OK"} default button 1 with icon caution
end tell
else
set pdfname to name of (info for (pdffile as alias))
tell me
activate
display dialog "PDF: " & pdfname & return & "Pages: " & output buttons {"OK"} default button 1 with icon note
end tell
end if
end repeat
end run
First, most of your code doesn’t have to be in the “tell application Finder” block. It’s usually better to keep code outside of tell blocks that doesn’t need to be in one. Why use the resources of the Finder if you don’t have to? As such here’s your code rewritten…
set theItems to choose file with prompt "Select a PDF file."
tell application "Finder"
set DocPath to (container of theItems) as string
set RealDocName to name of theItems
end tell
set pageCount to do shell script "/usr/bin/mdls -name kMDItemNumberOfPages " & quoted form of POSIX path of theItems & " | /usr/bin/grep -o '[0-9]\\+$'"
Second, do you realize that in order to get the page count you don’t need the Finder tell block at all? This would work…
set theItem to choose file with prompt "Select a PDF file."
set pageCount to do shell script "/usr/bin/mdls -name kMDItemNumberOfPages " & quoted form of POSIX path of theItem & " | /usr/bin/grep -o '[0-9]\\+$'"