Mac Icons for PDFs

I have a great many folders of PDFs, mostly grabbed from archive.org magazine_rack, ataribooks, etc. The trouble is when I open a folder of these, Finder makes preview icons for a few of them, then gives up and they all show a generic "PDF" icon. What I want is a persistent icon for the first page!

First, you need osxutils:

% sudo port install osxutils
% man seticon

And my icontool.

sips (Scriptable Image Processing System) is a built-in tool on the Mac, incredibly powerful image converter. I'm not gonna do anything fancy, just use it to get an image.

Now create pdficonset.zsh:

#!/bin/zsh
export CG_PDF_VERBOSE=1

iconify () {
    echo $1
    sips -s format png -z 1024 768 -p 1024 1024 $1 -o thumb.png && \
    icontool.zsh thumb.png thumb.icns && \
    seticon -d thumb.icns $1
    rm -f thumb.png thumb.icns
}

if [[ $# -eq 0 ]]; then
    ls -1 *.pdf |while read -r f; do
        iconify $f
    done
elif [[ "$1" == "-r" ]]; then
    find . -type f -d -iname "*.pdf" |while read -r f; do
        iconify $f
    done
else
    while [[ $# -gt 0 ]]; do
        iconify $1
        shift $1
    done
fi

Now run it:

% pdficonset.zsh
# iconifies pdfs in current dir
% pdficonset.zsh -r
# iconifies pdfs in all subdirs
% pdficonset.zsh foo.pdf
# iconifies named pdfs

Boom! All nice icons.

[update: added a little better error-safety. CG_PDF_VERBOSE just gives better but still not useful error messages.]

[update 2023-12-21: finally made it generate proper aspect ratio icons, and multiple file commands.]

I don't have the problem as bad with CBZ/CBR comics; they'd be trivial to extract the first page from, since they're just ZIP/BZIP files.

folder full of pdfs with nice icons instead of placeholders