How to count number of pages in PDF file using Apple Script?

I just need to create a code that counts the number of pages in a PDF to determine if they’re odd or even. Do you have any idea of how to do this? I’ve seen previous posts but they seem to be very old, so any other “novel” solution is appreciated.

Unfortunately, I don’t know how to do this with basic AppleScript except by calling a command-line utility. The following works but requires two frameworks, which is best avoided in a simple AppleScript. So, my suggestion is FWIW.

use framework "Foundation"
use framework "Quartz"
use scripting additions

set thePDF to (choose file)
set pageCount to getPageCount(thePDF)

on getPageCount(theFile)
	set theFile to current application's |NSURL|'s fileURLWithPath:(POSIX path of theFile)
	set theDocument to current application's PDFDocument's alloc()'s initWithURL:theFile
	return theDocument's pageCount() as integer
end getPageCount


set thePDF to (choose file of type "pdf")
set pageCount to getPageCount(thePDF)

on getPageCount(theFile)
	set filepath to POSIX path of theFile
	set pageCount to (do shell script "mdls -name kMDItemNumberOfPages -raw " & quoted form of filepath)
	return pageCount
end getPageCount
if pageCount mod 2 = 1 then
	return "Odd"
	return "Even"
end if