Automating printing with FreeHand MX

I need to automate freehand to print a list of files in OS X. I used to be able to set the output options by using the following code:

tell application “FreeHand MX”
activate
open this_item
print with PrintFileName and FitToPaper
close without saving
end tell

Now this just pulls up the print menu without passing on the PrintFileName and FitToPaper freehand dictionary commands. So I figured I would script the UI interface to actually set the settings and click the print button. The following script does just that as long as the file is open with the print menu already open:

tell application “FreeHand MX” to activate

tell application “System Events”
if (system attribute “sysv”) < 4144 or UI elements enabled then
tell application process “FreeHand MX”
click button “Print” of UI element 4 of window “Print” end tell
else
tell application “System Preferences”
activate
set current pane to pane “com.apple.preference.universalaccess”
beep
display dialog “GUI Scripting is not enabled.” & return & return & “Check “Enable access for assistive devices” in the Universal Access preference pane (authentication is required), then run this script again.” with icon stop buttons {“OK”} default button “OK”
end tell
end if
end tell

Now here is the crux of the problem: If I added this code after I use the script to open the print command it doesnt work because applescript will pause on the line “print” until that menu action is completed by manually pressing print or cancel. Only after the print window is closed does it move on and try to process the UI commands. Anybody know how I get around this and pass UI commands well applescript is still trying to process a previous print command. I don’t know much about scripting. Is there a simple do while … kind of call?

THANKS

If I understand properly your question, you could try using UI commands to click the Print button under the File menu, instead of send a “print” command.

Here is the new source:

tell application “Finder”
activate
tell application “Finder” to set the source_folder to (folder “Freehand” of folder “ENCHILADA_FOLDERS”) as alias
set source_folder to source_folder as alias
set the item_list to list folder source_folder without invisibles
set source_folder to source_folder as string
repeat with i from 1 to number of items in the item_list
set this_item to item i of the item_list
set this_item to (source_folder & this_item) as alias
set this_info to info for this_item
tell application “FreeHand MX”
activate
open this_item
tell application “System Events”
if (system attribute “sysv”) < 4144 or UI elements enabled then
tell application process “FreeHand MX”
click menu item “Print…” of menu “File” of menu bar item “File” of menu bar 1
click button “Print” of UI element 4 of window “Print”
end tell
else
tell application “System Preferences”
activate
set current pane to pane “com.apple.preference.universalaccess”
beep
display dialog “GUI Scripting is not enabled.” & return & return & “Check “Enable access for assistive devices” in the Universal Access preference pane (authentication is required), then run this script again.” with icon stop buttons {“OK”} default button “OK”
end tell
end if
end tell
close without saving
end tell
end repeat
end tell

It prints the first item in the item_list just like it should. However when it loops for the second item it loses its connection to freehand and stops at the point of the script that says:
tell application “freehand MX”
activate

I put a delay in two see if that would help it out but it actually caused freehand to crash on every print and then open and move onto the next. Do you think this is a print driver problem, or a freehand issue and not a script issue? Anyway thank you so much for your suggestion, I am moving in the right direction now!!

I’m not sure. If it works fine for the first item, it should for the second one, unless the second one is a corrupt document or something rare… :?:

What happens if you put in a try block when you open the item in FH?

tell app "Freehand"
   activate
   try
      open this_item
   on error the error_message number the error_number
      display dialog error_number & ": Error: " & error_message
   end try
...

Okay here is a new script layout


tell application "Finder"
	activate
	tell application "Finder" to set the source_folder to (folder "Freehand" of folder "ENCHILADA_FOLDERS") as alias
	set source_folder to source_folder as alias
	set the item_list to list folder source_folder without invisibles
	set source_folder to source_folder as string
	repeat with i from 1 to number of items in the item_list
		set this_item to item i of the item_list
		set this_item to (source_folder & this_item) as alias
		set this_info to info for this_item
		tell application "FreeHand MX"
			activate
			open this_item without Dialogs
			tell application "System Events"
				tell application process "FreeHand MX"
					click menu item "Print..." of menu "File" of menu bar item "File" of menu bar 1
					click button "Print" of UI element 4 of window "Print"
					click menu item "Close" of menu "File" of menu bar item "File" of menu bar 1
					click button "Don't Save" of window 1
				end tell
			end tell
		end tell
	end repeat
end tell


anyway
This worked once and then when I saved the code and double clicked it to run it again it crashed freehand. I then went back and altered it a couple times and it always crashed freehand. I put it back to the way it was originally, ran it and it worked beautifully. So I saved it, closed it, clicked on it and ran again not from the script editor and it crashed freehand. This is bizarre!!! I am really frustrated.. Anybody out there know why this is happening to me? I use this exact code, without UI comments to save a list of freehand files to eps files with new names and it works great. Also if I take out the part in this code that closes the file then it works great! Only when I close the file (either with UI comments or the freehand dictionary close command) does it crash!! Of course I need to batch thousands of prints a day so obviously I can't not close the freehand files. This is quite a pain. Macromedia will not help me with this and by no means supports any of the applescript functions of their product, which I don't think they have updated their code for four versions now... slackers! Help!