Author Topic: q8 as: collect using sys events trouble with save path  (Read 4093 times)

0 Members and 1 Guest are viewing this topic.

Offline 4610036

  • Jr. Member
  • **
  • Posts: 50
  • Karma: 0
  • Gender: Female
q8 as: collect using sys events trouble with save path
« on: April 23, 2009, 12:29:34 AM »
hi everyone
thanks for all the help so far. i am still feeling my way with subroutines (my last post), but need help with this latest conundrum:
basically i am trying to automate a collect -- which i'm pretty sure i read here wasnt scriptable -- so figured this might be a way to go.  i want to put a list of job folder numbers in, then do a repeat for each, which involves: creating a folder (within job folder) for the Collect; and then directing the Collect to the newly-created folder (for each one)....(and a few more steps i might come back for more help on, once i get past this current hurdle).

Problem:   i get the job opened and the collect window open and then i want to tell the collect window to save to a certain path.  which i have as a POSIX. but i dont know how to talk to the window....

set finalfolder to (":Volumes:CLIENTS:" & jobnum & ":" & cofinal & " Final")
   tell application "QuarkXPress"
      tell application "System Events"
         tell process "QuarkXPress"
            tell menu item "Collect for Output..." of menu 1 of menu bar item "File" of menu bar 1
      click
      delay 1
-- this is for when the program asks you to save the file... (which of course it doesnt always ask).

      keystroke "s" using {command down}

-- right here the "collect" window pops up. If it was one user doing one job, they would select a folder and hit "save"
      
And this is where i come to a skidding halt.

i know this is a strange one, and quite hard to explain, but if anyone has ideas on how to go about this, i would really appreciate it.
many thanks.
m./

Offline cdms

  • Full Member
  • ***
  • Posts: 111
  • Karma: 6
  • Gender: Male
Re: q8 as: collect using sys events trouble with save path
« Reply #1 on: April 29, 2009, 02:28:39 PM »
To go further down this route you need to start UI scripting and work out how to populate the fields in the dialog and press the buttons. This gets really messy very quickly. There is a tool from prefabsoftware that will help you find paths to different bits of some dialog boxes (try the 30 day trial) but I haven't been able to get it to show everything in the boxes I have tried to do this for.

In the end it may be easier to script the functionality you want (e.g. get the list of pictures and script the finder to copy them into a collect folder) rather than actually using the collect window.


Offline 4610036

  • Jr. Member
  • **
  • Posts: 50
  • Karma: 0
  • Gender: Female
Re: q8 as: collect using sys events trouble with save path
« Reply #2 on: June 17, 2009, 03:22:40 AM »
hi cdms. 
thanks for the reply. i didnt realize i hadnt posted back to thank you. 
i can indeed confirm that things did get messy pretty quickly in the system events world.  i think your suggestion of scripting individual elements is a good one. of course, despite your sound words of advice i have still doggedly pursued the other angle. '-) the prefabsoftware has been very helpful (another good suggestion).

one thing i'm still having trouble with (and i know its all down to my inexperience here), is getting the script to not "run away" on me.  i read on this site, i think, what seemed like a good trick:

repeat until window "Collect for Output" exists
end repeat


which seemed to work, but the same theory (to wait for the file to open up):

repeat if not (exists document 1)
end repeat


was not so well received.  it seemed to spool. 

i even tried:
try
with timeout 10
repeat if not (exists document 1)
end repeat
end timeout
end try


since i'm going against your advice, i'll understand if you dont want to talk to me anymore.  (and very tardy i am in replying.  see i only speak to you wen i need help). 

but, ya know, all help gratefully received, if not always entirely understood (or followed). ;D
mm./

Offline Jean-Marie Schwartz

  • Sr. Member
  • ****
  • Posts: 436
  • Karma: 6
  • Gender: Male
Re: q8 as: collect using sys events trouble with save path
« Reply #3 on: June 17, 2009, 03:50:13 AM »
Quote
repeat until window "Collect for Output" exists
end repeat

which seemed to work, but the same theory (to wait for the file to open up):

