UI Scripting of Illustrator CS Print Dialog?

[The original thread got a bit away from my original intent to discuss UI scripting…wanted to refocus this one on UI scripting. For the original discussion which goes over other options to try to do this, see here]

I’m stumped…either Adobe Illustrator CS is ignoring UI commands, or I’m doing something wrong…

I’m trying to enter valuers into the “Width” and “Height” fields under Media in the General category of the print dialog:


tell application "Illustrator CS"
activate
	tell application "System Events"
		tell process "Adobe Illustrator CS"
			keystroke "p" using {command down} --open print dialog
			keystroke tab --tab over to width field
			keystroke tab
			keystroke tab
			keystroke "3" --test data entry
			keystroke return -- print document
		end tell
	end tell
end tell

The “command-P” works, but once inside the dialog, all UI actions appear to be ignored. The tabs don’t work, data entry doesn’t work, and hitting the default button (either via “return” or via explicity calling out the button) doesn’t work.

I’m definitely way over on the “madness” side and nowhere near the “hobby” side at this point.

[runs screaming around the office]

:o
:mad:
:confused:

–Kevin

Answered my own question. I had to make one subtle change to the script,…


tell application "Illustrator CS"
	activate
	tell application "System Events"
		tell process "Adobe Illustrator CS"
			keystroke "p" using {command down} --open print dialog
			delay 1
			keystroke tab --tab over to width field
			keystroke tab
			keystroke tab
			keystroke "3" --test data entry
			keystroke tab
			keystroke return -- print document
		end tell
	end tell
end tell

The change was the “delay 1.” I figure this out accidentally because my script would work if I accidentally left the print dialog open from the previous failed attempt. Guess UI scripting was too fast for it’s own good. Going to try to apply this delay idea to my other UI scripting issues with this dialog box…

Here’s the latest wrinkle…

I’ve “discovered” what I can best describe as a temporary or transient submenu with this:


tell application "Illustrator CS"
	activate
	tell application "System Events"
		tell process "Adobe Illustrator CS"
			keystroke "p" using {command down} --open print dialog
			delay 1
			--select menu item "A0" of menu 1 of pop up button 3
		end tell
	end tell
end tell

This is an incomplete script, let me explain…

See the commented-out "select menu item “A0” line? That menu, “menu 1 of pop up button 3” is one I need to select an item from…but it’s not always there. :rolleyes:

Most of the time UI Browser only shows as far as “pop up button 3” but no submenus, even though manually selecting it there is obviously a list of paper sizes there.

What I tried doing was doing clicks, selects, sets, delays and such before that line, and every so often the “menu 1” will pop into existance. I need to figure out how to replicate getting it to populate and make itself available to UI scripting in a repeatable manner. And trying to manipulate it when UI Browsers says it isn’t there yields expected errors.

I’ve just spent over an hour just randomly putting code in and out of the spot between “delay 1” and “select menu” and got “menu 1” to pop-up maybe 4 times. But each time, I couldn’t repeat it’s appearance a second time in a row.

As anybody seen “Houdini” menus like this before? Soooo close… :confused:

Kevin,

Here’s my stab at the gui scripting. There will be a dialog box that will pop up asking where to save the file but that might be fairly easy to take care of.

PreTech

Don’t mind the extra stuff in the script.

set fileLoc to "Macintosh HD G52:Users:admin2:Desktop:theTest:" as Unicode text
tell application "Illustrator CS"
	activate
	set thisDoc to current document
	set n to name of thisDoc
	set h to height of thisDoc
	set w to width of thisDoc
	
	
	tell application "System Events"
		tell process "Illustrator CS"
			keystroke "p" using command down
			set t to every pop up button of window "Print"
			tell window "Print"
				click pop up button 4
				click menu item "Adobe PDF" of menu 1 of pop up button 4
				click pop up button 5
				click menu item "Adobe PostScript® File" of menu 1 of pop up button 5
				click pop up button 3
				click menu item "Custom" of menu 1 of pop up button 3
				click button "Printer..."
				keystroke return
			end tell
			
		end tell
	end tell
end tell

P.S. I put the “set t” part in to get the info for the buttons.

PreTech,

On mine, the script errored at:


click menu item "Adobe PDF 7.0" of menu 1 of pop up button 4

(I changed the string to match what we use)

Got the usual error I always get when I’ve tried to select/click or otherwise I do something that isn’t available:

System Events got an error: NSReceiverEvaluationScriptError: 4

That aside, can I ask why you included this:


set t to every pop up button of window "Print"

