0

I'm trying to modify an AppleScript "Copy folder tree" (by Jeff Fischer [email protected]). This handy script lets you duplicate a folder structure but without the folder contents (i.e., the resulting copy of the folder structure has all folders empty).

Currently, the script works by prompting you to navigate to the source folder who's structure you wish to copy.

WHAT I WANT: Instead of being prompted to navigate to the source folder, I'd like to be able to tell the script which is the source folder by right-clicking on it. Essentially foregoing the tedious navigation to it.

How can I modify this script to do this?

Here's the code:

---------------------------------------------------------------------------------------
-- Copy folder tree
-- by Jeff Fischer <[email protected]>
--
-- credit to Dan Decker and Robert Metzker for ideas from their scripts
---------------------------------------------------------------------------------------

-- make gFileTypes an empty set to omit copying/moving any files
property gFileTypes : {"psd", "jpg", "gif", "jpeg"}

tell application "Finder"
    set tSourceFolder to choose folder with prompt "Choose a folder structure to duplicate"
    if tSourceFolder is false then return "User Cancelled"
    set tDestFolder to choose folder with prompt "Choose the location for the duplicate structure"
    if tDestFolder is false then return "User Cancelled"
    
    my copyItems(tSourceFolder, tDestFolder)
end tell

on copyItems(pSourceFolder, pDestFolder)
    tell application "Finder"
        set tSourceFolderName to name of pSourceFolder as string
        make new folder at pDestFolder with properties {name:tSourceFolderName}
        set tNewDestFolder to (folder tSourceFolderName of folder pDestFolder) as alias
        
        -- copy or move over files of the specified type(s)
        set tSelectedFiles to (every file of folder pSourceFolder whose name extension is in gFileTypes)
        if tSelectedFiles ≠ {} then
            select (every file of folder pSourceFolder whose name extension is in gFileTypes)
            try
                -- uncomment the action you want, either copy or move
                -- copy the selection to folder tNewDestFolder
                move the selection to folder tNewDestFolder
                close window (name of folder pSourceFolder as string)
            end try
        end if
        
        -- copy over the folders, calling this handler recursively
        set tFolders to (every folder of pSourceFolder)
        repeat with tThisFolder in tFolders
            my copyItems(tThisFolder as alias, tNewDestFolder as alias)
        end repeat
    end tell
end copyItems

1 Answer 1

1

To be able to run the script by right-clicking on a folder you have to create an Automator Quick Action.

  • Launch Automator and select New Quick Action

  • Select In the Workflow receives current folders in Finder

  • Add a Run AppleScript action from the left side

  • Replace the code in the Run AppleScript action with

      ---------------------------------------------------------------------------------------
      -- Copy folder tree
      -- by Jeff Fischer <[email protected]>
      --
      -- credit to Dan Decker and Robert Metzker for ideas from their scripts
      ---------------------------------------------------------------------------------------
    
      -- make gFileTypes an empty set to omit copying/moving any files
      property gFileTypes : {"psd", "jpg", "gif", "jpeg"}
    
      on run {input, parameters}
          tell application "Finder"
              set tSourceFolder to item 1 of input
              set tDestFolder to choose folder with prompt "Choose the location for the duplicate structure"
              if tDestFolder is false then return "User Cancelled"
    
              my copyItems(tSourceFolder, tDestFolder)
          end tell
          return input
      end run
    
      on copyItems(pSourceFolder, pDestFolder)
          tell application "Finder"
              set tSourceFolderName to name of pSourceFolder as string
              make new folder at pDestFolder with properties {name:tSourceFolderName}
              set tNewDestFolder to (folder tSourceFolderName of folder pDestFolder) as alias
    
              -- copy or move over files of the specified type(s)
              set tSelectedFiles to (every file of folder pSourceFolder whose name extension is in gFileTypes)
              if tSelectedFiles ≠ {} then
                  select (every file of folder pSourceFolder whose name extension is in gFileTypes)
                  try
                      -- uncomment the action you want, either copy or move
                      -- copy the selection to folder tNewDestFolder
                      move the selection to folder tNewDestFolder
                      close window (name of folder pSourceFolder as string)
                  end try
              end if
    
              -- copy over the folders, calling this handler recursively
              set tFolders to (every folder of pSourceFolder)
              repeat with tThisFolder in tFolders
                  my copyItems(tThisFolder as alias, tNewDestFolder as alias)
              end repeat
          end tell
      end copyItems
    
  • Save the Quick Action

  • The action is available in the Services submenu of the contextual menu

Sign up to request clarification or add additional context in comments.

14 Comments

Thanks for your reply! Unfortunately, I don't understand the step: Replace the on run handler in the Run AppleScript action with the entire modified script
In Script Editor replace the code as suggested then copy (⌘C) the entire code and paste it (⌘V) in the code section of the Run Applescript action in Automator replacing the sample on run handler.
Thank you again but I still didn't understand. In any case, I think I'll have to give up. Cheers!
Perfect. Everything is clear and now works. Thanks!
Is it possible to modify this script so that it prompts me for a new name for the top level folder? Why do I need this? Because sometimes I need to duplicate a folder tree in the same directory. As it stands now if I try to save it to the same directory it throws an error. TIA!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.