I’m trying to print text files with some print options like scaling and no header without user intervention. Checking the TextWrangler dictionary the options/settings I need are available but I’m not having any success to set the option properties.
Here is my script, actually a folder action:
on adding folder items to this_folder after receiving added_items
repeat with this_item in added_items
tell application "TextWrangler"
set the page headers of print settings to false
print this_item
end tell
end repeat
end adding folder items to
If I remove the “set the page headers of print settings to false” line the file prints.
Have tried a few variations but definitely not the right one re adding/setting options.
Hi,
the dictionary of TextWrangler says, print settings is the property parameter of the print command,
try this
on adding folder items to this_folder after receiving added_items
repeat with this_item in added_items
tell application "TextWrangler"
print this_item with properties {page headers:false}
end tell
end repeat
end adding folder items to
No luck.
The only way I can get it to print at all is if I use “print this_item” by itself.
Perhaps there is a problem with TextWrangler. I’ll do some testing using TextEdit.
All the print settings properties are “get” not “set”. In TextWrangler’s Preferences, however, there is a “Text Printing” preference with a “Print page headers” checkbox that defaults to checked.
You mean there is actually no way to “set” print parameters? Duh, of course. r/o staring me in the face. I guess I just didn’t want to believe/see it. Thanks for forcing my eyes to open with brain engaged.
I did find the checkbox and that does solve the heading problem but my ultimate goal is to scale the printing so that 132 columns fit on a letter size page. I was using the ‘page headers’ as it seemed easy to confirm my scripting was correct, I.e. proof of concept/syntax.
Back to the drawing board then to find a way to print 132 columns without postscript. I can do it via the print dialog but I need to be able to do it without user intervention.
Any/all suggestions welcomed.
Basically I have reports formatted for 132 columns, as per wide carriage dot matrix printer, that now need to be made to fit on letter size laser. I can do it to a postscript laser printer and I can do it via scaling in the print dialog when printing the file from TextEdit manually but I need to be able to find some way to do it without user intervention to a laser that doesn’t do postscript.
If you want to print plain text files, then you could also try to use the «lp» command:
You can execute the «lp» command from within an AppleScript using the «do shell script» command.
How do you get lp to recognize your default printer as the output device?
lp would work great, it indeed it worked with options of cpi or scale.
If you use lp without options of cpi or scaling it prints HUGE
with cpi=17 it prints at 10cpi so is at least usable for that but it doesn’t really adjust cpi to anything but 10, which is ‘supposed’ to be the default.
scaling=50 has no effect either, I.e. prints at approx 10 cpi
even tried -o landscape but on that I got no output at all.
That’s really the root of my problem. On other 'nixes I use the lp command with great success. Which it would work on OS X.
As an aside, had to use CUPS admin to set a default printer for lp. I think there are command line utilities but CUPS admin worked for that. Have not found a way to set cpi or scaling though.
Printing works great for users on OS X but for apps that print without user intervention there just doesn’t seem to be a good work-around until lp gets fixed, which of course may be never.
If you really need to have full control over the output layout, then you might consider to create your own custom PDF document. I am using this technique a lot for my current employer and find it to be extremely helpful.
But you cannot do it with AppleScript. You need to use Python & Mac OS X 10.5 Leopard.
The (very simple) sample code below will create a DIN A4 PDF document named custompdf.pdf on your desktop, that contains the sentences «Hello, my name is Wintercorn. Jimmy Wintercorn. I am from Boston. And I hate snow.» written in black Courier 12 pt with normal character spacing.
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
from CoreGraphics import *
# name of current user
usr = os.getlogin()
# pdf file path
filepath = '/Users/%s/Desktop/custompdf.pdf' % (usr)
# pt values for DIN A4
pagewidth = 595
pageheight = 842
# creating an empty PDF document in DIN A4 Portrait format
pagebox = CGRectMake(0, 0, pagewidth, pageheight)
pdfcontext = CGPDFContextCreateWithFilename(filepath, pagebox)
if not pdfcontext:
errmsg = "Could not create PDF file at '%s'" % (filepath)
raise IOError, errmsg
# setting the color space to generic RGB
pdfcontext.setFillColorSpace(CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB))
# 1st page
pdfcontext.beginPage(pagebox)
pdfcontext.saveGState()
pdfcontext.setTextDrawingMode (kCGTextFill)
pdfcontext.setTextMatrix(CGAffineTransformIdentity)
# setting font, font size and text encoding
pdfcontext.selectFont('Courier', 12, kCGEncodingMacRoman)
# setting character spacing
pdfcontext.setCharacterSpacing(1)
# setting font color (red, green, blue, alpha)
pdfcontext.setRGBFillColor(0, 0, 0, 1)
text = u'Hello, my name is Wintercorn. Jimmy Wintercorn.'.encode('macroman')
pdfcontext.showTextAtPoint(40, 800, text, len(text))
text = u'I am from Boston. And I hate snow.'.encode('macroman')
pdfcontext.showTextAtPoint(40, 785, text, len(text))
pdfcontext.restoreGState()
pdfcontext.endPage()
pdfcontext.finish()
del pdfcontext
This way you could create custom PDF reports and directly print them with the «lp» or «lpr» command without getting in any layout troubles.
Thanks Martin.
Would each page in a multi-page report be treated as a 1st page or would I have to do something extra?
Some of our reports are long, separated by Ctrl/L at each page-feed.
Thanks again. Will do some perusing
You can easily create PDF documents that contain multiple pages. You only need to repeat the page creation and finishing steps several times:
pdfcontext.beginPage(pagebox)
pdfcontext.saveGState()
content for the new page
pdfcontext.restoreGState()
pdfcontext.endPage()
The following sample code will create a PDF document with 10 pages. Unfortunately the forum cannot display Python code well, so here is the whole script.
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
from CoreGraphics import *
# name of current user
usr = os.getlogin()
# pdf file path
filepath = '/Users/%s/Desktop/custompdf.pdf' % (usr)
# pt values for DIN A4
pagewidth = 595
pageheight = 842
# creating an empty PDF document in DIN A4 Portrait format
pagebox = CGRectMake(0, 0, pagewidth, pageheight)
pdfcontext = CGPDFContextCreateWithFilename(filepath, pagebox)
if not pdfcontext:
errmsg = "Could not create PDF file at '%s'" % (filepath)
raise IOError, errmsg
# setting the color space to generic RGB
pdfcontext.setFillColorSpace(CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB))
for i in range(1, 11):
# 1st page
pdfcontext.beginPage(pagebox)
pdfcontext.saveGState()
pdfcontext.setTextDrawingMode (kCGTextFill)
pdfcontext.setTextMatrix(CGAffineTransformIdentity)
# setting font, font size and text encoding
pdfcontext.selectFont('Courier', 12, kCGEncodingMacRoman)
# setting character spacing
pdfcontext.setCharacterSpacing(1)
# setting font color (red, green, blue, alpha)
pdfcontext.setRGBFillColor(0, 0, 0, 1)
text = u'Hello, my name is Wintercorn. Jimmy Wintercorn.'.encode('macroman')
pdfcontext.showTextAtPoint(40, 800, text, len(text))
text = u'I am from Boston. And I hate snow.'.encode('macroman')
pdfcontext.showTextAtPoint(40, 785, text, len(text))
pdfcontext.restoreGState()
pdfcontext.endPage()
pdfcontext.finish()
del pdfcontext
Thank you very much Martin. Off to do some python research