As you are using 10.11.6 you may use ASObjC.
The code is long because I inserted in it handlers which in my real life are in libraries.
# http://http://macscripter.net/post.php?tid=45689
#
# 2017/05/03 -- enhanced the code building the list of names and the list of duplicate files
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
on run
(*
# Don't worry, Macintosh HD is not my boot disk
set largeFolder to POSIX path of ("Macintosh HD:Users:Important:pour ebay:")
set smallFolder to POSIX path of ((path to desktop as text) & "_X28737Æ’YK:")
*)
set largeFolder to POSIX path of (choose folder with prompt "Choose the folder containing numerous files !")
set smallFolder to POSIX path of (choose folder with prompt "Choose the folder containing few files !")
my compare(largeFolder, smallFolder)
end run
on compare(largeFolder, smallFolder)
local iconp2d, name, storageName, storageFolder, localisedLabels, theLabel, largeDirectoryContents, smallDirectoryContents, namesInSmall, anURL, itsName
set iconName to "Icon" & return
set p2d to path to desktop
set storageName to "duplicates_setacilpud"
# Create the folder to store duplicates.
set storageFolder to my createFolderNamed:storageName inFolder:(POSIX path of p2d)
# We must pass a localized label name so we grab them.
set localisedLabels to getLabelsNames()
--> {"Aucun", "Gris", "Vert", "Violet", "Bleu", "Jaune", "Rouge", "Orange"
# We will use red, why not ?
set theLabel to localisedLabels's item 7
# Grab the entire contents of the main folder
set largeDirectoryContents to my getEntireContents:largeFolder
# Grab the entire contents of the small folder
set smallDirectoryContents to my getEntireContents:smallFolder
# Build an array containing the names of files of small folder
set namesInSmall to current application's NSMutableArray's arrayWithArray:{}
repeat with anURL in smallDirectoryContents
set itsName to anURL's |lastPathComponent|()
(namesInSmall's insertObject:itsName atIndex:(count namesInSmall))
end repeat
set thePredicate to current application's class "NSPredicate"'s predicateWithFormat:("lastPathComponent IN %@") argumentArray:({namesInSmall})
# Build a list of files of large folder whose name is available in the small one
set theDuplicates to (largeDirectoryContents's filteredArrayUsingPredicate:(thePredicate))
# Main loop
repeat with anURL in theDuplicates
--set anURL to contents of anURL
# If the file is a duplicate, apply the wanted treatment.
set itsName to (anURL's |lastPathComponent|()) as text
if (itsName does not start with ".") and itsName is not iconName then
# May set a red label on duplicates
--(my setTags:{theLabel} forItem:(anURL's |path|() as text))
# May copy duplicates into the storage folder. I do that because I don't wish to modify my largeFolder
(my copyThis:(anURL's |path|() as text) intoFolder:storageFolder)
# Move duplicates into the storage folder. I guess that it's the one you will use.
--(my movePath:(anURL's |path|() as text) toFolder:storageFolder)
end if
end repeat
end compare
#=====#=====#=====#=====#=====#=====
# Some ASObjC handlers borrowed to Shane STANLEY
#=====#=====#=====#=====#=====#=====
on getEntireContents:POSIXPath
set |⌘| to current application
-- Set up an NSFileManager enumerator and get the folder's "entire contents" (visible files and folders) as NSURLs.
set theFileManager to |⌘|'s class "NSFileManager"'s defaultManager()
set rootURL to |⌘|'s class "NSURL"'s fileURLWithPath:(POSIXPath)
set dirAndPackageKeys to |⌘|'s class "NSArray"'s arrayWithArray:({|⌘|'s NSURLIsDirectoryKey, |⌘|'s NSURLIsPackageKey})
set theEnumerator to theFileManager's enumeratorAtURL:(rootURL) includingPropertiesForKeys:(dirAndPackageKeys) options:((|⌘|'s NSDirectoryEnumerationSkipsHiddenFiles) + (|⌘|'s NSDirectoryEnumerationSkipsPackageDescendants as integer)) errorHandler:(missing value)
set entireContents to theEnumerator's allObjects()
return entireContents
end getEntireContents:
#=====#=====#=====#=====#=====#=====
-- Creates a new folder. There is no error if the folder already exists, and it will also create intermediate folders if required
on createFolderNamed:proposedName inFolder:POSIXPath # appelé par une instruction
local |⌘|, theFolderURL, theDestURL, theFileManager
set |⌘| to current application
set theFolderURL to |⌘|'s |NSURL|'s fileURLWithPath:POSIXPath
if class of proposedName is text then set proposedName to |⌘|'s NSString's stringWithString:proposedName
set proposedName to proposedName's stringByReplacingOccurrencesOfString:"/" withString:":"
set theDestURL to theFolderURL's URLByAppendingPathComponent:proposedName
set theFileManager to |⌘|'s NSFileManager's |defaultManager|()
-- set {theResult, theError} to theFileManager's createDirectoryAtURL:theDestURL withIntermediateDirectories:true attributes:(missing value) |error|:(reference)
theFileManager's createDirectoryAtURL:theDestURL withIntermediateDirectories:true attributes:(missing value) |error|:(missing value)
-- if not (theResult as boolean) then error (theError's |localizedDescription|() as text)
return theDestURL's |path| as text
end createFolderNamed:inFolder:
#=====#=====#=====#=====#=====#=====
on movePath:posixSource toFolder:posixDestination
local |⌘|, theSourceURL, destURL, shortURL, origName, theFileManager, theResult, theError, destName
set |⌘| to current application
set theSourceURL to |⌘|'s |NSURL|'s fileURLWithPath:posixSource
set destURL to |⌘|'s |NSURL|'s fileURLWithPath:posixDestination
set theFileManager to |⌘|'s NSFileManager's |defaultManager|()
set {theResult, theError} to theFileManager's createDirectoryAtURL:destURL withIntermediateDirectories:true attributes:(missing value) |error|:(reference)
--if not (theResult as boolean) then error (theError's |localizedDescription|() as text)
# maintenant, move cheminPosixDuFichierSource item
set destName to theSourceURL's |lastPathComponent|()
set destURL to destURL's URLByAppendingPathComponent:destName
my moveFromURL:theSourceURL toURL:destURL withReplacing:true
return destURL's |path| as text
end movePath:toFolder:
#=====
-- This handler is called by other handlers, and is not meant to called directly
on moveFromURL:sourceURL toURL:destinationURL withReplacing:replaceFlag
set |⌘| to current application
set theFileManager to |⌘|'s NSFileManager's |defaultManager|()
set {theResult, theError} to (theFileManager's moveItemAtURL:sourceURL toURL:destinationURL |error|:(reference))
if not theResult as boolean then
if replaceFlag and (theError's code() = |⌘|'s NSFileWriteFileExistsError) then -- it already exists, so try replacing
-- replace existing file with temp file atomically, then delete temp directory
set {theResult, theError} to theFileManager's replaceItemAtURL:destinationURL withItemAtURL:sourceURL backupItemName:(missing value) options:(|⌘|'s NSFileManagerItemReplacementUsingNewMetadataOnly) resultingItemURL:(missing value) |error|:(reference)
-- if replacement failed, return error
if not theResult as boolean then error (theError's |localizedDescription|() as text)
else -- replaceFlag is false or an error other than file already exists, so return error
error (theError's |localizedDescription|() as text)
end if
end if
end moveFromURL:toURL:withReplacing:
#=====#=====#=====#=====#=====#=====
on copyThis:POSIXPath intoFolder:folderOrPath
local |⌘|, theSourceURL, destName, theFolderURL, theDestURL
set |⌘| to current application
set theSourceURL to |⌘|'s |NSURL|'s fileURLWithPath:POSIXPath
set destName to theSourceURL's |lastPathComponent|()
set theFolderURL to |⌘|'s |NSURL|'s fileURLWithPath:folderOrPath
set theDestURL to theFolderURL's URLByAppendingPathComponent:destName
my copyFromURL:theSourceURL toURL:theDestURL withReplacing:true
end copyThis:intoFolder:
#=====
-- This handler is called by other handlers, and is not meant to called directly
on copyFromURL:sourceURL toURL:destinationURL withReplacing:replaceFlag
set |⌘| to current application
set theFileManager to |⌘|'s NSFileManager's |defaultManager|()
set {theResult, theError} to (theFileManager's copyItemAtURL:sourceURL toURL:destinationURL |error|:(reference))
if not theResult as boolean then
if replaceFlag and (theError's code() as integer = |⌘|'s NSFileWriteFileExistsError as integer) then -- it already exists, so try replacing
-- create suitable temporary directory in destinationURL's parent folder
set {tempFolderURL, theError} to theFileManager's URLForDirectory:(|⌘|'s NSItemReplacementDirectory) inDomain:(|⌘|'s NSUserDomainMask) appropriateForURL:(destinationURL's |URLByDeletingLastPathComponent|()) create:true |error|:(reference)
if tempFolderURL = missing value then error (theError's |localizedDescription|() as text) -- failed, so return error
-- copy sourceURL to temp directory
set tempDestURL to tempFolderURL's URLByAppendingPathComponent:(destinationURL's |lastPathComponent|())
set {theResult, theError} to theFileManager's copyItemAtURL:sourceURL toURL:tempDestURL |error|:(reference)
if not theResult as boolean then
-- copy failed, so delete temporary directory and return error
theFileManager's removeItemAtURL:tempFolderURL |error|:(missing value)
error (theError's |localizedDescription|() as text)
end if
-- replace existing file with temp file atomically, then delete temp directory
set {theResult, theError} to theFileManager's replaceItemAtURL:destinationURL withItemAtURL:tempDestURL backupItemName:(missing value) options:(|⌘|'s NSFileManagerItemReplacementUsingNewMetadataOnly) resultingItemURL:(missing value) |error|:(reference)
theFileManager's removeItemAtURL:tempFolderURL |error|:(missing value)
-- if replacement failed, return error
if not theResult as boolean then error (theError's |localizedDescription|() as text)
else -- replaceFlag is false or an error other than file already exists, so return error
error (theError's |localizedDescription|() as text)
end if
end if
end copyFromURL:toURL:withReplacing:
#=====#=====#=====#=====#=====#=====
on setTags:tagList forItem:fileOrPOSIXPath
local |⌘|, thisURL, theResult, theError
set |⌘| to current application
if class of fileOrPOSIXPath is not text then set fileOrPOSIXPath to POSIX path of fileOrPOSIXPath
set thisURL to |⌘|'s class "NSURL"'s fileURLWithPath:fileOrPOSIXPath -- make URL
set {theResult, theError} to thisURL's setResourceValue:tagList forKey:(|⌘|'s NSURLTagNamesKey) |error|:(reference)
if theResult as boolean is false then error (theError's |localizedDescription|() as text)
end setTags:forItem:
#=====#=====#=====#=====#=====#=====
on getLabelsNames()
return (current application's NSWorkspace's sharedWorkspace()'s fileLabels()) as list
--> {"Aucun", "Gris", "Vert", "Violet", "Bleu", "Jaune", "Rouge", "Orange"}
end getLabelsNames
#=====#=====#=====#=====#=====#=====
Yvan KOENIG running Sierra 10.12.4 in French (VALLAURIS, France) mardi 2 mai 2017 21:59:29