repeat if not (exists document 1)
end repeat
Hum! sorry but to me it's not really the same: on one hand you have "repeat until" and on the other hand you have "repeat if not exists…" which is not really the same, is it?
Give a shot at "repeat until document 1 exists". (Haven't tried it, sorry.)
Jean-Marie Schwartz
May the Schwartz be with you :-)

= Mac Pro Quad-Core Intel 2.8 GHz • 10 Go 1066 MHz DDR3 • Mac OS 10.8.5 • QuarkXPress 9.5.4.1 • Adobe CS5 =

Offline larsen67

  • Sr. Member
  • ****
  • Posts: 459
  • Karma: 10
  • Gender: Male
Re: q8 as: collect using sys events trouble with save path
« Reply #4 on: June 17, 2009, 07:13:03 AM »
I tried a Q&D test using GUI scripting and you may get some luck out of it. I have no idea if the dialog is the same in V8. This is what worked ffor me although I have not put in the checkbox options at this point.

001   tell application "QuarkXPress"
002        activate
003        tell document 1
004             set DocName to name
005             if modified is false then
006                  my Collect_Output(DocName)
007             end if
008        end tell
009   end tell
010   
011   on Collect_Output(DocName)
012        tell application "System Events"
013             if UI elements enabled then
014                  tell process "QuarkXPress"
015                       tell menu bar 1
016                            tell menu "File"
017                                 click menu item "Collect for Output..."
018                            end tell
019                       end tell
020                       delay 0.5
021                       repeat until (exists window "Collect for Output")
022                            delay 0.5
023                       end repeat
024                       keystroke "D" using command down -- Go to Desktop
025                       delay 0.5
026                       tell window "Collect for Output"
027                            tell button "New Folder"
028                                 set {xPosition, yPosition} to position
029                                 set {xSize, ySize} to size
030                            end tell
031                       end tell
032                       tell application "Extra Suites"
033                            ES move mouse {xPosition + (xSize div 2), yPosition + (ySize div 2)}
034                            ES click mouse
035                       end tell
036                       keystroke (DocName & space & "Collected")
037                       delay 0.5
038                       keystroke return
039                       delay 0.5
040                       keystroke return
041                       delay 1
042                       try -- Incase your NOT collecting fonts
043                            tell window 1 -- Font Warning Dialog
044                                 tell button "OK"
045                                      set {xPosition, yPosition} to position
046                                      set {xSize, ySize} to size
047                                 end tell
048                            end tell
049                            tell application "Extra Suites"
050                                 ES move mouse {xPosition + (xSize div 2), yPosition + (ySize div 2)}
051                                 ES click mouse
052                            end tell
053                       end try
054                  end tell
055             else
056                  display dialog "UI element scripting is not enabled." & ¬
057                       return & "Check \"Enable access for assistive devices\"" giving up after 6
058             end if
059        end tell
060   end Collect_Output

Offline larsen67

  • Sr. Member
  • ****
  • Posts: 459
  • Karma: 10
  • Gender: Male
Re: q8 as: collect using sys events trouble with save path
« Reply #5 on: June 17, 2009, 09:00:41 AM »
OK, bored so I had a go at including the 6 checkboxes too. This is working for me with a basic test file.

