I need a folder action that duplicates each dropped file elsewhere. I tried to start one, but it doesn’t work at all. This seems like it might be something people have done before. I’m good at modifying finished scripts… does anyone have something I could get started with?
on adding folder items to this_folder after receiving added_Items
tell application "Finder" to duplicate added_Items to folder "disk:path:to:folder:"
end adding folder items to
on adding folder items to thisFolder after receiving addedItems
set thePath to path to desktop as alias & "Dropbox:"
if not pathExists(thePath) then
display dialog "Could not find the path. Files have not been copied."
return
end if
tell application "Finder"
try
repeat with anItem in addedItems
copy file anItem to folder thePath
end repeat
on error
display dialog "Could not copy files."
return
end try
end tell
end adding folder items to
on pathExists(thePath) -- pathExists("path:to:folder: OR :file")
try
get thePath as alias
return true
on error
return false
end try
end pathExists
set thePath to path to desktop as alias & "Dropbox:"
¢ apart from path to desktop is an alias anyway, the line results a list containing the alias and the string
copy file anItem to folder thePath
¢ anItem is an alias, the keyword file is not needed
¢ copy is the equivalent to set, the proper Finder command to copy files is duplicate
on adding folder items to thisFolder after receiving addedItems
set thePath to ((path to desktop as text) & "Dropbox:")
if not pathExists(thePath) then
display dialog "Could not find the path. Files have not been copied."
return
end if
tell application "Finder"
try
repeat with anItem in addedItems
duplicate anItem to folder thePath
end repeat
on error
display dialog "Could not copy files."
return
end try
end tell
end adding folder items to
on pathExists(thePath) -- pathExists("path:to:folder: OR :file")
try
get thePath as alias
return true
on error
return false
end try
end pathExists
Any other checks that could be nice to have in the script? I added a “make new folder” instead of the dialog that says the folder doesn’t exist (and moved the tell “Finder”, not sure if that’s right or not). Do I also need to do a check for duplicate file names within the folder, or will the Finder do this easily? I don’t want the “Stop” or “Replace” dialog to pop up. I want it to make a new file with a " copy 1", or " copy 2", etc. to show up at the end if the file already exists.
on adding folder items to thisFolder after receiving addedItems
set theFolder to "Dropbox"
set thePath to ((path to desktop as text) & theFolder & ":")
if not pathExists(thePath) then
try
tell application "System Events" to make new folder at end of (path to desktop) with properties {name:theFolder}
on error
display dialog "Files have not been copied. Could not find the path:" & return & return & thePath
return
end try
end if
try
repeat with anItem in addedItems
tell application "Finder" to duplicate anItem to folder thePath
end repeat
on error
display dialog "Could not copy files."
return
end try
end adding folder items to
on pathExists(thePath) -- pathExists("path:to:folder: OR :file")
try
get thePath as alias
return true
on error
return false
end try
end pathExists
I tried to do some renaming, but it got really complicated. If there is a simpler way to do it, please let me know. Appending “with replacing” to the duplicate should work if I set this system up a certain way, so I am going to use that for now:
on adding folder items to thisFolder after receiving addedItems
set theFolder to "Dropbox"
set thePath to ((path to desktop as text) & theFolder & ":")
if not pathExists(thePath) then
try
tell application "System Events" to make new folder at end of (path to desktop) with properties {name:theFolder}
on error
display dialog "Files have not been copied. Could not find the path:" & return & return & thePath
return
end try
end if
try
repeat with anItem in addedItems
tell application "Finder" to duplicate anItem to folder thePath with replacing
end repeat
on error
display dialog "Could not copy files."
return
end try
end adding folder items to
on pathExists(thePath) -- pathExists("path:to:folder: OR :file")
try
get thePath as alias
return true
on error
return false
end try
end pathExists
This should be similar or the same as the last post:
on adding folder items to thisFolder after receiving addedItems
set theFolder to "RIP Dropbox"
set thePath to ((path to desktop as text) & theFolder & ":")
if not pathExists(thePath) then
try
tell application "System Events" to make new folder at end of (path to desktop) with properties {name:(theFolder & " - Not Printed")}
on error
display dialog "Files have not been copied. Could not find the path:" & return & return & thePath
return
end try
end if
try
repeat with anItem in addedItems
tell application "Finder" to duplicate anItem to folder thePath with replacing
end repeat
on error
display dialog "Could not copy files."
return
end try
end adding folder items to
on pathExists(thePath) -- pathExists("path:to:folder: OR :file")
try
get thePath as alias
return true
on error
return false
end try
end pathExists
I’m having an issue though with large files. Even though the action says, “on receiving added items”, when the file is large (500MB is the one I was having trouble with), the script tried to duplicate before the file is done copying.
How would I tell the script to wait, say a minute before continuing?
Or would a “busy status” checker apply here?
on adding folder items to thisFolder after receiving addedItems
repeat with anItem in addedItems
repeat 20 times
try
open for access file anItem -- if this succeeds, the file is not busy.
close access result
exit repeat
on error -- file is busy
delay 3 -- times it out in 1 minute
end try
end repeat
-- do my action here
end repeat
end adding folder items to
The only problem is that the “open for access” always comes back false. Am I using “anItem” improperly?