Below are two versions ignoring completely the old Finder.
They are verbose but they are efficient.
----------------------------------------------------------------
use AppleScript version "2.5"
use framework "Foundation"
use scripting additions
----------------------------------------------------------------
property |⌘| : a reference to current application
Germaine()
on Germaine()
-- define some constants
set ourSuffix to ".jpg" -- our standardized extension
set nameList to {"Muskmelon", "Horned melon", "Pomelo", "Cantaloupe", "Boysenberry", "Berry", "Grape", "Lime", "Horned melon", "Jujube", "Pear", "Banana", "Date palm", "Lemon", "Prune", "Cantaloupe", "Lychee", "Tangelo", "Jujube", "Lemon", "Cranberry"}
set skipsSubdirectoryDescendants to |⌘|'s NSDirectoryEnumerationSkipsSubdirectoryDescendants as integer --> 1
set skipsPackageDescendants to |⌘|'s NSDirectoryEnumerationSkipsPackageDescendants as integer --> 2
set skipsHiddenFiles to |⌘|'s NSDirectoryEnumerationSkipsHiddenFiles as integer --> 4
set theOptions to skipsSubdirectoryDescendants + skipsPackageDescendants + skipsHiddenFiles
set theFolder to choose folder -- default WITHOUT multiple selections
set fileManager to a reference to |⌘|'s NSFileManager's defaultManager()
set keysToRequest to {}
set rootURL to theFolder as «class furl»
set allURLs to (fileManager's enumeratorAtURL:rootURL includingPropertiesForKeys:keysToRequest options:theOptions errorHandler:(missing value))'s allObjects()
set rootURL to (|⌘|'s NSArray's arrayWithObject:theURL)'s firstObject()
-- extract the URLs whose extension is jpg or jpeg (ignoring case)
set theFormat to "(self.pathExtension ==[c] 'jpg') OR (self.pathExtension ==[c] 'jpeg')"
set thePredicate to |⌘|'s NSPredicate's predicateWithFormat:theFormat
set theArray to (allURLs's filteredArrayUsingPredicate:thePredicate)
-- build an array with the file names because, as far as I know, we can't sort NSURL objects
set theNames to |⌘|'s NSMutableArray's new()
repeat with aURL in theArray
(theNames's addObject:(aURL's lastPathComponent()))
end repeat
-- sort the array of names
set theNames to theNames's sortedArrayUsingSelector:"localizedStandardCompare:"
-- now, a true NSURL is required
set rootURL to (|⌘|'s NSArray's arrayWithObject:rootURL)'s firstObject()
set aCounter to 0
repeat with aName in theNames
set aCounter to aCounter + 1
if aCounter > (count nameList) then error "There is too many jpg files in the folder"
-- drops the original extension
set nameNoExt to aName's stringByDeletingPathExtension()
-- build a new name using our standardized extension
set newName to (nameNoExt as text) & "-" & item aCounter of nameList & ourSuffix
-- build the complete original URL
set oldURL to (rootURL's URLByAppendingPathComponent:aName)
-- build the complete new URL
set newURL to (rootURL's URLByAppendingPathComponent:newName)
-- rename the original
(fileManager's moveItemAtURL:oldURL toURL:newURL |error|:(reference))
end repeat
end Germaine
----------------------------------------------------------------
use AppleScript version "2.5"
use framework "Foundation"
use scripting additions
----------------------------------------------------------------
property |⌘| : a reference to current application
Germaine()
on Germaine()
-- define some constants
set ourSuffix to ".jpg" -- our standardized extension
set nameList to {"Muskmelon", "Horned melon", "Pomelo", "Cantaloupe", "Boysenberry", "Berry", "Grape", "Lime", "Horned melon", "Jujube", "Pear", "Banana", "Date palm", "Lemon", "Prune", "Cantaloupe", "Lychee", "Tangelo", "Jujube", "Lemon", "Cranberry"}
set skipsSubdirectoryDescendants to |⌘|'s NSDirectoryEnumerationSkipsSubdirectoryDescendants as integer --> 1
set skipsPackageDescendants to |⌘|'s NSDirectoryEnumerationSkipsPackageDescendants as integer --> 2
set skipsHiddenFiles to |⌘|'s NSDirectoryEnumerationSkipsHiddenFiles as integer --> 4
set theOptions to skipsSubdirectoryDescendants + skipsPackageDescendants + skipsHiddenFiles --> 7
set keysToRequest to {}
set fileManager to a reference to |⌘|'s NSFileManager's defaultManager()
set theFolder to choose folder -- default WITHOUT multiple selections
set rootURL to theFolder as «class furl»
set allURLs to (fileManager's enumeratorAtURL:rootURL includingPropertiesForKeys:keysToRequest options:theOptions errorHandler:(missing value))'s allObjects()
-- extract the URLs whose extension is jpg or jpeg (ignoring case)
set theFormat to "(self.pathExtension ==[c] 'jpg') OR (self.pathExtension ==[c] 'jpeg')"
set thePredicate to |⌘|'s NSPredicate's predicateWithFormat:theFormat
set ourFiles to (allURLs's filteredArrayUsingPredicate:thePredicate)
-- as far as I know, we can't sort NSURL objects
-- so replace every NSURL object by the pointed path
set ourFiles to |⌘|'s NSMutableArray's arrayWithArray:ourFiles
set knt to count ourFiles
repeat knt times
set aPath to ((ourFiles's item 1)'s |path|())
(ourFiles's removeObjectAtIndex:0) -- objectAtIndex:0 = item 1 of…
(ourFiles's addObject:aPath)
end repeat
-- Now the array contain paths which we may sort
set ourFiles to ourFiles's sortedArrayUsingSelector:"localizedStandardCompare:"
-- now, a true NSURL is required
set rootURL to (|⌘|'s NSArray's arrayWithObject:rootURL)'s firstObject()
set aCounter to 0
repeat with aPath in ourFiles
set aCounter to aCounter + 1
if aCounter > (count nameList) then error "There is too many jpg files in the folder"
-- drops the original extension
set nameNoExt to (aPath's lastPathComponent())'s stringByDeletingPathExtension()
-- build a new path using our standardized extension
set newName to (nameNoExt's stringByAppendingString:("-" & item aCounter of nameList & ourSuffix))
-- build the complete original URL
set oldURL to (|⌘|'s NSURL's fileURLWithPath:aPath)
-- build the complete new URL
set newURL to (rootURL's URLByAppendingPathComponent:newName)
-- rename the original
(fileManager's moveItemAtURL:oldURL toURL:newURL |error|:(reference))
end repeat
return true
end Germaine
I really don’t know which is the fastest one.
Is it deliberate that “Cantaloupe” and “Jujube” appear twice in the names list ?
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 24 février 2020 14:02:05
I tried an alternate format for thr predicate:
set theFormat to “(self.pathExtension IN[c] {‘jpg’, ‘jpeg’})”
but itsn’t ignoring case