Skip to content

macos – Applescript: Copy/Duplicate only if size requirement is met

  • by

Assuming you aren’t planning on duplicating countless thousands of files, here is a simple script to duplicate files from one folder to another.

First, it generates a list of files in the source folder. It then cycles through each list item and if the file’s size is within the range, adds the file to a matching list. When that is complete, it duplicates the matching files to the destination folder.

tell application "Finder"
set srcFol to ((path to downloads folder as text) & "here:")
set dstFol to ((path to desktop as text) & "there:")

set everyFile to (get files of folder srcFol as alias list)
set sizedFiles to {}
repeat with aFile in (a reference to everyFile)
    set sz to size of aFile
    if sz is greater than 200000 and sz is less than 299000 then
        set end of sizedFiles to aFile
        set label index of aFile to 4 -- used to highlight matching files during testing
    end if
end repeat

duplicate sizedFiles to dstFol with replacing
end tell

NB the ‘set label index’ line simply puts a colour tag on each file. I use it during testing to highlight the files being duplicated. Comment it out once you confirm everything works. If you don’t wish existing files to be overwritten remove the with replacing.

If you plan on duplicating folders with lots and lots of files (or with subfolders) then you might want to consider do shell script along with something like ditto or rsync.

Leave a Reply

Your email address will not be published. Required fields are marked *