Help Sorting lists - again

Dear All,

I know this has been covered before and I think I have read all the threads on the subject, but I am still getting error messages when I try and use some of the various sort routines I have “stolen” from this site

My script is for Finale 2004c (which has no Applescript support) it is as below.

I have two problems;

  1. The files seems to print in a completely random order, ideally they would print in the order they are in the Finder Window or even better by being sorted against a custom sort list (probably out of my “comfort zone”). I have tried but got various error messages, my guess is a “coercion” problem?

  2. Is there a way using UI scripting to know when an application has printed a document and is “idle”, at the moment I have to add a delay at the end of the routine. This is fine except if the document takes longer to print than the delay, the routine print one document over and over (very bad!)

As I am sure you can tell, this is all new to me, so any help or pointers to more information would be gratefully received.

Regards

David Hage
Dakota Music Ltd

– start Finale 2004c if it is not already running
tell application “Finale 2004c”
activate
end tell

–tell finder to set selected file(s) droppped on it to variable MyFileList
tell application “Finder”
set MyFileList to the selection
end tell

– this is the start of the repeat for the printing of each file
–open print dialog and press print for selected files
repeat with MyFile in MyFileList
open MyFile – this’ll tell finder to open in the default app for the file type
tell application “System Events”

–bring Finale 2004c to front
set frontmost of process “Finale 2004c” to true
tell process “Finale 2004c”

– this section opens page setup and chooses a paper size

–open page setup
click menu item “Page Setup…” of menu “File” of menu bar item “File” of menu bar 1
delay 1
click pop up button 2 of UI element 1 of UI element 4 of window “Page Setup”
– this selects the Kyocera as the “Format for” printer to prevent losing bottoms of pages
delay 0.2
keystroke “Kyo”
keystroke return
delay 0.5
–click A4 and press return
click pop up button 1 of UI element 1 of UI element 4 of window “Page Setup”
delay 0.2
–choose paper size here
keystroke “A4”
delay 0.2
keystroke return
–click portrait button is radio button 3
click radio button 3 of radio group 1 of UI element 1 of UI element 4 of window “Page Setup”
–close page setup
keystroke return

end tell
end tell
delay 0.5

–This section brings up print dialogue and selects the Finale 2004c settings
tell application “System Events”
set frontmost of process “Finale 2004c” to true
tell process “Finale 2004c”
keystroke “p” using {command down}
delay 1.5
–bring up Finale 2004c print menu
click pop up button 1 of window “Print”
–Select Finale 2004c settings
keystroke “Fin”
keystroke return
delay 0.5
– this clicks the one-up button
click radio button “1-up” of UI element 1 of UI element 3 of window “Print”

– print the file
keystroke return
end tell
delay 7
end tell
– this ends the repeat section
end repeat
end open

Hi, David.

The reason I asked for details of the Finder window when you posted this query on the Mac OS forum yesterday was to help decide what to push at you. :wink:

I have a handler here in ScriptBuilders that returns a list of Finder references to files and/or folders sorted on a given property such as “name”, “modification date”, “label”, etc. — much as the ‘sort’ command used to do in pre-X versions of the Finder. The properties on which the items can be sorted are the same as those by which items can be “arranged” in Finder windows, so this could be a solution if your window is arranged in one of these ways.

Alternatively, I can post a handler here that lists items according to their position in an icon-view window.

Both handlers require a Finder reference to the items for their input. Your script uses the Finder’s ‘selection’, which unfortunately no longer acts as such a reference in Mac OS X. (ie. You can’t use an expression like ‘name of selection’ to get the names of the selected items.) But you can set up an alternative reference to the selected items using the handler in the example below.

If you’re using my routine from ScriptBuilders, it’s probably best to load the file into your own script as a library, as it contains two rather large handlers. Thus to sort selected items sorted by name:

set lib to load script file ((path to scripts folder as Unicode text) & "Libraries:FinderSort().scpt") -- your own path here.

on FinderSelectionRef()
  tell application "Finder"
    set s to selection
    repeat with i from 1 to (count s)
      set item i of s to name of item i of s
    end repeat
		
    return a reference to (items of target of front window whose name is in s)
  end tell
end FinderSelectionRef

set sortedFiles to lib's FinderSort(FinderSelectionRef(), "name")
--> A list of the selected Finder items, sorted by name.

Let me know if the ‘sort by position’ alternative is of any interest.

Hi Nigel,

Huge thanks for the pointers, not sure I quite understand it all, but I will push on and yell if I get stuck. One thing, is there a “standard” place in OSX to store your script.

Regards

Dave Hage
Dakota Music Ltd

The standard place to store scripts is somewhere within the Scripts folder in the user’s Library folder. An alias to this is returned by the command ‘path to scripts folder’. (There’s also a Scripts folder in the Library folder at the root level of the startup disk, but this isn’t freely add-to-able.) If you were talking about the storage for my script in particular, it doesn’t really matter — so long as you know where is when you write yours. :wink: