Projectguru
Projectguru

Reputation: 21

AppleScript: execute script on selected folder

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

Upvotes: 0

Views: 470

Answers (1)

vadian
vadian

Reputation: 285069

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

Upvotes: 1

Related Questions