【问题标题】:Create an Automator folder action to automatically create a directory based on part of a filename, then move to a new directory on the server创建 Automator 文件夹操作以根据文件名的一部分自动创建目录,然后移动到服务器上的新目录
【发布时间】:2019-08-13 00:40:13
【问题描述】:

我需要创建一个应用程序或文件夹操作,用户可以在其中放置文件。该文件夹将:

  1. 检查每个文件名的前 13 位数字并使用这 13 位数字创建一个新目录
  2. 将前 13 位相同的所有文件移至相关文件夹中
  3. 将整个文件夹移动到服务器上的预设目录

JPG 名称将是:

  • 00319-BB01-01-C1 HighResDig.jpg 00319-BB01-01-C1 HighResPrint.jpg
  • 00319-BB01-01-C1 LowResDig.jpg 00319-BB01-01-C1 AI.jpg
  • 00319-BB01-01-C1 目录.jpg 00319-BB01-01-C1 Web.jpg
  • 00319-BB01-01-S1 HighResDig.jpg 00319-BB01-01-S1 HighResPrint.jpg
  • 00319-BB01-01-S1 LowResDig.jpg 00319-BB01-01-S1 AI.jpg
  • 00319-BB01-01-S1 目录.jpg 00319-BB01-01-S1 Web.jpg
  • 00319-BB01-01-S2 HighResDig.jpg 00319-BB01-01-S2 HighResPrint.jpg
  • 00319-BB01-01-S2 LowResDig.jpg 00319-BB01-01-S2 AI.jpg
  • 00319-BB01-01-S2 目录.jpg 00319-BB01-01-S2 Web.jpg
  • 00320-BB01-01-C1 HighResDig.jpg 00320-BB01-01-C1 HighResPrint.jpg
  • 00320-BB01-01-C1 LowResDig.jpg 00320-BB01-01-C1 AI.jpg
  • 00320-BB01-01-C1 Catalogue.jpg 00320-BB01-01-C1 Web.jpg
  • 00320-BB01-01-S1 HighResDig.jpg 00320-BB01-01-S1 HighResPrint.jpg
  • 00320-BB01-01-S1 LowResDig.jpg 00320-BB01-01-S1 AI.jpg
  • 00320-BB01-01-S1 目录.jpg 00320-BB01-01-S1 Web.jpg
  • 00320-BB01-01-S2 HighResDig.jpg 00320-BB01-01-S2 HighResPrint.jpg
  • 00320-BB01-01-S2 LowResDig.jpg 00320-BB01-01-S2 AI.jpg
  • 00320-BB01-01-S2 目录.jpg 00320-BB01-01-S2 Web.jpg

目前,JPG 是使用 Photoshop Droplet 自动创建的。然后,Droplet 在用户桌面上创建一个包含所有版本文件的目录。此文件夹称为 JPG。 如果我可以为 Desktop>JPGs 文件夹创建一个文件夹操作,然后自动运行脚本来创建新目录并移动文件,那将会很有用。创建文件可能需要 2 秒到 1 分钟,具体取决于一次创建的文件数量。

我有一些部分工作的代码来创建新文件夹,但我无法使用 Automator 中的文件夹操作自动运行它

on run {input, parameters}

    set chosenFolder to (choose folder)
    tell application "Finder" to set fileList to files of (chosenFolder)

    repeat with aFile in fileList
        set {name:Nm, name extension:Ex} to info for (aFile as alias)
        if Ex is missing value then set Ex to ""
        if Ex is not "" then set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
        set dateFolder to text 1 thru 13 of Nm
        set sourceFile to quoted form of POSIX path of (aFile as text)
        set destinationFile to quoted form of (POSIX path of chosenFolder & dateFolder & "/" & name of aFile)
        do shell script "ditto " & sourceFile & space & destinationFile
        do shell script "rm " & sourceFile
    end repeat
end run

这将创建一个新文件夹并移动正确的文件。当文件被放入特定文件夹时,我需要它自动运行。然后我需要将新创建的文件夹移动到另一个目录。

【问题讨论】:

    标签: applescript automator


    【解决方案1】:

    您可以通过文件夹操作来执行此操作,而无需使用 Automator。打开脚本编辑器应用并复制以下代码:

    on adding folder items to this_folder after receiving these_items
        tell application "System Events"
            set chosenFolder to POSIX path of this_folder
            repeat with aFile in these_items
                set {Nm, Ex} to {name, name extension} of aFile
                try
                    if Ex is not in {missing value, ""} then
                        set dateFolder to text 1 thru 13 of Nm
                        set destinationFolder to my checkForFolder(chosenFolder, dateFolder)
                        move aFile to destinationFolder
                    end if
                on error
                    set malformedFileNameFolder to my checkForFolder(chosenFolder, "Malformed File Names")
                    move aFile to malformedFileNameFolder
                end try
            end repeat
        end tell
    end adding folder items to --ing folder items to
    
    on checkForFolder(baseFolderPath, folderName)
        tell application "System Events"
            try
                if not (exists folder folderName of folder baseFolderPath) then
                    make new folder at folder baseFolderPath with properties {name:folderName}
                end if
                return folder folderName of folder baseFolderPath
            on error errstr
                display dialog errstr
            end try
        end tell
    end checkForFolder
    

    在“~/Library/Scripts/Folder Action Scripts”中使用您喜欢的任何名称保存它。打开文件夹操作设置应用程序,单击左侧的“+”按钮添加您的 JPGs 文件夹,单击右侧的“+”按钮将脚本附加到文件夹。那么你应该完成了。

    附:如果您需要以编程方式设置文件夹操作 - 例如,如果您手动设置这些操作而不仅仅是克隆用户 - 您可以使用如下脚本:

    tell application "System Events"
        set folderPath to "/path/to/target/folder"
        set scptPath to "/Users/whomever/Library/Scripts/Folder Action Scripts/script name"
    
        try
            set fa to first folder action whose path is folderPath
        on error errstr
            set fa to make new folder action with properties {path:folderPath, enabled:false}
        end try
    
        set {fileType, fileExt, fileName} to {file type, name extension, name} of file scptPath
        if fileType is "osas" or fileExt is "scpt" then
            tell fa
                try
                    make new script at end of scripts with properties {name:fileName}
                on error errstr
                    display dialog errstr
                end try
            end tell
        end if
    
        enable fa process new changes ask
    end tell
    

    您可以将最后一行中的“询问”更改为“是”或“否”,具体取决于您是否要自动处理文件夹中已有的项目。

    【讨论】:

      【解决方案2】:

      你可以移动文件夹

      do shell script "mv " & quoted form of (POSIX path of chosenFolder & dateFolder & "/") & space & "/path/to/destinationFolder
      

      如果两个文件分布在同一个 dateFolder 中会发生什么?上面的语法将覆盖现有文件夹。

      对于文件夹操作,您实际上不需要自动化工作流。

      【讨论】:

        最近更新 更多