I am attempting to write a synchronizing script for two folders. I am doing it partly for the learning and the fun as I know of a few solutions that exist in the paid for and free to use at your own risk world. This is what I have so far
tell application "Finder"
(*Make list of all contents of the Folders*)
display dialog "Please choose the first folder"
set theFolder to (choose folder)
set TheList to the entire contents of the container theFolder
display dialog "Please choose the other folder"
set theOtherFolder to (choose folder)
set theOtherList to the entire contents of the container theOtherFolder
-- Explore the contents of TheOtherFolder as compaired to theFolder and sync contents
set theCount to the count of TheList
set theCount2 to the count of theOtherList
set P to (theCount - theCount2)
if P is less than 0 then
set theCount to theCount2
end if
repeat with x from 1 to theCount
set theWords to item x in TheList
set theOtherWords to item x in theOtherList
theWords = theOtherWords
if the result = "true" then
set theMod to the modification date of item x in TheList
set theOtherMod to the modification date of item x in theOtherList
theMod - theOtherMod
if the result is not equal to 0 then
if result is less than 0 then
duplicate item x in theOtherList to theFolder replacing 1
else
duplicate item x in TheList to theOtherFolder replacing 1
end if
end if
else
exists (item x in TheList)
if the result = "false" then
duplicate item x in TheList to theOtherFolder
else
duplicate item x in theOtherList to theFolder
end if
end if
end repeat
end tell
it’s clunky and I can’t even say it gets the job done. (The count feature makes the script stop once it exceeds the number of items in the list itemed folder)
What I’m stumped on is
A) Getting AppleScript to finding out if item x in theList EXISTS in theOtherList.
B) I also need to figure out how to get applescript to ask if Item I EXISTS at or in a certain location. For example
exists (item x in theList)
is suppose to read
exists (item x in theList) at theOtherFolder
But as you can see if you open that is an editor AppleScript cannot accept that syntax.
Any suggestions on
the struggles with the exist command
making it less clunky
Cheers
Marcus
Model: MacBook
AppleScript: 2.3
Browser: Safari 531.21.10
Operating System: Mac OS X (10.6)
your approach to compare the files in the same order can’t work.
If you add a file in folder theFolder the order might change in the way,
that the same item is now e.g. item 3 in theFolder and item 2 in theOtherFolder.
This syntax will never find something anyway because an object in two different locations is never the same object.
.
set theWords to item x in TheList
set theOtherWords to item x in theOtherList
theWords = theOtherWords
.
btw: the result of a comparison is a boolean value true or false, not a string “true” or “false”.
To check the existence of a file check if a file with a specified name exists at a specified path
for example
set folderA to choose folder
set folderB to choose folder
tell application "Finder"
set itemsA to items of folderA
repeat with oneItem in itemsA
set nameA to name of oneItem
if exists item ((folderB as text) & nameA) then
display dialog "item '" & nameA & "' exists at " & folderB as text
end if
end repeat
end tell
I’m with ya on that one. I knew that this was not a solution but rather a way of getting something to start with
And
Ya I had it written as true or false and it was missed until I switched the syntax to coerce it into a string. Then the if functions started working again.
Thanks for your solution. I will continue to play with it but it still leaves me wondering:
How does one check if text (or name or folder or whatever) exists in a variable list? And why does
set theWords to item x in TheList
exist theWords in theOtherList
--Set Folders
display dialog "Please select the folder that you want to syncronize"
set folderA to choose folder
display dialog "Please select the target folder that you want to syncronize to the first folder"
set folderB to choose folder
--Select Application
tell application "Finder"
(*If new items exist in FolderA then copy them to Folder B*)
set theItemsA to items of folderA
repeat with oneItem in theItemsA
set nameA to name of oneItem
set theModA to the modification date of oneItem
if not (exists item ((folderB as text) & nameA)) then
set x to (item ((folderA as text) & nameA))
duplicate x to folderB
(*If item in FolderA is more current than Folder B replace*)
else
set x to (item ((folderB as text) & nameA))
set theModB to the modification date of x
set y to (theModA - theModB)
if y is greater than 0 then
set z to (item ((folderA as text) & nameA))
duplicate z to folderB replacing 1
end if
end if
end repeat
end tell
display dialog "Your folders are now syncronized"
Totally works. Thanks so much. I have needed this program in the worst way. 'Preciate it!
I just feel that this problem is more easily solved with Unix utilities like rsync or ditto.
Maybe you want to do some more processing if some of the files are different or such.
I know this is in a different direction, but I would have done it that way if it wasn’t any intermediate actions or user interaction that requires applescript.
As a matter of fact it would be nice to have such a utility to synchronize a directory tree with another
automagically!