2

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

1

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.

  • Thanks for your reply, I wan't to use the folder action with the "adding folder items" statements. When a file is dropped on the folder I wan't it to duplicate to a destination folder immidiatly, That is what I wan't. I tried to put the "Choose folder" command for setting the dest. folder inside the 2 statements, but then every time a file is dropped it will trigger the "choose window". Is there a way to set the destination folder once? which is used by every file dropped on the source folder? Thanks! – Pat78 Jun 20 '18 at 19:44
  • this is what I tried: on adding folder items to this_folder after receiving these_items set target_folder to choose folder with prompt "Select backup folder:" tell application "Finder" to duplicate these_items to target_folder end adding folder items to – Pat78 Jun 20 '18 at 19:45
  • Sorry, I misunderstood when you said that you wanted to run "as a normal applescript". So if you want the choose folder window to appear only once, and from then on, use the same folder, you declare a property at the beginning of the script. See the edited email above. – MBUST Jun 21 '18 at 13:13
  • Yes! This is exactly what I meant and it works. I tried the "if - then" statement before but didn't know about the property at the beginning. Thank you very much! – Pat78 Jun 21 '18 at 16:10
  • is there a way that I can set the destination folder every time when use the script on another folder or when I delete the destination folder? Without recompiling the script? Right now, as you said, it is remembering the destination folder, even when I deleted the destination folder. Thanks! – Pat78 Jun 21 '18 at 19:50
0

@Patrick de Gier, this altered version of @MBUST 's code, allows the user to choose a destination folder every time

on adding folder items to this_folder after receiving these_items
    set target_folder to choose folder with prompt "SELECT TARGET FOLDER:" with multiple selections allowed
    tell application "Finder" to duplicate these_items to folder target_folder
end adding folder items to
  • I tried that one. But then the script the script will ask for a target folder every time a file is dropped in the folder. What I'm looking for is a variant that when I run the code for the first time, it will ask for choosing a target folder (Like MBUST's variant). But When the target folder is changing. For example when I use an external HD for back up one day and for another day I use another external HD as Back up (for photoshoots) I want the script to see that the target folder is missing and it will ask to set a new target folder. Don't know if it is possible. Thanks! – Pat78 Jun 22 '18 at 7:14
  • Sorry for the lack of clarity guys! really appreciate all your help – Pat78 Jun 22 '18 at 7:22
  • A variant of my last iteration should serve as the model to handle when either the destination or the target folder is missing. And when they are not missing, the script will use the stored property. So store each folder variable as a separate property at the beginning of the script, as I showed you in my script above. Use that as a model and you will get exactly what you want. – MBUST Jun 23 '18 at 7:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .