script continues after print

HI
this script below works but at the end when it prints
and brings up the dialog box the script does something else
and you have to click on the print window to bring it to the front before continueing. It did not do this before I added the if button = button returned part of the script.
Also is there another way to reference a piece of a script that is repeated instead of repeating as below.
any help would be appreciated.
Daniel

tell application “MultiAd Creator Pro”
if document 1 exists then
set theFolder to (folder of document 1) as text
set theName to name of document 1
end if
if text 1 thru 7 of theName is not equal to text 15 thru 21 of (theFolder) then
set wrong_name to button returned of ¬
(display dialog “This advert may be named Incorrectly” buttons {“Cancel”, “OK”} default button “Cancel” with icon stop)
if wrong_name = “OK” then
save document 1
set targetPath to “ADVERTS IN:”
set documentName to name of document 1
export EPS spread 1 of document 1 saving in (targetPath & documentName) font inclusion no fonts preview options color or gray export method composite scaling by 100.0 with binary encoding and level 2 PostScript without TIFF OPI Comments, OPI Omit TIFF and OPI Omit EPS

                    with timeout of 300 seconds
                         tell application "MultiAd Creator Pro"
                              print document 1 with interaction, text overflow checking and missing font checking
                         end tell
                    end timeout
         end if

else
set folder_path to (theFolder)

            tell application "Finder"
                   set names_ to (name of files of folder folder_path whose name begins with text 15 thru 21 of theFolder & "00" and name ends with ".HED") as list
            end tell
            
            set highest_ to item 1 of names_
            repeat with i from 2 to (count names_)
              if item i of names_ is greater than highest_ then
                           set highest_ to item i of names_
                    end if
      end repeat
          
            set f to text 8 thru 9 of highest_
          
            tell application "MultiAd Creator Pro"
              if text 8 thru 9 of theName is not equal to f then
                          set update_name to button returned of ¬
                            (display dialog "This advert may have been updated" buttons {"Cancel", "OK"} default button "OK" with icon stop)
                            if update_name = "OK" then
                                  save document 1
                            set targetPath to "ADVERTS IN:"
                            set documentName to name of document 1
                              export EPS spread 1 of document 1 saving in (targetPath & documentName) font inclusion no fonts preview options color or gray export method composite scaling by 100.0 with binary encoding and level 2 PostScript without TIFF OPI Comments, OPI Omit TIFF and OPI Omit EPS
                                
                                    with timeout of 300 seconds
                                         tell application "MultiAd Creator Pro"
                                              print document 1 with interaction, text overflow checking and missing font checking
                                         end tell
                                    end timeout
                         end if
              else
                        save document 1
                    set targetPath to "ADVERTS IN:"
                    set documentName to name of document 1
                      export EPS spread 1 of document 1 saving in (targetPath & documentName) font inclusion no fonts preview options color or gray export method composite scaling by 100.0 with binary encoding and level 2 PostScript without TIFF OPI Comments, OPI Omit TIFF and OPI Omit EPS
                        
                            with timeout of 300 seconds
                                 tell application "MultiAd Creator Pro"
                                      print document 1 with interaction, text overflow checking and missing font checking
                                 end tell
                            end timeout
                 end if
      end tell
    end if

end tell

Hi,

With regard to having to click on the print dialog to bring it to the front: What application is at the front when you have to click the print dialog: MultiAd Creator Pro or some other application?

With regard to referencing the same code without repeating it: If I understand what you’re asking, you want to use a handler. You put repeated code inside a wrapper like this:

on handlerName()
– code
end

… and then call it like this:

handlerName()

There’s a lot more to it. You’ll want to consult a reference. The AppleScript Language Guide PDF file linked to from the front page of macscripter.net would be a good start.

Hi

I figured out the first problem I did not set “activate” in script. It now works fine.

Thanks for the handler info I have just found an example of one and now it makes sense.

Thanksvery much