Apologies if this is posted in the wrong spot. I’m an utter newb, as will become clear by what I’m about to ask:
I have a watched folder script that is supposed to move files, one by one, from the folder to the desktop. However, I can’t seem to get it to work. For example, if I put 3 files in the folder, it moves 2 files out and leaves one behind. If I put 6 files in the folder, it might be move 3 or 4, and leave two others behind. There must be something wrong with the item_count, but for the life of me I cannot figure out what. Can anyone help?
BTW, I am open to any and all suggestions as to how I might have a watched folder move items sequentially (say one every 15 seconds) rather than all at once. Automator, 3rd party apps, what have you…
Thanks in advance for all of your help/suggestions
Here is the script:
on adding folder items to this_folder after receiving added_items
tell application "Finder"
set item_count to count items in the container this_folder
end tell
if item_count is not equal to 0 then
set k to 1
repeat while k is less than (item_count + 1)
delay 5
tell application "Finder"
set temp to item k of the container this_folder
set next_file to temp as string
tell application "Finder"
move next_file to desktop
end tell
set k to k + 1
end tell
end repeat
end if
end adding folder items to
this is a common mistake. You count the items at the beginning of the script
but you don’t adjust the number after having moved a file.
For example you have 6 files. After three loops there are only 3 files left but the loop wants to move item 4 and there is no item 4. In case of an error the event handler aborts silently.
Anyway your script is quite complicated, this is sufficient
on adding folder items to this_folder after receiving added_items
tell application "Finder" to move added_items to desktop
end adding folder items to
yes, that does work, but it moves all of the files all at once, rather than one at a time. So I just need to figure out a way to get the loop count to match the number of files that are actually in the folder, in order to prevent the error you described…hmmmm
on adding folder items to this_folder after receiving added_items
repeat with oneItem in (get added_items)
tell application "Finder" to move oneItem to desktop
delay 5
end repeat
end adding folder items to
is it possible that I just have a bad preference somewhere that is causing my Scripted events to screw up? It seems like that script you sent should definitely have worked.
on adding folder items to this_folder after receiving added_items
tell application "Finder"
repeat while (count this_folder) > 0
move item 1 of this_folder to desktop
delay 5
end repeat
end tell
end adding folder items to
I fiddled with the code ideas you gave me, and I’ve got a solution that works well enough for what I need. I can put up to 5 files in the folder, and it will delay 3 seconds between each move. That should give me enough breathing room for the process(es) I have in mind. Thanks a ton for your help!
Cheers
Here is the code:
on adding folder items to this_folder after receiving added_items
tell application "Finder"
set item_count to count items in the container this_folder
repeat while item_count > 0
delay 2
move item 1 of this_folder to desktop
delay 1
end repeat
end tell
end adding folder items to
This might work, but the script ends with an error, because again you don’t count the items properly after an iteration of the loop.
Use this instead, it includes your delays but counts the items properly
on adding folder items to this_folder after receiving added_items
tell application "Finder"
repeat while (count this_folder) > 0
delay 2
move item 1 of this_folder to desktop
delay 1
end repeat
end tell
end adding folder items to
Note: container in Finder dictionary is only used to specify the parent folder of an item
and count folder does exactly the same as count items of folder