Adobe Illustrator CS & GUI Scripting of Print Dialog

I’ve been trying to go at this a number of ways over the last three days, but no luck.

BACKGROUND:
Trying to get Illustrator to print a PostScript file from Illustrator that crops to the edges of the artwork itself (i.e. not using the Document or Page size, but the “geometric bounds.”). This PS file then goes to Distiller to create a PDF. I don’t want to create the PDF from Illustrator directly for a variety of reasons.

Well once I figured out how to get the geometric bounds, no matter how I plug them into the “print options” class, the resulting PostScript file (when run through Distiller) keeps using the page size. Here’s what’s not working:


tell application "Illustrator CS"
	--"color management options" class not used
	set sep_options to {class:color separation options, convert spot colors:false, over print black:false, separation mode:composite}
	set coord_options to {class:coordinate options, emulsion:false, fit to page:false, horizontal scale:100.0, position:center, tiling:single full page, vertical scale:100.0}
	--"flattener preset" not used
	set flat_options to {class:flattening options, clip complex regions:false, convert strokes to outlines:false, convert text to outlines:false, flattening balance:100, gradient resolution:300.0, overprint:preserve, rasterization resolution:300.0}
	set font_options to {class:font options, download fonts:subset, font substitution kind:oblique substitution}
	set job_options to {class:job options, bitmap resolution:0.0, collate:false, copies:1, designation:visible printable layers, file path:full_path, print area:artboard bounds, print as bitmap:false, reverse pages:false}
	set marks_options to {class:page marks options, color bars:false, page info marks:false, registration marks:false, trim marks:false}
	set paper_options to {class:paper options, height:artwork_height, offset:0, transverse:false, width:artwork_width}
	set ps_options to {class:postscript options, binary printing:false, compatible shading:false, force continuous tone:false, image compression:none, negative printing:false, PostScript:level 2, shading resolution:300.0}
	set ppd_name to "AdobePDF 7.0" as Unicode text
	--"print preset" not used
	set printer_name to "Adobe Postscript® File" as Unicode text
	
	--full version for reference
	set print_options to {class:print options, color separation settings:sep_options, coordinate settings:coord_options, flattener settings:flat_options, font settings:font_options, job settings:job_options, page marks settings:marks_options, postscript settings:ps_options, PPD name:ppd_name, printer name:printer_name}
end tell

In theory, the “paper options” class should be passing on the geometric bounds via artwork_height and artwork_width, but seems to be ignored. I’ve tried changing “artboard bounds” to “artwork bounds” but then Illustrator prints a PS file with most of the objects off the page, no idea why, since doing this manually doesn’t have that effect. Very wierd.

CURRENT PLAN:
So, I’m stepping back and going at this from square-on as a GUI scripting puzzle. Or thought I was. I got stuck before I ever got going. One of the first things I need to set is the Media Size dialog. So far I’ve hit two problems. First was that the menu doesn’t have an assigned name, and seems to change it’s ID for reasons I cannot discern, though it may be related to when “Defined by Driver” was the last choice used. I got around that:


tell application "Illustrator CS"
	activate
	tell current document
		tell application "System Events"
			tell process "Illustrator CS"
				keystroke "p" using {command down}
				
				--Media:Size: popup ID seems to change from 2 to 3 for reasons I can't discern
				if (value of pop up button 2 of window "Print") is "Defined by Driver" then
					set media_size_button to 2
				else
					set media_size_button to 3
				end if
			end tell
		end tell
	end tell
end tell

I’m using PreFab’s excellent UU Browser, which helped me figure out the above (bless them for this tool!). But I cannot seem to click, select, or set a value for that popup button. Wierder is usually if there are choices in a menu like that, UI Browser will show them to you, but not for this one. Is it because it’s dynamic and linked to the OS print archecture? Is this why selecting it via the UI is such an issue?

I need to get this popup button to the “Custom” setting so that I can ungrey the Height & Width settings, which are the key for me. In fact, I just explored and those two fields (Height and Width) do not show up in UI Browser (which means playing the “keystroke tab” game to get to them…).

BACKUP PLAN:
I also tried to find a way to get Acrobat Professional to crop the image (i.e. just let Illustrator do it’s crummy default behavior), but unlike Illustrator, Acrobat doesn’t seem to understand individual objects or “geometric bounds” of any sort that I can discern, at least not via AppleScript. The cropping tool and dialog seem to be able to “crop to fit” but I can’t see a way for AppleScript to kick-start these features. I haven’t checked the JavaScript guide yet to see if it’s possible that route. Kinda trying to not to delve into that rat’s nest…

QUESTION:
So that leaves one of three challenges for me:

–Find some way to script the print dialog to pay attention to custom page settings, or…

–Find a way to GUI my way out of it, or…

