AppleScript:在选定的文件夹上执行脚本

AppleScript: execute script on selected folder

我正在尝试修改 AppleScript“复制文件夹树”(作者 Jeff Fischer jeff@5fischers.org)。这个方便的脚本可让您复制文件夹结构但不复制文件夹内容(即,文件夹结构的结果副本的所有文件夹都是空的)。

目前,该脚本的工作原理是提示您导航到要复制其结构的源文件夹。

我想要什么: 我不想被提示导航到源文件夹,而是希望能够通过 右键单击​​ 来告诉脚本哪个是源文件夹。基本上放弃了繁琐的导航。

如何修改此脚本来执行此操作?

代码如下:

---------------------------------------------------------------------------------------
-- Copy folder tree
-- by Jeff Fischer <jeff@5fischers.org>
--
-- 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

要能够通过右键单击文件夹来运行 脚本,您必须创建一个 Automator 快速操作。

  • 启动 Automator 和 select 新的快速操作

  • Select Workflow 在 Finder[= 中接收当前 文件夹 39=]

  • 从左侧添加一个 运行 AppleScript 动作

  • 将 运行 AppleScript 操作中的代码替换为

      ---------------------------------------------------------------------------------------
      -- Copy folder tree
      -- by Jeff Fischer <jeff@5fischers.org>
      --
      -- 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
    
  • 保存快速操作

  • 该操作在上下文菜单的 Services 子菜单中可用