The number of Pages in a pdf as custom metadata

I have been trying to the number of pages in a pdf to be available as a custom metadata.
I am not expert in AppleScript. The following is not working for me. Can sb help me with it please?

tell application id "DNtp" 
	set theRecords to get the selection 
	repeat with eachRecord in theRecords 
		set thePageCount to page count of theRecords
		add custom meta data thePageCount for "Pages" to theRecords
	end repeat
end tell

You have to use eachRecord (exactly one record), not theRecords (a list of records) inside the loop.

1 Like

Yes, that worked. Thank you.

On a side note: I applaud you exploring AppleScript. Learning to automate things is a wonderful, though sometimes frustrating, pursuit. But in the end, you can lean back and enjoy the fruits of your labors :slight_smile:

That being said, this is also an instance where someone who doesn’t want to tackle scripting could effectively use a batch process or smart rule using the Page Count placeholder…

The only requirement here is needing to run the Pro or Server edition of DEVONthink to use the custom metadata. For Standard users, there is the possibility of using the Finder Comments instead.

5 Likes

Sidebar: is there a glossary of Devonthink-specific terms/variables/instructions for AppleScript? I would not have known this.

1 Like

Just drop DEVONthink 3.app onto the Script Editor’s Dock/Finder icon to view its complete AppleScript suite.

Thank you! Can I suggest putting that action in the documentation somewhere?

A different approach is already part of the documentation, see page 200 of the PDF:

2 Likes

Welcome @CynthesisToday

You’d need to add your own custom metadata or else just use that Page attribute. That’s up to you.

It’s getting the information from the PDF itself.

can you please help me a little with this? I have created a custom data field but when I do want to create the smart rule How do I enter the placeholder page count? If I command click on the empty filed there is no option to enter a placeholder

Did you try this?


thanks @chrillek but like I said when using the custom metadata field it is impossible to select any Placeholder at all

Interestingly, that seems to depend on the type of custom metadata. I can choose placeholders for “ISBN” and “Reference” which are both of type “single line text”. OTOH, I don’t get to choose placeholder for “amount”, which is floating point number (all of these are predefined by DT, I think).
So, perhaps changing the field type to text helps? Or perhaps it’s not a workaround since you want to treat the custom metadata as number later…

I’m not sure if this limitation of using placeholders only with certain metadata is intentional or a glitch. @cgrunenberg should know more about that.

Only text based custom metadata supports placeholders.

1 Like

thanks that was it now it Is working

If you need the page number in your custom meta data to be a real number (for example, if you want to sort records by number of pages or so), you could use a script in your smart rule. Something like

function performsmartrule(records) {
  const app = Application("DEVONthink 3");
  records.forEach(r => 
    app.addCustomMetaData(r.pageCount(), {for: 'pagenumber', to: r})
)}

Usage: Select “Execute script”, “JavaScript” as action in your smart rule and replace text in the “Edit script” popup with this code. This assumes that your custom meta data is named ‘pagenumber’. If not, change the word after the for in the r.addCustomMetadata() call.

cool thanks will give it a try

Use it with a duplicate of a document first. I’m on holidays and didn’t test the code at all!

Sorting is possible even if’s a string, e.g. the names “1”, “2” and “10” are also sorted like numbers and not just like strings (in that case the order would be “1”, “10”, “2”)