–Find a way to crop to the geometric bounds after-the-fact with Acrobat

Are we having fun yet? I’m not. :frowning:

–Kevin

Model: Dual 2.7 GHz G5
AppleScript: (default Tiger 10.6.4)
Browser: Firefox 1.5.0.4
Operating System: Mac OS X (10.4)

My bad…for those needing more context, here is the entire current script. It works (results in a PDF from an open Illustrator CS file), just doesn’t do the cropping I need. Providing in case you need the context of the geometric bounds discovery.


set save_location to (path to desktop) as Unicode text

--open Illustrator file and print to PostScript file
tell application "Illustrator CS"
	activate
	set user interaction level to never interact
	
	set current_document to name of current document as string
	
	--truncate ".ai" from name if present
	--
	tell application "Finder"
		set name_length to length of current_document as number
		set extension_check to items (name_length - 2) through name_length of current_document as string
	end tell
	
	if extension_check is ".ai" then
		set current_document to items 1 through (name_length - 3) of current_document as string
	end if
	
	set full_path to (save_location & current_document & ".ps")
	
	--determine bounds of artwork
	--special thanks to Toby aka "marketforceit" of MacScripter.net
	--
	--get bounds
	set {left_edge, top_edge, right_edge, bottom_edge} to geometric bounds of current document
	
	--get height & width
	set artwork_width to (right_edge - left_edge)
	set artwork_height to (top_edge - bottom_edge)
	
	--load print options
	--
	--"color management options" class not used
	set sep_options to {class:color separation options, convert spot colors:false, over print black:false, separation mode:composite}
	--set coord_options to {class:coordinate options, emulsion:false, fit to page:false, horizontal scale:100.0, position:center, tiling:single full page, vertical scale:100.0}
	--"flattener preset" not used
	set flat_options to {class:flattening options, clip complex regions:false, convert strokes to outlines:false, convert text to outlines:false, flattening balance:100, gradient resolution:300.0, overprint:preserve, rasterization resolution:300.0}
	set font_options to {class:font options, download fonts:subset, font substitution kind:oblique substitution}
	set job_options to {class:job options, bitmap resolution:0.0, collate:false, copies:1, designation:visible printable layers, file path:full_path, print area:artboard bounds, print as bitmap:false, reverse pages:false}
	set marks_options to {class:page marks options, color bars:false, page info marks:false, registration marks:false, trim marks:false}
	set paper_options to {class:paper options, height:artwork_height, offset:0, transverse:false, width:artwork_width}
	set ps_options to {class:postscript options, binary printing:false, compatible shading:false, force continuous tone:false, image compression:none, negative printing:false, PostScript:level 2, shading resolution:300.0}
	set ppd_name to "AdobePDF 7.0" as Unicode text
	--"print preset" not used
	set printer_name to "Adobe Postscript® File" as Unicode text
	
	set print_options to {class:print options, color separation settings:sep_options, flattener settings:flat_options, font settings:font_options, job settings:job_options, page marks settings:marks_options, paper settings:paper_options, postscript settings:ps_options, PPD name:ppd_name, printer name:printer_name}
		
	--print to PostScript® file	
	print current document options print_options
	
	(* deactivate for testing/development
	--close current document saving no
	*)
end tell

--switch to Distiller and create PDF
tell application "Acrobat Distiller 7.0"
	open full_path
end tell

Kevin,

I don’t know if this will help or not. Another member sometime back was having a similar problem printing to post script with the postscipt file being the same size of the artboard. I just came across this in the Illustrator CS guide. This prints to a postscript file than can then be made into a pdf. You can probably use this to print to a pdf through applescript. It will have to be modified for your purposes, but I think the problem is in specifying the printer. This doesn’t specify any printer in particular and seems to work. Might not be the total solution, but maybe a start.

set theDesktop to path to desktop as string
tell application "Illustrator CS"
	set printOptions to {class:print options, job settings:{class:job options, file path:(theDesktop & "printfile.ps" as string), designation:visible printable layers, reverse pages:true}}
	print document 1 options printOptions
end tell

PreTech

Just gave yours a whirl and same results as mine. Only difference really is mine abstracts-out the file path (so there is not so much nesting, for clarity) and his reverses the page order. Mine fine-tunes (or explicitly states) all the options I dare touch.

I should have been more clear that I tried turning on and off quite a few of the options. Only the “job options” class seems required, and even fiddling with that simple line didn’t yield what I needed. :frowning:

(I’m rather hoping this is just a syntax issue or something I overlooked…been staring at the Illustrator CS library and the Adobe Illustrator CS AppleScript guide for a while…).

