r/applescript • u/Perfect-Extent9215 • Sep 21 '22
Need help with a script to copy the delta between two folders to a third location
Hi, I have 2 external drives that each contain a folder containing a bunch of other folders. The second drive is a curated subset copy of the folders in the first drive, approximately 900 or so out of the 1200 in the first drive. I want to copy the remaining 300 into a different folder on the second drive, so that it can also act as a backup for the first drive, without affecting the curation I did putting the original 900 together.
I put this script together to show what I want to achieve, but I assume it's inefficient and would like to know if there's a better way to achieve what this script is doing:
tell application "Finder"
set originalFolder to (choose folder with prompt "Choose Original folder")
set subsetFolder to (choose folder with prompt "Choose Subset folder")
set outputFolder to (choose folder with prompt "Choose Output folder")
repeat with eachOriginal in (get every folder in originalFolder)
set originalName to name of eachOriginal
set found to false
repeat with eachSubset in (get every folder in subsetFolder)
set subsetName to name of eachSubset
if subsetName is originalName then
set found to true
exit repeat
end if
end repeat
if found is false then
copy eachOriginal to folder outputFolder
end if
end repeat
end tellit feels wrong having to do the nested repeat, and I was hoping for something more akin to "if originalName not in subsetFolder then" but I haven't been able to find anything along those lines in my google searches to use as a starting point.
Any recommendations?
Edit: P.S. No, I haven't tried running the script yet, as I need to retrieve the second drive from the RV first, and the RV is in storage. Just trying to prep now for when I do retrieve the drive.
2
u/Perfect-Extent9215 Sep 22 '22
Ok, thanks everyone. So it seems that the answer to my original question of just eliminating the nested repeat would be to change my script to this:
But, I'm gathering from ChristoferK's posts, that is still not the most efficient method, and he has made suggestions on alternative methods to achieve what I'm trying to do. I've only done one script prior to this so I'm still fairly new and have a lot to learn. It's going to take me a little bit to deep-dive into ChristoferK's methods and learn what they're doing.
Thanks for the help everyone, I'll let you know how it goes once I settle on the method I'll use.
4
u/ChristoferK Sep 21 '22 edited Sep 21 '22
Method 1 : Filtering with whose and Finder
In theory, Finder permits the use of pre-existing lists of items to form part of the predicate used to filter objects in a
whoseclause, which no other application does. So you could do this:In the above,
Awould be the folder containing the original 1200 folders, andBwould be the folder housing the curated subset of 900 folders. This makesXthe list of folder names in B, which will form a subset of the folder names in A.Lrepresents the difference between setsAandB, returning the folders inAthat don't share their names with any of those inB.If you run it as it is, it won't operate on them yet, and simply return the list of folder names for the 300 folders that
Lreferences. This is so you can see whether the filtering has been accurate.This is, of course, if Finder doesn't time out in the process, which is very likely, because Finder is shit and should not be used for file operations unless it's necessary.
Method 2 : Iterating, but sensibly with System Events
System Events is usually better for file system stuff, but like most applications, it won't let you filter against
X. However, the iterative method would almost certainly be more performant if a single repeat loop operatea in a way that minimises AppleScript's workload:System Events, especially since Monterey, has been troublesome with file system stuff, for example, refusing to copy items when it used to be happy with this. The
movecommand would likely work perfectly fine, asLwill contain a list of file system objects. However, if you wanted to copy rather than move the items, it's still better using System Events to collate the list of folders, but we'd make sure that the items inLwere each coerced to the universal class ofalias, after which the copying can be handled by Finder (the command is calledduplicate, notcopy).Method 3 : Eliminating strings using delimiters
But, another method might be the most performant in some situations, by merely performing string comparisons:
Here, we're using text item delimiters to eliminate any paths in list
A(the original list of 1200) that appear in listB(the curated list of 900). It's a fairly tall order to use a list of delimiters consisting of 300 items, but all of these methods are, in one way or another, a lot to ask from AppleScript.Lwill contain a list of absolute HFS paths that can be operated on using Finder or System Events. At one time, they both used to accept lists of plain text paths that could be used withmoveand other similar commands. Finder I think still allows this, but I can confirm the best route to use later when I'm at a computer.For now, I'd recommend seeing what the return results of all of the above are. If anything throws an error besides a timeout, don't worry too much, as it'll be the result of a typo from me writing this out on my mobile phone. I'll make corrective edits in a few hours but this gives you something to mess around with in the meantime.
2
u/copperdomebodha Sep 21 '22
Simple method. No optimization.