?

Or is that leftover from exploring the dialog box? If you don’t have PreFab’s UI Browser, I can’t recommend it highly enough. Makes poking-around the menus and dialog boxes much easier.

My apologies if I don’t respond promptly after today…I’m in System Testing for another corporate initiative and that will be occupying huge blocks of my time next week. I still hope to squeeze some time in for this, but can’t promise anything.

As always, thanks!

Kevin,

Since I don’t have CS2, your error might be from where the buttons are placed. The line of code you asked about was something I had in there for testing purposes. What I did was go through the pop up buttons and “click” them via the script and watch which button was actually clicked because the buttons were not in the order you might expect. Here’s the latest bit of code. After you “print” the file, a window asking where you wish to save it pops up with a text field in it asking for a name. I can get a name for it so far, but haven’t gone far enough to try and figure out how to navigate to a folder. One solution is to manually print a file to the one you wish to save in and that will be the default folder selected. Anyway. . .

set fileLoc to "Macintosh HD G52:Users:admin2:Desktop:theTest:" as Unicode text
tell application "Illustrator CS"
	activate
	set thisDoc to current document
	set n to name of thisDoc
	set h to height of thisDoc
	set w to width of thisDoc
	set fileName to n & ".ps"
	
	tell application "System Events"
		tell process "Illustrator CS"
			keystroke "p" using command down
			set t to every pop up button of window "Print"
			tell window "Print"
				
				click pop up button 4
				click menu item "Adobe PDF" of menu 1 of pop up button 4
				click pop up button 5
				click menu item "Adobe PostScript® File" of menu 1 of pop up button 5
				click pop up button 3
				click menu item "Custom" of menu 1 of pop up button 3
				click button "Printer..."
				keystroke return
			end tell
			--get properties of text field 1 of front window --"Print to File"
			set value of text field 1 of front window to fileName
			keystroke return
		end tell
	end tell

Another problem arises if the folder selected already has a file of the same name in it. Another dialog box will pop up asking to replace or not.

What I have been doing with this is inserting code such as:

get properties of text field 1 of front window

so I can see what names/items are part of the text field/window/button and so on. In AS Studio, a text field has contents whereas this has “value”.

Hope this helps some. So far this has been easier than straight scripting! :wink:

PreTech

P.S. I just ran the above script a couple times in a row. On the second go round, it would give an NSError deal. I’m not exactly sure why, but I added a “delay 2” after the file is printed and it seems to work alright.

Kevin,

Bad news man.:frowning: After a shutdown of the computer and restart, my previous script (which works for the most part) will not get the print dialog to actually change the printer from what ever was previously selected. Eventhough you can see the buttons being clicked and changed, the changes don’t actually take effect. If you print from Illustrator manually first, the correct printing will take place. However, the custom page size will remain however it was set from your last print dialog!:o:mad::mad: I tried changing my test page custom size and it retained the last actual page size from the manual print command. Bummer. I did just run into this link which may lead to something, but we will see. http://bbs.applescript.net/viewtopic.php?id=18425

PreTech

DOH! I hadn’t had time to fiddle with your solution yet. Well I’ll use your script as a basis for fiddling, sounds like you got farther than I did. Not sure if I’ll be working on this script or my “automating Lotus Notes script” as a priority this week.

I checked the other link…what part seems promising? I can’t use CUPS (use Distiller, Obi Wan) and I don’t think they’ll let me deploy additional libraries. And guessing the button location is a bad idea…we have sidekicks (smaller, secondary monitors) and Adobe sometimes plops the dialog on the smaller display at random, which means I can’t use clicks that require coordinates. :confused:

I’ll keep XTool on the radar though, just in case.

Kevin,

This is the part I was thinking of as being useful, however you can’t select the “Adobe Postscript® File” as a printer from the printer setup.:frowning:

(*
1) Go to http://www.codepoetry.net/projects/cups-pdf-for-mosx
   CUPS-PDF is a backend module for CUPS (Mac OS X's printing system) 
   by Volker C. Behr that, rather than printing to a device, prints 
   straight to PDF files that appear in a folder on your desktop.
2) Download and install the package (requires an admin password)
3) Follow the simple instructions on the CodePoetry site
*)

property OP : missing value
tell application "Printer Setup Utility"
   set OP to current printer
   set current printer to printer "Virtual Printer"
end tell

-- later on after getting your pdf files by printing to the Virtual Printer
delay 3 -- you can watch to check that this works in the Printer List window

tell application "Printer Setup Utility" to set current printer to OP

