Need help with KMDitemNumberOfPages

Im a bit confiused. This script works fine (returns the page counts of PDFs) when I do it from local files on my mac. But if I choose files on a SMB volume it always returns “null”

Any idea’s

set theFiles to choose file with multiple selections allowed
repeat with aitem in theFiles
	my get_pagecount(aitem)
end repeat

on get_pagecount(afile)
	set myResult to do shell script "mdls -name kMDItemNumberOfPages '" & (afile & "'")
	return myResult
end get_pagecount

In your script, aitem/afile is an alias.

The shell instruction requires a POSIX path. I choose to ask the system itself to quote the path so I edited the script this way :

set theFiles to choose file with multiple selections allowed
repeat with aitem in theFiles
	my get_pagecount(aitem)
end repeat

on get_pagecount(afile)
	set afile to quoted form of POSIX path of afile
	set myResult to do shell script "mdls -name kMDItemNumberOfPages " & afile
	return myResult
end get_pagecount

Yvan KOENIG (VALLAURIS, France) dimanche 3 novembre 2013 09:25:34

Still getting the same results

Behaves flawlessly here.

tell application “AppleScript Editor”
choose file with multiple selections allowed
→ {alias “Macintosh HD:Users:yvankoenig:Desktop:« Le harcèlement scolaire m’a détruit à petit feu » | Rue89.pdf”, alias “Macintosh HD:Users:yvankoenig:Desktop:977174_a1.pdf”}
end tell
tell current application
do shell script “mdls -name kMDItemNumberOfPages ‘/Users/yvankoenig/Desktop/« Le harcèlement scolaire m’a détruit à petit feu » | Rue89.pdf’”
→ “kMDItemNumberOfPages = 3”
do shell script “mdls -name kMDItemNumberOfPages ‘/Users/yvankoenig/Desktop/977174_a1.pdf’”
→ “kMDItemNumberOfPages = 6”
end tell
Résultat :
“kMDItemNumberOfPages = 6”

Are you sure that the submitted files are PDF ones ?

Yvan KOENIG (VALLAURIS, France) lundi 4 novembre 2013 09:57:19

It does work fine for files on my mac. Just not files that are on a mounted volume. Any other idea’s?

Even if I run the command via terminal I get the following

PKincaid:~ pkincaid$ mdls -name kMDItemNumberOfPages /Volumes/ExampleJobContainer/5000000/12345_3_Q500.pdf
kMDItemNumberOfPages = (null)

Hello.

One possible explanation could be that the volume in speak weren’t indexed by Spotlight. Please have a look at the preferences Pane for Spotlight.

Another explanation would be that the spotlight database for the volume is somehow corrupted.

So, first attemtp would be to remove the volume from “exceptions” in the spotlight pane. Then add it to exceptions, then remove it again. (It has to index before you remove it for the last time of course. ” Feel free to google for how to instigate Spotlight indexing of a volume.)

Good Luck. :slight_smile:

Does “mdls -name kMDItemNumberOfPages '” need the volume indexed to work? Reason I ask is this is a corporate server (PC) that is mounted SMB. Not sure that indexing it from a mac would work well.

Yes – all mdls does is search the volume index.

Shane, do you know of another way to return count of pages of a PDF? Without opening it in any applications.

It’s easy enough if you are running Mavericks. Save this code in an ASObjC library (yell if you don’t know how):

use framework "Quartz"
use framework "Foundation"

on countPagesInPDFAt:posixPath
	set aURL to current application's |NSURL|'s fileURLWithPath:posixPath
	set theDoc to current application's PDFDocument's alloc()'s initWithURL:aURL
	return theDoc's pageCount() as integer
end countPagesInPDFAt:

Then just call it from your script like this:

use theLib : script "<name of library>" 
use scripting additions
set thePath to POSIX path of (choose file)
set pageCount to theLib's countPagesInPDFAt:thePath

Yelling…

Save this code in an ASObjC library (yell if you don’t know how):

Thanks Shane

  1. Open a new script window in AppleScript Editor, add the first block of code, and save it as script bundle to the folder ~/Library/Script Libraries/ (which you will have to create), calling it something like “PDF Lib.scptd”.

  2. That will make Bundle Contents button in the ASE’s toolbar active, so click on it; a drawer will open. In the drawer, click on the checkbox AppleScript/Objective-C Library, and save again.

Now any script can call it with the second bit of code (after you enter the correct name for the lib).