List pdf names with number of pages and security state: how?

Hi,

I like to have a file with listed which pdf’s are in a given folder and also the number of pages in every pdf and the security state (secured or not).
I’ve tried something and this works for a single file and the written data in Result isn’t clean and I am (still) not able to write it to file:

set my_PDF to POSIX path of (choose file without invisibles)

do shell script "mdls " & quoted form of my_PDF

set _itempath to my_PDF

set Fname to do shell script "mdls -name kMDItemDisplayName " & quoted form of POSIX path of _itempath

set Pages to do shell script "mdls -name kMDItemNumberOfPages " & quoted form of POSIX path of _itempath

set Security to do shell script "mdls -name kMDItemSecurityMethod " & quoted form of POSIX path of _itempath

set _Result to {Fname, Pages, Security}

Can somebody help me out? Thanks in advance!

MacBert

Model: iMac11,3
AppleScript: 2.3 (118)
Browser: Safari 533.19.4
Operating System: Mac OS X (10.6)

Hi,

try this, just choose the folder containing the PDF files,
the result will be written into a file “PDFMetaData for .txt” on desktop


property trimChars : {quote, space}
property keys : {"Name: ", "Pages: ", "Security: "}

set baseFolder to (choose folder)
set folderName to name of (info for baseFolder)
set targetFile to ((path to desktop as text) & "PDFMetaData for " & folderName & ".txt")
set {TID, text item delimiters} to {text item delimiters, tab}
try
	set dataStream to open for access file targetFile with write permission
	set eof of dataStream to 0
	tell application "Finder"
		set pdfFiles to files of baseFolder whose name extension is "pdf"
	end tell
	
	repeat with oneFile in pdfFiles
		set quotedPOSIXfile to quoted form of POSIX path of (oneFile as alias)
		set theData to paragraphs of (do shell script "mdls " & quotedPOSIXfile & " | grep 'kMDItemDisplayName\\|kMDItemNumberOfPages\\|kMDItemSecurityMethod' | awk '/ = /  {for (i = 3; i <= NF; i++) printf \"%s \", $i; printf \"\\n\";}'")
		set theText to {}
		repeat with i from 1 to count theData
			set end of theText to item i of keys & trimLine(item i of theData)
		end repeat
		write ((theText as text) & return) to dataStream
	end repeat
	close access dataStream
on error
	try
		close access file targetFile
	end try
end try
set text item delimiters to TID

-- remove leading and trailing quotes and spaces
on trimLine(aLine)
	repeat until last character of aLine is not in trimChars
		set aLine to text 1 thru -2 of aLine
	end repeat
	repeat until first character of aLine is not in trimChars
		set aLine to text 2 thru -1 of aLine
	end repeat
	return aLine
end trimLine

Hi StefanK,

Thank you very much, this works great! :slight_smile:

MacBert

PS. Looking at your script: I don’t think I’ll ever learn this…

It’s worth it .

Hi SefanK,

I’ve seen some strange things after using your script:

  • After processing 300 files only the first 258 are done. If done in two, like 1-150 and 151-300, no problem.

  • Files with an “_” are in another order than the Finders shows them. It is like this:
    Finder:
    File 1_a.pdf
    File 1.pdf

    File list:
    File 1.pdf
    File 1_a.pdf

Any ideas how to change this behavior? If not, no problem; thanks anyway for your help!