It got me thinking… A (probably) less ‘clunky’ solution could be:-
set myDoc to (choose file with prompt "Choose a pdf")
set pathToDoc to POSIX path of myDoc
set allFonts to do shell script "grep -a 'FontName' ../../" & pathToDoc
giving the following:-
But even this is hardly a list of just the Fonts used in a document.
Does anyone know of a quick way to strip this down?
set myDoc to (choose file with prompt "Choose a pdf")
set pathToDoc to POSIX path of myDoc
set allFonts to do shell script "tr '\\r' '\\n' < ../../" & pathToDoc & " | grep -a 'FontName'"
set myDoc to (choose file with prompt "Choose a pdf")
set pathToDoc to POSIX path of myDoc
set allFonts to do shell script "tr '\\r' '\\n' < ../../" & pathToDoc & " | grep -a 'FontName' | cut -d '/' -f 4 | cut -d '+' -f 2"
A big thanks to divster for your suggestion. I now don’t even have to open Acrobat!!
Nicely done.
Just a few more kinks: Files with spaces in their filenames return nothing. And exotic characters in the filename makes the script fail. Any ideas?
I too found that paths with spaces in them fail, and also that the font name is not always in the same place within the resuls.
The following addresses this
set myDoc to (choose file with prompt "Choose a pdf")
set pathToDoc to POSIX path of myDoc
set allFonts to do shell script "tr '\\r' '\\n' < ../../" & quoted form of pathToDoc & " | grep -a 'FontName' | cut -d '+' -f 2 | cut -d '/' -f 1"
Now it’s working on most pdfs I’ve tried, but it still returns empty on some of them. I’m not sure it’s about the characters in the file name, maybe they’re just structured differently. I could imagine that pdf standards have changed quite a few times.
I have found that some fonts (but not the majority) do not have the 6 characters and ‘+’ in the string returned. As I am using the ‘+’ as a delimiter in the Unix ‘cut’ command, this is giving some odd results.
Fonts in one document are:-
Which when cut using the ‘+’ delimiter gives:-
Problem is, as the FontName is not always in the same position, I cannot use the ‘/’ as a delimiter.
would repeating through each line (paragraphs of allFonts) and using the offset of “FontName/” +1 to “/” work??
Glad you’re having some success. I don’t use grep in a shell script but do use it in InDesign for styles. Hopefully the syntax is similar enough to be useful. If I use the following:
This is positive look behind for /FontName/ followed by any character repeated one or more times shortest match followed by positive look ahead for /
I can highlight the font names in your example as follows:
Then it would be easy to chop off the other rubbish with:
This is any upper case character (5 times) followed by + (escaped with a )
I’m not familiar with that ‘kind’ of grep in Unix, but again based on your suggestion and working with the Unix grep and regex I now have the following that returns everything after the FileName/ and then repeats through every line and if it contains the “+” character, it ‘cuts’ it out.
set myDoc to (choose file with prompt "Choose a pdf" without invisibles)
set pathToDoc to POSIX path of myDoc
set allFonts to paragraphs of (do shell script "tr '\\r' '\\n' < ../../" & quoted form of pathToDoc & " | egrep -ao \"FontName.*/\" | cut -d '/' -f 2") -- | cut -d '/' -f 1")
set fontList to {}
repeat with i from 1 to number of items in allFonts
set thisItem to item i of allFonts
if thisItem contains "+" then
set thisItem to do shell script "echo " & thisItem & " | cut -d'+' -f2 "
set fontList to fontList & thisItem
else
set fontList to fontList & thisItem
end if
end repeat
fontList
I’ve not tested loads of PDFs, but so far so good. Thanks for everyones input.
is a good site for Mac specific Unix commands. For everything else google search “Mac OS X Unix commands” (Some Linux/Solaris commands are not available in Mac OS X!)
I also refer to the book “Mac OS X UNIX Toolbox - 1000+ Commands for Mac OS X Unix”