001   tell application "QuarkXPress"
002        activate
003        tell document 1
004             set DocName to name
005             if modified is false then
006                  -- Value 1 enables checkbox & Value 0 disables checkbox
007                  my Collect_Output(DocName, 1, 1, 0, 1, 1, 1)
008             end if
009        end tell
010   end tell
011   
012   on Collect_Output(DocName, PF, SF, CP, EP, LP, L)
013        tell application "System Events"
014             if UI elements enabled then
015                  tell process "QuarkXPress"
016                       tell menu bar 1
017                            tell menu "File"
018                                 click menu item "Collect for Output..."
019                            end tell
020                       end tell
021                       delay 0.5
022                       repeat until (exists window "Collect for Output")
023                            delay 0.5
024                       end repeat
025                       keystroke "D" using command down -- Go to Desktop
026                       delay 0.5
027                       tell window "Collect for Output"
028                            if (value of checkbox 1) = 0 then -- Dialog dropdown we want checked to access new folder button
029                                 tell checkbox 1
030                                      set {xPosition, yPosition} to position
031                                      set {xSize, ySize} to size
032                                 end tell
033                                 tell application "Extra Suites"
034                                      ES move mouse {xPosition + (xSize div 2), yPosition + (ySize div 2)}
035                                      ES click mouse
036                                 end tell
037                            end if
038                       end tell
039                       tell UI element 1 of UI element 1 of UI element 1 of UI element 1 of UI element 1 of UI element 9 of window "Collect for Output"
040                            if (value of checkbox "Printer Fonts") is not PF then
041                                 tell checkbox "Printer Fonts"
042                                      set {xPosition, yPosition} to position
043                                      set {xSize, ySize} to size
044                                 end tell
045                                 tell application "Extra Suites"
046                                      ES move mouse {xPosition + (xSize div 2), yPosition + (ySize div 2)}
047                                      ES click mouse
048                                 end tell
049                            end if
050                            if (value of checkbox "Screen Fonts") is not SF then
051                                 tell checkbox "Screen Fonts"
052                                      set {xPosition, yPosition} to position
053                                      set {xSize, ySize} to size
054                                 end tell
055                                 tell application "Extra Suites"
056                                      ES move mouse {xPosition + (xSize div 2), yPosition + (ySize div 2)}
057                                      ES click mouse
058                                 end tell
059                            end if
060                            if (value of checkbox "Colour Profiles") is not CP then
061                                 tell checkbox "Colour Profiles"
062                                      set {xPosition, yPosition} to position
063                                      set {xSize, ySize} to size
064                                 end tell
065                                 tell application "Extra Suites"
066                                      ES move mouse {xPosition + (xSize div 2), yPosition + (ySize div 2)}
067                                      ES click mouse
068                                 end tell
069                            end if
070                            if (value of checkbox "Embedded Pictures") is not EP then
071                                 tell checkbox "Embedded Pictures"
072                                      set {xPosition, yPosition} to position
073                                      set {xSize, ySize} to size
074                                 end tell
075                                 tell application "Extra Suites"
076                                      ES move mouse {xPosition + (xSize div 2), yPosition + (ySize div 2)}
077                                      ES click mouse
078                                 end tell
079                            end if
080                            if (value of checkbox "Linked Pictures") is not LP then
081                                 tell checkbox "Linked Pictures"
082                                      set {xPosition, yPosition} to position
083                                      set {xSize, ySize} to size
084                                 end tell
085                                 tell application "Extra Suites"
086                                      ES move mouse {xPosition + (xSize div 2), yPosition + (ySize div 2)}
087                                      ES click mouse
088                                 end tell
089                            end if
090                            if (value of checkbox "Layout") is not L then
091                                 tell checkbox "Layout"
092                                      set {xPosition, yPosition} to position
093                                      set {xSize, ySize} to size
094                                 end tell
095                                 tell application "Extra Suites"
096                                      ES move mouse {xPosition + (xSize div 2), yPosition + (ySize div 2)}
097                                      ES click mouse
098                                 end tell
099                            end if
100                       end tell
101                       tell window "Collect for Output"
102                            tell button "New Folder"
103                                 set {xPosition, yPosition} to position
104                                 set {xSize, ySize} to size
105                            end tell
106                       end tell
107                       tell application "Extra Suites"
108                            ES move mouse {xPosition + (xSize div 2), yPosition + (ySize div 2)}
109                            ES click mouse
110                       end tell
111                       keystroke (DocName & space & "Collected")
112                       delay 0.5
113                       keystroke return
114                       delay 0.5
115                       keystroke return
116                       delay 1
117                       try -- Incase your NOT collecting fonts
118                            tell window 1 -- Font Warning Dialog
119                                 tell button "OK"
120                                      set {xPosition, yPosition} to position
121                                      set {xSize, ySize} to size
122                                 end tell
123                            end tell
124                            tell application "Extra Suites"
125                                 ES move mouse {xPosition + (xSize div 2), yPosition + (ySize div 2)}
126                                 ES click mouse
127                            end tell
128                       end try
129                  end tell
130             else
131                  display dialog "UI element scripting is not enabled." & ¬
132                       return & "Check \"Enable access for assistive devices\"" giving up after 6
133             end if
134        end tell
135   end Collect_Output

