iChat...

Any chance the folders are created in sequence? I.e. is the create date of folder 2.0.1 always going to be greater than folder 1.9.8, etc?

Will the numbers always be single digits? If so, you could make a list of good named folders (testing the length of the file name and making sure it started with “folder” and ending with “#.#.#” to get rid of anything that didn’t conform), then you could sort the results and use the last one.

How about something like this. Seems a bit clunky but also seems to work… :wink:

(* Characters which are ok to match in your version numbers *)
property okCharacters : "1234567890."

(* A sample list of folder names *)
set queryList to {"abc", "Folder 4.9.2", "Folder 4.9.2a", "Folder 4.0.1", "Folder 4.2.8", "Folder 4.8.7", "Folder 4.0Beta", "flibr-djibit"}

set maxVersion to 0
repeat with tmpQuery in queryList
	if tmpQuery begins with "Folder " then
		set tmpVersion to (text items 8 through -1 of (tmpQuery as string)) as string
		if (validateString(tmpVersion) is true) and (tmpVersion > maxVersion) then
			set maxVersion to tmpVersion
		end if
	end if
end repeat
return (maxVersion as string)

(* A subroutine to check if all the characters in the version are ok *)
to validateString(tmpVersion)
	set tmpChars to (characters of tmpVersion) as list
	set charsOK to true
	
	repeat with tmpChar in tmpChars
		if okCharacters does not contain tmpChar then
			set charsOK to false
		end if
	end repeat
	
	return charsOK
end validateString

j

that’s an interesting approach jobu, works good too. thanks!

If your folders’ version numbers are definitely only groups of single digits, and only folders are so named, you might get away with this:

set mySubPath to (path to current user folder as Unicode text) & "Folder:Subfolder:"

do shell script "find " & (POSIX path of mySubPath) & " -name "Folder [0-9].[0-9].[0-9]""
set targetPath to (last paragraph of result) as POSIX file as Unicode text

tell application "Finder"
  set theTarget to folder targetPath
end tell

It appears to sort correctly. Another possibility, if it’s of any interest, is to use my “FinderSort()” handler, which is here in ScriptBuilders. This will correctly sort even folders with double-digit version strings such as “4.8.10”. It’s actually two quite large handlers, but you don’t need its text in your script. You can simply load it as a library. Then your script might be:

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

tell application "Finder"
  set mySub to folder "Folder:Subfolder:" of home
  
  -- Get a reference to the likeliest subfolders.
  set fRef to a reference to (folders of mySub whose name begins with "Folder " and ¬
    name extension is in {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15"})

  -- Sort the folders by name.
  set sortedFolders to FS's FinderSort(fRef, "name")
  
  -- Check through from the end of the sorted list.
  -- Accept the first folder whose name has no rogue characters.
  set OK to false
  considering case
    repeat with i from (count sortedFolders) to 1 by -1
      set theTarget to item i of sortedFolders
      set n to name of theTarget
      repeat with j from 8 to (count n)
        set OK to (character j of n is in OKchars)
        if (not OK) then exit repeat
      end repeat
      if (OK) then exit repeat
    end repeat
  end considering
  if (not OK) then error "No suitable folder was found."
  
  theTarget
end tell

Nigel, this is an awesome little utility. great job! i wish i could use it in this application but this will need to be run on thousands of machines from a CDROM so it’s not practical for that. but it’s awesome and i’m going to get a lot of use out of it on my own machine. thanks!

Another way is to “feel” your way towards the lastest version folder name using nested repeats to generate the numbers:

tell application "Finder"
  set parentFolder to folder "Folder:Subfolder:" of home
  set folderNames to name of folders of parentFolder whose name begins with "Folder "
  set latestName to my latestVersionFolderName(folderNames)
  set theTarget to folder latestName of parentFolder
end tell

on latestVersionFolderName(folderNames)
  set baseName to "Folder 4."
  considering case -- for faster string comparisons
    repeat with v2 from 9 to 0 by -1
      repeat with v3 from 9 to 0 by -1
        set thisName to baseName & v2 & "." & v3 -- automatic number-to-string coercions
        if (thisName is in folderNames) then return thisName
      end repeat
    end repeat
  end considering
end latestVersionFolderName

This will handle “double figures” if you make the repeats loop from 15 to 0 rather than from 9 to 0. You could also, if you liked, add an outer ‘v1’ loop in the handler to supply the “major” version number. You’d then have to adjust ‘basename’ and the ‘set thisName…’ line.