Hello,
I’m new to Applescript and I have some questions.
What I’m trying to accomplish is monitor the contents of two folders, one local and one server folder. I want to add any files from the local folder that do not exist on the server folder and remove any files that exist on the server folder that do not exist on the local folder.
This will be put into an on idle handler to keep the two folders in sync.
I have written a script handler that goes through a folder and returns a list of aliases of all the files in that folder. I run this for both the local and the server folder.
on getFileList(currentFolder)
set iTemBucket to {}
tell application "Finder" to set folderAlias to every item of currentFolder
if not (folderAlias = {}) then
repeat with currentItem in folderAlias
set currentItemKind to kind of currentItem
if currentItemKind = "Folder" then
set iTemBucket to iTemBucket & getFileList(currentItem)
else
if currentItemKind = "MPEG-4 Audio File" or currentItemKind = "MP3 Audio File" or currentItemKind = "MPEG-4 Audio File (Protected)" then
set iTemBucket to iTemBucket & (currentItem as alias)
end if
end if
end repeat
end if
return iTemBucket
end getFileList
This returns a list of aliases of all the files in the folder: currentFolder
I then run a compare script that compares whether items in folder 1 are in folder 2 and should return a list of items that are not in folder 2.
on compareFolders(items1, items2)
set newItems to {}
tell application "Finder"
repeat with currentItem in items2
display dialog "Checking Item: " & currentItem as text
if currentItem is not in items1 then
set newItems to newItems & (currentItem as alias)
display dialog "Adding Item: " & currentItem as text
display dialog "Currently items are " & newItems as text
end if
end repeat
end tell
if (not (newItems = {})) then
display dialog "There are new Items: " & newItems as text
return newItems
else
display dialog "There are not new Items: " & newItems as text
return {}
end if
end compareFolders
I’ll then compare the two folders by:
set iTemsToCopy to compareFolders(serverItems, folderItems)
-- if there are iTemsToCopy then copy iTemsToCopy to server
set iTemsToDelete to compareFolders(folderItems, serverItems)
-- if there are iTemsToDelete then delete iTemsToDelete from server.
If I run my code when the server folder is empty, everything works fine. However, if I run the code a second time, the compare script still tells me there are items to copy, even though nothing has changed.
I think it is because the two lists of aliases have different paths and therefore are considered different items when I do the currentItem is not in items2 check. If so, how do I properly compare these two lists?
Thanks for reading…
-Michael