Offline 4610036

  • Jr. Member
  • **
  • Posts: 50
  • Karma: 0
  • Gender: Female
Re: q8 as: collect using sys events trouble with save path
« Reply #6 on: June 17, 2009, 09:24:59 AM »
whoa. larsen.  youre bored and this is what you come up with? awesome!

this is where i get into trouble... its like wen you go to a foreign country and you learn enough to order the good food and book a room, and then someone starts talking philosophy...  i'm trying to keep up.

this is the entirety of my script, which i know is terrible to post the whole damn thing... but i dont know how else to show you where i'm at.  i remoted into my office computer to pick this up, so will wait to look more closely at your script and try it out... i guess what i'm saying is, ive never used the x,y coordinates thing.  its been all i can do to figure out an individual button name, and then get some syntax going.  this script works about half the time, or it seems to (althou i was tinkering with it a lot yesteray -- which is why there's some stuff commented out, that is just trying to get something to work or not).  sometimes it gets all the way thru and then doesnt delete the file (at the very bottom of the script, thus "failing"). i figured if it works once, then it should always work. and if it doesnt work all the time, then its because the script is gonig faster than my computer -- which speaks to server connxn time and file size variances

jean-marie.

i agree with you. the two statements are not the same at all!

THANK YOU, both! i really appreciate your input. i'll just try and figure out how this gels with what i had.....

but here it is... and now youll see how awkward my coding is.  :-\

global ansbutton
global collectpath
global Destination
global filea
global fileapath
global joblist
global jobnum
global kcn
global kcnfin
global kcnfinfold
global multi_file
global notdone
global notdone_error
global this_file

set notdone to {}

display dialog ("Input a job number:" & return & "separate by SPACES" & return & return & return & "This will not source files already archived" & return & return & "-- If there is more than one Quark File, the most-recently modified is used") default answer "000125 000124 000123"
set joblist to the text returned of the result as string
set ATID to AppleScript's text item delimiters
set AppleScript's text item delimiters to space
set joblist to (every word of joblist)
set AppleScript's text item delimiters to ATID

repeat until joblist is {}
   try
      set jobnum to (text item 1 of joblist) as string
      set jobnum to ("V" & jobnum) as string
      set AppleScript's text item delimiters to ATID
      
      open_jobfile()
      set kcn to result
      set kcnfin to (the first word of kcn) as string
      set kcnfinfold to (kcnfin & " Final") as string
      set collectpath to ("//Volumes/CLIENTS/" & jobnum & "/" & kcnfinfold & "/")
      make_finfold()
      do_collect()
      print_cropmarkPDF()
      close_file()
      stuff_collect()
      move_2_Collect()
      delay 1
   on error
      if (count of notdone) is equal to 0 then
         set notdone to jobnum
      else
         set notdone to notdone & ", " & jobnum
      end if
   end try
   set joblist to the rest of joblist
end repeat

-----------------------------------------
-- ERROR
-----------------------------------------


if (count of notdone) is equal to 0 then
   display dialog ("Your collects are finished")
else
   display dialog ("Collect was NOT completed for:" & return & return & notdone & return & return & "You'll need to go back and figure out why, and then run collect on the individual job")
end if


-----------------------------------------
-- SUBROUTINES:
-----------------------------------------

on open_jobfile()
   -----------------------------------------
   -- Open Job Folder and Make Alias
   -----------------------------------------
   set openfilepath to ("VOLUMES:CLIENTS:" & jobnum & ":")
   tell application "Finder"
      make new alias file at desktop to folder jobnum of disk "CLIENTS" of application "Finder"
      open result
      -----------------------------------------
      -- get names of all quark files starting with "k"
      -----------------------------------------
      set multi_file to (get every document file of front Finder window whose name extension is in {"qxd", "qxp"} and name starts with "k") as list
      -----------------------------------------
      -- sort finder window by last modified date
      -----------------------------------------
      set sortfile to sort multi_file by modification date
      -----------------------------------------
      -- check if there is more than one file.
      -- if so, use the most-recently modified
      -----------------------------------------      
      -- if (count of multi_file) is greater than 1 then
      set multi_file to last item of mutli_file
      -- end if
      
   end tell
   -----------------------------------------
   -- open the job with Suppress Warnings
   -----------------------------------------
   
   tell application "QuarkXPress"
      activate
      open multi_file with Suppress All Warnings
      delay 1
      if not (exists project 1) then
         delay 6
      end if
      -----------------------------------------
      -- get the kcomm job number (to use for naming the "Final" folder
      -----------------------------------------
      tell layout space 1 of project 1
         set kcn to name as string
      end tell
      return kcn
   end tell
end open_jobfile

on close_file()
   tell application "QuarkXPress"
      close document 1 saving no
   end tell
end close_file

on make_finfold()
   -----------------------------------------
   -- check for a pre-existing "FINAL" folder
   -- if found, rename with "old"
   -- then CREATE a new "FINAL" Folder
   -----------------------------------------   
   tell application "Finder"
      if exists (folder kcnfinfold of folder jobnum of disk "CLIENTS" of application "Finder") then
         set name of (folder kcnfinfold of folder jobnum of disk "CLIENTS" of application "Finder") to (kcnfinfold & ".old")
      end if
      make new folder in front Finder window with properties {name:kcnfinfold}
   end tell
end make_finfold

on do_collect()
   -----------------------------------------
   -- using system events, "collect" the opened quark file
   -----------------------------------------
   tell application "QuarkXPress"
      activate
      tell application "System Events"
         tell process "QuarkXPress"
            tell menu item "Collect for Output..." of menu 1 of menu bar item "File" of menu bar 1
               click
               delay 3
               keystroke "s" using {command down}
               delay 3
            end tell
            -----------------------------------------
            --   wait for Collect Window to load
            -----------------------------------------
            try
               with timeout of 5 seconds
                  repeat until window "Collect for Output" exists
                  end repeat
               end timeout
            end try
            -----------------------------------------
            -- browse to the correct "final" folder
            -----------------------------------------
            tell window "Collect for Output"
               keystroke "G" using {command down}
               delay 1
               tell text field 1 of sheet 1 -- of window "Collect for Output"
                  keystroke collectpath -- this is "typed in" to the Go To Window
               end tell
               tell sheet 1 -- of window "Collect for Output"
                  click button "Go"
               end tell
            end tell
            tell window "Collect for Output"
               click button "Save"
            end tell
            -----------------------------------------
            -- need this huge delay to allow for
            -- the collect to happen in "real time"
            -----------------------------------------
            repeat until not (exists (window "Currently Collecting"))
            end repeat
         end tell
      end tell
   end tell
end do_collect

on print_cropmarkPDF()
   -----------------------------------------
   -- print a "with crops" pdf, directly to the "Final" Folder
   -- slug line is NOT updated
   -----------------------------------------
   tell application "QuarkXPress"
      if (exists layer "Non-Printing") of layout space 1 of project 1 then tell layer "Non-Printing" of layout space 1 of project 1
         set properties to {suppress print:false, visible:true}
      end tell
      -----------------------------------------
      -- PDF is printed using "PROXY" profile if the job can be identified as such
      -- otherwise uses document default settings (this for tombstones etc)
      -----------------------------------------
      if kcn contains {"PC ", "NC ", "IC "} then
         export layout space 1 of project 1 in (":Volumes:CLIENTS:" & jobnum & ":" & kcnfinfold & ":" & kcn & "_(WCR).pdf") as "PDF" PDF output style "kcomm_WCR_9.5x14"
      else
         export layout space 1 of project 1 in (":Volumes:CLIENTS:" & jobnum & ":" & kcnfinfold & ":" & kcn & "_(WCR).pdf") as "PDF"
      end if
   end tell
end print_cropmarkPDF

on stuff_collect()
   -----------------------------------------
   -- checks for pre-existing ".sitx" folder
   -- renames "old" if exists
   -----------------------------------------
   tell application "Finder"
      if exists (file (kcnfinfold & ".sitx") of folder jobnum of disk "CLIENTS" of application "Finder") then
         set name of (file (kcnfinfold & ".sitx") of folder jobnum of disk "CLIENTS" of application "Finder") to (kcnfinfold & ".sitx" & ".old")
      end if
      if exists (file (kcnfinfold & ".sitx.old") of folder jobnum of disk "CLIENTS" of application "Finder") then
         set name of (file (kcnfinfold & ".sitx") of folder jobnum of disk "CLIENTS" of application "Finder") to (kcnfinfold & ".sitx.old" & ".old")
      end if
   end tell
   -----------------------------------------
   -- stuffs the current collect
   -- deletes the "final" folder it just stuffed.
   -----------------------------------------
   tell application "DropStuff"
      set x to a reference to collectpath
      set stufffin to stuff x
   end tell
end stuff_collect
-----------------------------------------
-- copies the current ".sitx" collect to the:
-- T:/Typesetting/DF King/Collect folder
-----------------------------------------
on move_2_Collect()
   set del_file to (":VOLUMES:CLIENTS:" & jobnum & ":" & kcnfinfold & ":")
   set this_file to (":VOLUMES:CLIENTS:" & jobnum & ":" & kcnfinfold & ".sitx")
   set Destination to (":VOLUMES:CLIENTS:Typesetting:COLLECT")
   do shell script "cp -p " & quoted form of POSIX path of this_file & space & quoted form of POSIX path of Destination
   -----------------------------------------
   -- deletes the "final" folder and leaves the compressed (stuffed) file in the job folder
   -----------------------------------------
   do shell script "mv " & quoted form of POSIX path of del_file & space & trash
   if (exists del_file) then
      delay 7
   end if
   --   repeat until not (exists del_file)
   -- end repeat
end move_2_Collect

Offline cdms

  • Full Member
  • ***
  • Posts: 111
  • Karma: 6
  • Gender: Male
Re: q8 as: collect using sys events trouble with save path
« Reply #7 on: June 17, 2009, 09:44:50 AM »
Hello again, I may be missing the point here but would you not just put the timeout on the open statement? (I'm not a big fan of potentially endless loops)
Unless you have your open in it's own try block you should not get past the open without the document being present.
If this isn't the answer then try something like;

tell application "QuarkXPress"
repeat with theCounter from 1 to (choose a safety value here)
 delay (pick a number here)
 try
    set theDoc to document 1
    exit repeat
  end try
end repeat
end tell

Set a delay and a safety value depending on how long you are prepared to wait for the document to open. Without something like this then corrupt documents, unsuppressed warnings etc will hang your script forever.

 

Offline 4610036

  • Jr. Member
  • **
  • Posts: 50
  • Karma: 0
  • Gender: Female
Re: q8 as: collect using sys events trouble with save path
« Reply #8 on: June 19, 2009, 11:32:37 AM »
hi cdms
thanks for the suggestion.
tell application "QuarkXPress"
activate
try
with timeout of 20 seconds
open multi_file with Suppress All Warnings
end timeout
end try


is this what you mean?  am i understanding the timeout code correctly?  i want to give it at least 20 seconds to open the document, but if it finishes opening before hand, it moves on to the next step, yes??  it seems to work. the problem is that the script works sometimes and not others. which would lead me to believe that it is a timing issue... maybe.  i will keep working on it and see if i cant get a reliable result.

thanks, as always, for all the help.

p.s.: i do not support potentially endless loops either... tho sometimes in applescript my brain slips into them quite easily! ;D

Offline larsen67

  • Sr. Member
  • ****
  • Posts: 459
  • Karma: 10
  • Gender: Male
Re: q8 as: collect using sys events trouble with save path
« Reply #9 on: June 19, 2009, 02:27:54 PM »
This is not needed by default AppleScript has an event timeout of 60 seconds and it will always wait for this before throwing an error. In some cases you can request a shorter time to create a test of say the existence of a dialog but in the majority of cases a timeout is only required when you think/expect some process may exceed this limit. I use printing to postscript and distilling in time out blocks just to be safe handling heavier files. If used as you have then you need to catch the files that don't open in time under 20 seconds with an on error statement. What 'cdms' was suggesting was using a different kind of loop statement that won't leave your script in a end less cycle if the file does not open. Length of delay x number of repeats then move on