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
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
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.)
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.
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
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”.
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).