问题
I am working on a script that duplicates folders from an afp share to a local folder and does all kind of things with those folders afterwards. (it deletes the original folders after duplicating)
This code is working fine:
tell application "Finder"
duplicate every folder of folder afpFolder to localFolder with replacing
delete every folder of folder afpFolder
end tell
My problem is that our employees will be adding new folders to afpFolder
regulary and quite often. My script is running every 10 seconds (with LaunchAgents) because it needs to process the duplicated data as often as possible.
The question I have is: What happens when the script is duplicating and deleting folders and in the exact same moment somebody adds a new folder to afpFolder
?
Does the script only delete what was in afpFolder
in the moment it started running or could it be that it would delete one of those newly created folders without duplicating it?
I have also thought about making something with a list. For example:
set folderList to {}
tell application "Finder" to set folderList to every folder of afpFolder
duplicate folderList to localFolder
delete folderList
(this might not work this way)
Can anyone please help me answer the question?
Can I work with the upper code where I just duplicate and delete? Or do I have to worry about the script deleting folders that are created in the moment the script runs, without duplicating them?
If the upper code can cause trouble, can you help me with the list solution?
回答1:
The first script would delete items that are added between the duplicate and delete commands. But I can't think of any case where the second approach would not work:
tell application "Finder"
set l to items of ((POSIX file "/private/tmp/test") as alias)
duplicate l to desktop
delete l
end tell
You could also try using mv or rsync:
do shell script "mv /path/to/afp_folder/* /path/to/local_folder/"
do shell script "rsync -a /path/to/afp_folder/ /path/to/local_folder/
rm -r /path/to/afp_folder/*"
回答2:
The code from your answer @John Alarik is inefficient and I think in some spots it won't even work. Please don't take that as a criticism because I can see you're trying hard to find a solution which is the right way to go about getting help. So I thought I'd help with your code.
First you seem to be doing many unnecessary conversions of strings, alias's, and file specifications. In addition some of them are incorrect in the context you are using them and will probably cause errors. Your repeat statement in overly complicated too.
Try this code. It works in the same manner as you want but is much cleaner and shouldn't have any errors. I hope it helps. Good luck.
set afpFolder to "examplepath:path:folder:" as alias
set localFolder to "examplepath:path:folder:" as alias
tell application "Finder"
set folderList to folders of afpFolder
repeat with aFolder in folderList
duplicate aFolder to localFolder with replacing
delete aFolder
end repeat
end tell
回答3:
Don't make it more complicated than it needs to be.
Both Duplicate and Delete can accept a list as a direct parameter. There is no need for any repeat loops. Please note that this is the same format that Lauri suggested, just formatted with the OP's example. Give her the upvote.
set afpFolder to "examplepath:path:folder:" as alias
set localFolder to "examplepath:path:folder:" as alias
tell application "Finder"
set folderList to folders of afpFolder
duplicate folderList to localFolder
delete folderList
end tell
回答4:
Update: See @regulus6633's post about this code!
Thanks for all the help. In the meantime I could solve my problem even without your answers due to a good code snippet I found for making and processing a list.
This is the final solution, which works fine even if you copy a new folder to afpFolder while the script is running.
tell application "Finder"
set afpFolder to "examplepath:path:folder:" as alias
set localFolder to "examplepath:path:folder:" as string
set folderList to every folder of folder afpFolder
set i to 1
repeat the count of folderList times
set afpUniqueFolder to (item i of folderList) as string
duplicate afpUniqueFolder to localFolder
delete afpUniqueFolder
set i to i + 1
end repeat
end tell
I know that there is also a command called move
instead of duplicate
and delete
.
We had some strange issues with move
before, so since then I stick to duplicate
and delete
.
Thanks to all! This website is very helpful!
来源:https://stackoverflow.com/questions/17094205/is-applescript-processing-new-folders-that-are-created-in-the-moment-the-script