If you don’t specify a printer in Illustrator it uses whatever printer was used last. No such thing as a “null” that I’m aware of. We use the Acrobat 7 PPD to avoid printer-specific wierdness. It does work manually, it’s the same technique we’ve been using for 5+ years. The muddiness is somewhere between Adobe and/or Apple and AppleScript. :wink:

And before I forget…I always appreciate the help, whether it works or not. Sometimes even the ones that don’t work get me going down a path I hadn’t considered.

Kevin,

Sorry that didn’t help. It seemed to work for me but now, of course, it isn’t. I’ve been playing with this printing problem for quite a while now. I’m out of ideas. I’ve tried every setting and every way to write the settings that I can think of. I’m beginning to think that this is a bug or something that was overlooked (or just something that is supposed to be written in some obscure way that hasn’t been included in the guide). I can’t get it to write to a custom page size or any other size other than letter.:mad: I’ll keep trying. Please post any solutions you may come up with.

PreTech

Actually, now that you mention it, you’re right…it keeps setting Letter size even when I save it with another paper size like Tabloid. Now that is really odd in and of itself.

Hmmm…with that in mind I may have to take the plunge into the JavaScript guides and see if that works. Wish me luck. O.o

–Kevin

Kevin,

I emailed bradevans, the other member looking for a solution to the custom page size to pdf problem. He said he bypassed the print to postscript by opening the files saved as eps’ in distiller. So this is what I came up with from his solution. Note that this is a test script to get a pdf cropped to the artwork bounds. Anyway, hope this may help you.

set filepath to "MacIntosh HD:Users:uName:Desktop:theTest:" as Unicode text
tell application "Illustrator CS"
	activate
	set thisDoc to current document
	tell thisDoc
		set thisDocName to name of thisDoc
		set selection to every page item
		tell application "System Events" to tell process "Illustrator CS" to keystroke "g" using command down
		set selection to group item 1
		set vb to visible bounds of selection
		get position of selection
		set w to (item 3 of vb) - (item 1 of vb) + 2
		set h to (item 2 of vb) - (item 4 of vb) + 2
	end tell
	set newDoc to make new document with properties {height:h, width:w}
	duplicate selection of document thisDocName to newDoc
	tell newDoc
		set position of group item 1 to {1, h - 1}
	end tell
	set fullName to (filepath & (("newFile") as Unicode text))
	save newDoc in file fullName as eps with options {class:EPS save options, CMYK PostScript:true, compatibility:Illustrator 11, embed all fonts:true, embed linked files:true, include document thumbnails:true, overprint:preserve, PostScript:level 2, preview:color Macintosh}
	
end tell

tell application "Acrobat Distiller 6.0.2"
	open fullName
end tell

Thanks to bradevans for his work on this.

PreTech

This was our old technique for Illustrator 8 files. However, with the Transparency and bitmap-like effects in CS, this technique was invalidated. The reason is EPS does not convert the transparencies correctly and/or in a way that many printers can understand. In essance the EPS format does not “understand” all the postscript tricks used to cause the transparency effects. This aauses nasty artifacts and odd printing issues if you try to use the resultant EPS to create a PDF. In some case, a PDF cannot even be created.

We had this same issues trying to save CS files to Acrobat 5 or even Acrobat 6…those older version of Acrobat did not “understand” or “interpret” transparency properly. Sometimes displaying fine but refusing to print, or introducing their own artifacts. Acrobat 7 solved the problem with some finality, thankfully.

Don’t you just love all the fun stuff graphics folks have to work with? :wink:

Adobe’s own incosistancies is why we use .PS files into Distiller rather than making PDFs straight from Illustrator or InDesign…the PDF creation engines are different in each program and do not allow shared settings files. In my world, I need each and every PDF created the exact same way for consistancy across the Mac and PC worlds. I need finite control and fine-tuning of settings that only Distiller allows.

Now I’m finding that Adobe neglected or goofed on a very important aspect…printing in it’s scriptability, and it gets very frustrating.

I do appreciate all the ideas though…nice to see I was in good company, since I’d explored them to some degree myself. :wink:

By the way you might want to pass on my simplified grouping and height/width code to him (courtesy of Toby/marketforceit):


	--get bounds
	set {left_edge, top_edge, right_edge, bottom_edge} to geometric bounds of current document
	
	--get height & width
	set artwork_width to (right_edge - left_edge)
	set artwork_height to (top_edge - bottom_edge)

BTW…not frustrated with you, your help is very appreicated, just venting my annoyance with Adobe. I like them alot more than Quark but times like this they really honk me off. :stuck_out_tongue_winking_eye:

Well, rats man!:frowning: I don’t have to deal with postscript files here and the complexity of the art is not that great (the type of printing we do is fairly limited). Sorry! I tried.

PreTech

Well, I don’t want to discourage you from helping on other stuff, so understand I appreciated it. :slight_smile:

