Get number of page of pdf

Hi everybody
Is it possible to get the number of page of a pdf ( using OXS preview feature )
At this time i’m using applescript to grab the info and return it to xojo
But if osx can make preview of it… just by pressing space bar
Maybe i can get the info there instead on opening the pdf by applescript

Thank

I use this in a Listbox (KeyDown)

if asc(key)=32 then dim myfile as FolderItem = GetFolderItem _ (me.cell(me.ListIndex,0),FolderItem.PathtypeNative) if right(myfile.name,4) = ".pdf" then dim mshell as new shell dim s as string = "qlmanage -p " + myfile.ShellPath + " >& /dev/null &" mshell.Execute s return true end if end

Thanks Axel
But i dont want to make preview… ( i just take the example of the preview feature… get pages count )
I need to get count of pages of each to 5000 pdf
then display result in listbox

It might be possible with a MDItem or NSMetaData, which are basically the classes that developers can use to read the spotlight data for a file.

Check the MBS Plugins or MacOSLib for either of these two classes.

Getting the number of pages is possible with MBS plugins without DynaPDF. I’m not if front of my computer right now, but I can look for the code if you want.

If you do decide to utilize the spotlight data, the key is “kMDItemNumberOfPages”

I think, i will try by spotlight data… thanks Sam i’v find the ref at the apple dev page
https://developer.apple.com/library/mac/documentation/Carbon/Reference/MDItemRef/index.html#//apple_ref/doc/constant_group/Common_Metadata_Attribute_Keys
I’m not sure how to use it… but i’v find a exemple where it use the kMDItem on the forum
https://forum.xojo.com/2434-splitting-mdls-shell-results
Is it a good base to start ?
Thanks

[quote=206804:@Denis Despres]I’m not sure how to use it… but i’v find a exemple where it use the kMDItem on the forum
https://forum.xojo.com/2434-splitting-mdls-shell-results
Is it a good base to start ?[/quote]
I’ve not tried it that way, but give it a go and let us know if it works.

It’s not that difficult using declares to get the MDItem from a file, the big complication comes in getting the resulting information into Xojo easily. Converting NSDictionaries and the values within to Xojo values.

Work like a charm

sh.Execute "mdls -name kMDItemNumberOfPages -raw " + f.Item(i).ShellPath

inside my loop trought the folder
Scanning 4000 pdf’s and get the page count of each…
Display the name + path + page count in a listbox take 1 minutes
Not bad for me… what do you think of performance
do you think it would be faster if i send it first in array… then put it in my listbox ??

With only 4000 items it’s unlikely that the listbox has a major effect on the speed. But try it out.

The slowest is not the listbox, it is the batch command.

Not sure to understand Michel
I don’t have a choice… after drag n drop the folder… i filter the pdf only
then loop trough
Is there other way ?

When doing profiling you try to optimize the part of the code that is the slowest. Both Michel and myself have worked with the listbox and we know that filling a listbox is pretty fast. So if you want to optimize our guess is that the listbox isn’t the bottleneck. Making the shell faster most likely won’t be possible. But fire up Instruments and profile away before we guess too much.

[quote=206873:@Denis Despres]Not sure to understand Michel
I don’t have a choice… after drag n drop the folder… i filter the pdf only
then loop trough
Is there other way ?[/quote]

You are alright. My comment was simply that you should not worry about the listbox versus an array, as anyway the source of your information is a batch that is much slower.

Thank you all
Now i know i’m doing right… with all your advice

There could be one way to drastically speed things up : instead of using one shell, use a series of asynchronous ones which each takes a portion of the list.

For instance, if you use 10 asynchronous shells, you can divide your 4000 files in chunks of 400, all running at the same time.

It is much more complex, though.

mdls?

Why not go with PDFKit or CoreGraphics on Mac?
We have a plugin for those or you try some declares.

Just opening the PDF and querying page count is not that difficult.

dim f as FolderItem = SpecialFolder.Desktop.Child("test.pdf") dim p as new PDFDocumentMBS(f) MsgBox str(p.pageCount)

like this.

Very good idea Michel
I just dont know how send 10 functions simultaneous
last time i’v tried ( another project… another circonstance )
they just wait for previous to end

Christian… im not sure to follow you on this
dont want to open the pdf
Just get the page count the fastest way possible
in a folder up to 5000 pdf’s
and display the result in listbox