MACSCRPT Archives

March 2010

MACSCRPT@LISTSERV.DARTMOUTH.EDU

Options: Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
Laine Lee <[log in to unmask]>
Reply To:
Macintosh Scripting Systems <[log in to unmask]>
Date:
Thu, 4 Mar 2010 23:53:10 -0600
Content-Type:
text/plain
Parts/Attachments:
text/plain (75 lines)
I knew if I posted something that, even though it worked,  I'd get a
response or two on how to improve it. Even saw quit a few names I recognized
from the past.

I ended up converting to cups-pdf, which I think is basically a virtual
printer that implements gostscript and ps2pdf.

Had a little trouble getting it to work on snow leopard and then I had the
classic problem of making sure a file exists and was completely written.

My Safari stuff was reduced to:

    tell application "Safari"
        open location "file://" & htmlPath
        set name of document of window 1 to filename # what cups-pdf uses to
name file
        print window 1 with properties {target printer:"PDF-Writer"}
        close window 1
    end tell

I still found it best to use System Events to make sure the print file was
done. The basic pseudo code is something like:

    tell application "System Events"
        repeat with i from 1 to 15
            set printers to (name of every application process where name is
"PrinterProxy")
            if the number of printer proxies is equal to the number at start
                it's done writing the file, exit repeat
            end if
            delay 1
        end repeat
    end tell

Takes about 3 seconds to write out a 1meg PDF.

Thanks for the suggestion.

Steve Alex

On Mar 2, 2010, at 9:00 PM, John Delacour

>     print window 1 with properties {target printer:"my virtual printer"}
> 
> The print files are spooled to /var/spool/cups/, which requires root access.
> 
> Then convert the resulting PostScript file to pdf using ps2pdf
> 
>  (do shell script "man ps2pdf")
> 
> for which you need ghostscript, which is not a troublesome installation.
> 
> Otherwise Preview will automatically convert ps to pdf on opening.


I think the CUPS-PDF method will nullify live links, while neither Smile nor
the .ps method will. But if you have Snow Leopard, you should be able to let
Preview do all the work, without any need for converting .ps to .pdf (not
that I like the idea of needing admin access to make a pdf). I just created
a virtual printer using the method already provided, except that I didnšt
customize its name, then used the following script. The first line is just
to provide a quick example with plenty of links, of course.

tell application "Safari" to open location "localhost:631"
tell application "Safari" to print window 1 with properties {target
printer:"localhost"}
set _mactemp to (path to temporary items) as Unicode text
set _pdf to POSIX path of _mactemp & "my.pdf"
do shell script "cd /var/spool/cups/; f=`ls -t -name d*001`; cp $f \"" &
_pdf & "\"; open -a preview \"" & _pdf & "\"" with administrator privileges
do shell script "lprm -P localhost"
tell application "localhost" to quit

Laine Lee

ATOM RSS1 RSS2