I’m still playing with it though. It seems to me that somehow the print dialog window is not “active” (or something like that) since it doesn’t really register the “clicks”.

PreTech

What I was getting at though is the commented section says CUPS-PDF generates PDFs directly. There are lots of ways to output a PDF directly from Illustrator, even with scripts. :wink:

I need a “postscript file” (.ps) for use with Distiller. I want Distiller in the loop because our PDF creation workflow revolves around using it as a standardization enforcement tool via the use of “.joboptions” files.

BTW: saw your other thread and will keep an eye on it. Looks like I’m going to bang my head against even wierder things the next couple of weeks…making Lotus Notes obey me. :rolleyes:

Yeah, I didn’t get into the CUPS thing too much. I was interested in the code for selecting the printer you want to use. Unfortunately, the postscript printer that you need to select seems to be an Illustrator internal thing that you can’t access from Printer Setup.

PreTech

Hey guys

Been following your Posts on this subject has i use illustrator a lot but never needed to script the print dialog.
Anyhow here’s my attempt at scripting this problem…
Gotta say i think i’ve had some success but this may just be my machine and my settings…
some of the options are not needed but you can delete as you see fit

set filePath to alias "ai file on desktop tec.. etc.."
tell application "Adobe Illustrator"
	set docname to get the name of the current document
	set FullPath to filePath & docname & ".ps" as Unicode text
	set |Adobe PostScript® File| to "Adobe PostScript® File" as Unicode text
	set |Adobe PDF| to "Adobe PDF 7.0" as Unicode text
	set |paper options| to {class:paper options, height:1000.0, name:"custom", offset:1000.0, transverse:true, width:1000.0}
	set |page mark options| to {class:page marks options, registration marks:false, color bars:false, page info marks:false, trim marks:false}
	set |color management options| to {class:color management options, name:"[Default]"}
	set |color separation options| to {class:color separation options, separation mode:host based separation, over print black:false}
	set |coordinate options| to {class:coordinate options, fit to page:true, horizontal scale:100.0, vertical scale:100.0, orientation:landscape, tiling:single full page}
	set |flattening options| to {class:flattening options, clip complex regions:false}
	set |postscript options| to {class:postscript options, binary printing:true}
	set |job options| to {class:job options, bitmap resolution:0.0, collate:false, copies:1, designation:all layers, file path:FullPath, name:"untitled-1.ai", print area:artwork bounds, print as bitmap:false, reverse pages:false}
	set |print options| to {class:print options, printer name:|Adobe PostScript® File|, PPD name:"Adobe PDF 7.0", job settings:|job options|, paper settings:|paper options|, page marks settings:|page mark options|, color management settings:|color management options|, coordinate settings:|coordinate options|, color separation settings:|color separation options|, postscript settings:|postscript options|}
	print current document options |print options| --without dialogs
	close current document saving no
end tell

seemed to work better for me when i put the job options in the same order has they were in the illustrator dictionary…

Hope it helps

cheers

this was tested on illustrator cs2

Pidge1,

Tried your script and it was a valiant attempt, alas it still returns a page size of only 8.5" by 11". :frowning:

PreTech

P.S. Of course I’m still using Illustrator CS, so CS2 may make the difference.

Hey pretech

i was getting good results yesterday with this, maybe its cause it was in CS2 i will have another look tonite when i get to work
to see if i can recreate the same results in just CS.

cheers

I’ll have a look probably next week in CS2. Out users have CS1 but I have CS2 for testing in advance of deployment. If it works in CS2 by some miracle I can probably push to put-off the need for the automation until then.

I’m just about to dive into learning FileMaker 8 (we’ve been stuck at 6 for a while) and working on a Lotus Notes + FileMaker automation that is on a higher priority than my PDF automation. I’ll keep watching, but might not have anything constructive to add until later next week or the week after.

Sorry Guys!!

set |coordinate options| to {class:coordinate options, fit to page:true, horizontal scale:100.0, vertical scale:100.0, orientation:landscape, tiling:single full page}

I was lulled into a false sense of security by illustrator…
and it was the fit to page option in the above line which tricked me…

Ahh Well keep plodding!!!

Yeah I don’t know about PreTech, but I’ve felt that way quite a few times in the course of figuring out this puzzle. :confused:

I’m slowly coming to realize that the harder I push on AppleScript the more it starts to show it’s seedy underside. Things like this one though I don’t blame Apple for. I’ve had trouble since Day 1 when trying to make Adobe products jump through hoops. Their implementation is quite, hmmm, “odd” at times.