Remove relentless printer settings in Pages

Jun 08, '10 07:30:00AM

Contributed by: mbogosian

I needed to get rid of this behavior in Pages when it came time to format the document for printing on different printers.

I've submitted bug report upon bug report to Apple and received no satisfaction. So finally, I created a solution myself in the form of a shell script that will remove the printer settings associated with each Pages document that prevents printing to the default printer.

I don't know AppleScript that well, but maybe some entrepreneurial hacker can make a drag-n-drop app out of this. (See this article and this tech note for possible starting places.)

The script is called rm_pages_sticky_printer.sh.

#!/usr/bin/env bash

PROG_NAME="$(basename "${0}")"
PROG_DIR="$(cd "$(dirname "${0}")" && pwd)"
XSLT="$(cat <<EOF
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sl="http://developer.apple.com/namespaces/sl">
    <xsl:template match="sl:NSPrinter" />

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()" />
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
EOF
)"

for i in unzip xsltproc zip ; do
    if ! which >/dev/null "${i}" ; then
        echo 1>&2 "${PROG_NAME}: missing ${i}"

        exit 1
    fi
done

if [ "${#}" -eq 0 ] ; then
    echo "usage: ${PROG_NAME} [PAGES_FILE...]"

    exit
fi

basewd="${PWD}"

for i in ${1+"${@}"} ; do
    echo -n "working on ${i}..."
    idir="$(cd "$(dirname "${i}")" && pwd)"
    ibase="$(basename "${i}")"

    if [ -d "${i}" ] ; then
        inplace=1
    else
        inplace=''
    fi

    if [ -z "${inplace}" ] ; then
        iwdir="$(mktemp -d)"
        unzip -d "${iwdir}" -q "${i}"
    else
        iwdir="${i}"
        gunzip > "${iwdir}/index.xml" -c "${iwdir}/index.xml.gz"
    fi

    echo "${XSLT}" \
        | xsltproc >"${iwdir}/index.xml.tmp" - "${iwdir}/index.xml"

    if [ -z "${inplace}" ] ; then
        iorig="$(TMPDIR= mktemp -p "${iwdir}" index.xml.orig.XXXXXX)"
        cp -Rfp "${iwdir}/index.xml" "${iorig}"
        cat "${iwdir}/index.xml.tmp" >"${iwdir}/index.xml"
    else
        iorig="$(TMPDIR= mktemp -p "${iwdir}" index.xml.gz.orig.XXXXXX)"
        cp -Rfp "${iwdir}/index.xml.gz" "${iorig}"
        gzip >"${iwdir}/index.xml.gz" -c "${iwdir}/index.xml.tmp"
    fi

    rm -f "${iwdir}/index.xml.tmp"

    if [ -z "${inplace}" ] ; then
        inew="${idir}/${ibase%.*}.noprinter.${ibase##*.}"
        cd "${iwdir}"
        zip -9qry "${inew}" .
        cd "${basewd}"
    fi

    echo done
done


[crarko adds: I haven't tested this one. The script is mirrored here.]

Comments (2)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20100604110933292