Yeah we’re creating multi-megabyte packaging files, thousands of objects each, using every bell & whistle available. When I come to these forums I don’t like messing with my corney titles and throwing names around, but I work in the graphics department for a top consumer products company. Our art department creates all the packaging and collateral for 40+ distinct brands and countless subbrands (I think over 100). 40+ Mac workstations, and probably about 100 people who have to be able to use the PDFs we generate, from top-end, fully-loaded Mac systems and high-end EFI RIPs to Windows 2000 PCs and your basic HP color office printers (with usually not enough RAM).

Makes my job, “interesting.” I’m kinda your basic jack-of-all-IS/IT-trades. Design needs a solution, the group I’m in makes it happen. Lately, it’s automating tasks…because they are repetitious or to force/encourage standardization. Do more with less, etc. The good news is I often get the time to do things up-front (“pay now”) rather than doing it quick-n-crappy (“pay later”).

It’s fun…it’s just stuff like this that seems so obvious, something I feel like I’m a mere hair away from solving, and nothing. bleh

UPDATE:

Against my own wishes, I’m going to explore two remaining solutions:

–See if I can get the “extended JavaScript” Adobe uses to get this to work, on the off chance it obeys JavaScript better than AppleScript. I say this because the commands available in the JavaScript guide for Illustrator are no different than the ones I can access in AppleScript. :confused:

–Resurrect the idea of building a Preset. If this can get me past the problem I may be okay, though it will still involved selecting a preset then tabbing around and somehow getting dimensions into the Custom Media Size height and width fields.

I’m also hoping starfox has something that might work over in this closely related thread:

http://bbs.applescript.net/viewtopic.php?id=18190

I’ll keep y’all posted and share any solutions I manage to stumble upon.

–Kevin

Kevin,

I may be onto something here. I have gotten the postscript file to be the correct size now when printed. If you open the postscript file in Illustrator it has the correct dimensions of the page size. Unfortunately, distiller is still turning it into an 8 x 11 page. I’ve only got distiller 6. something. Maybe distiller 7 has more scriptability? Six is not very scriptable. There may be settings reachable through applescript that need to be changed in distiller.?
Any way, here is the code I have so far. Still working on it . . .

set fileLoc to "Macintosh HD:Users:uName:Desktop:theTest:" as Unicode text
tell application "Illustrator CS"
	set thisDoc to current document
	set n to name of thisDoc
	set h to height of thisDoc
	set w to width of thisDoc
	set thePPDs to PPDs
	set ppdName to name of item 1 of PPDs
	set thePrinter to item 1 of printers
	set pName to name of thePrinter
	set t to paper sizes of properties of thePrinter
	set t to t & {{class:paper, name:"myPaper", properties:{class:paper properties, width:w, height:h, imageable area:{0.0, 0.0, w, h}, custom paper:true}}}
	set paper sizes of properties of thePrinter to t
	tell thisDoc
		
		set filePath to fileLoc & n & ".ps"
		
		set jOpts to {class:job options, file path:filePath, print area:artboard bounds}
		set pprOpts to {class:paper options, height:h, name:"myPaper", offset:0.0, transverse:false, width:w}
		set psOpts to {class:postscript options, PostScript:level 3}
		set prnOpts to {class:print options, job settings:jOpts, paper settings:pprOpts, postscript settings:psOpts, PPD name:ppdName, printer name:pName}
		
	end tell
	
	print thisDoc options prnOpts
	
end tell

This does insert the custom page size into the printer properties. The problem now seems to be in getting distiller to keep the page size.

PreTech

P.S. Note that item 1 of printers for me is the Adobe PDF printer. Yours maybe something else. I haven’t yet tried another printer that has custom paper sizes to see if that helps (although it shouldn’t make any difference).

Actually, Distiller is very literal in generating PDFs. If it’s generating letter-sized sheets, then it is being fed that by the PostScript file, which means Illustrator still isn’t cropping the document to the artwork bounds like it should be. Everything I’ve tried, limited by my understanding of the way Adobe structures it’s syntax, seems to indicate the Paper properties are ignored for custom paper sizes. :frowning:

I use the “Adobe PostScript® File” printer and AdobePDF PPD.

If you use the AdobePDF printer then you aren’t technically outputting a .PS but directly printing a PDF.

In other words, there are technically three ways to generate a PDF from Illustrator:

–using Save As… from Illustrator
–using Adobe PDF printer from print dialog from Illustrator (no Distiller necessary)
–using Adobe PostScript File then using Distiller

Don’t suppose you’re any good with UI scripting? I’ve refocused a fresh discussion along those lines here:

http://bbs.applescript.net/viewtopic.php?id=18357

I’m closer on this than I was with trying to control through properties.

While it’s a very base and vernacular way to put it, doesn’t this all just suck?