How to combine “Choose folder command” with “adding folder items” function with Applescript

applescriptautomatorfolder-actionfolders

I'm using a folder action to create a "hot" folder, which immediately duplicates the files placed in this folder to a specific backup folder.

This is the script I'm using as folder action and it works fine:

on adding folder items to this_folder after receiving these_items
   tell application "Finder" to duplicate these_items to folder 
   ("Macintosh HD:Users:photograper:Desktop:Target:")
end adding folder items to

But I want to run this folder action like a normal applescript and trigger the "adding folder items" function with a "Choose folder" command. So I can manually point a specific source and target folder and get the script running.

I tried this, but no result:

set this_folder to choose folder with prompt "Select source folder:" 
with multiple selections allowed
set target_folder to choose folder with prompt "Select backup folder:"


on adding folder items to this_folder after receiving these_items
   tell application "Finder" to duplicate these_items to target_folder
end adding folder items to

Any suggestions how to combine the two parts of scripting together so they can interact?

Thanks!

Patrick

Best Answer

Just remove the handler and change the variable "these_items" for "this_folder", like so:

set this_folder to choose folder with prompt "Select source folder:" with multiple selections allowed
set target_folder to choose folder with prompt "Select backup folder:"

tell application "Finder" to duplicate this_folder to target_folder

Then run the script as a normal script, and it will copy the folder entirely to your destination.

If, on the other hand, you just want the items to be copied (not the folder), you could use this variation:

set this_folder to choose folder with prompt "Select source folder:" with multiple selections allowed
set target_folder to choose folder with prompt "Select backup folder:"

tell application "Finder"
    set these_items to list folder of this_folder without invisibles
    repeat with i from 1 to count of these_items
        set ItemX to item i of these_items
        set ItemX to (this_folder & ItemX as string) as alias
        duplicate ItemX to target_folder
    end repeat
end tell

Either way, you would end up with a duplicate of the contents of the folder.

Now, given your comments below, this version will run the script as a folder action, but will remember your destination folder when you declare a property at the beginning of the script. If you recompile the script, it will ask you again.

property target_folder : ""

on adding folder items to this_folder after receiving these_items
    if target_folder = "" then
        set target_folder to ¬
            choose folder with prompt "Select source folder:" with multiple selections allowed
    else
        try
            --if the script has a property that has been deleted (i.e., the destination folder), it will cause 
            --an error that resolves by asking for a new folder
            set target_folder to target_folder as alias
        on error
            set target_folder to ¬
                ¬
                    choose folder with prompt "Select source folder:" with multiple selections allowed
        end try
    end if
    tell application "Finder" to duplicate these_items to folder target_folder
end adding folder items to

I hope this helps.