Apple Script- 搜索文件夹和重命名文件

Apple Script- Search Folder and Rename files

我正在寻找一些关于添加更大的 apple 脚本的帮助,我看到了很多类似的查询,但是 none 非常符合要求,所以如果有人可以帮助或指导我找到答案,我会是一个巨大的帮助,

我想遵循这个一般前提

`“选择姓名”默认答案“” 将 ChosenName 设置为结果返回的文本

将 ImagesFolder 设置为(选择文件夹时提示“选择图像文件夹:”)`

我正在苦苦挣扎

如果 ImagesFolder 包含名为“Image Set 1”的文件夹,则 查看文件夹“Images Set 1”并使用此逻辑重命名内容

if file name conatins 0001_ rename file to ChosenName & “front”

if file name conatins 0002_ rename file to ChosenName & “Back”

if file name conatins 0003_ rename file to ChosenName & “Top”

如果文件名包含 0004_ 将文件重命名为 ChosenNamet & “Bottom”

其他

如果 ImagesFolder 包含名为“Image Set 2”的文件夹,则 查看文件夹 images 2 并使用此逻辑重命名内容

if file name conatins 0001_ rename file to ChosenName & “F”

if file name conatins 0002_ rename file to ChosenName & “B”

如果文件名包含 0003_ 将文件重命名为 ChosenName & “T”

如果文件名包含 0004_ 将文件重命名为 ChosenNamet & “B”

(如果有帮助,我用来识别这些文件的唯一字符总是最后一个字符)

谢谢 P

这个脚本可以满足您的需求。您需要扩展它以管理 "Image Set 2" 文件夹及其扩展名,但是复制 Tell "Finder" 块中的内容将非常容易。

因为你有多个文件夹,我用了一个子程序来处理你的文件夹,每次都调用新规则。例如,第一个规则是处理“图像集 1,搜索 0001,0002,0003,0004 并将每个替换为 Front,Back,Top,Bottom。

规则2是处理“Image Set 2,搜索0001,0002,0003,0004并分别替换为F,B,T,B。

第一部分建立规则。脚本本身被简化为遍历每个规则的循环,使用 3 个变量调用子例程 "Process_SubFolder":子文件夹名称、当前目标和新名称。

(* 
Define record Rule, made of 3  variables : 
   NFolderNFolder: the name of sub-folder
   NSource : the list of part of file names to be processed
   NDest : the list of new names. This list MUST count same number of items as NSource       
All rules are added into ListRules
*)
global ChosenName, ImagesFolder -- mandatory to use in the sub-routine

set Rule to {NFolder:"Image Set 1", NSource:{"0001", "0002", "0003", "0004"}, NDest:{"Front", "Back", "Top", "Bottom"}}
set ListRules to {Rule}
set Rule to {NFolder:"Image Set 2", NSource:{"0001", "0002", "0003", "0004"}, NDest:{"F", "B", "T", "B"}}
set ListRules to ListRules & {Rule}


set R to display dialog "Enter a name" default answer ""
set ChosenName to text returned of R
if ChosenName is "" then return -- no name selected, end of script

set ImagesFolder to choose folder with prompt "Choose Images Folder:"
repeat with aRule in ListRules
    Process_SubFolder(NFolder of aRule, NSource of aRule, NDest of aRule)
end repeat
-- end of main script


on Process_SubFolder(LFolder, LSource, LDest)
    tell application "Finder"
        set SubFolder to (ImagesFolder as string) & LFolder
        if folder SubFolder exists then
            set FileList to every file of folder SubFolder -- get all files of Images Set 1
            repeat with aFile in FileList -- loop through each file
                set FName to name of aFile
                set NewName to ""

                -- Manage extension of the file
                if name extension of aFile is "" then
                    set NewExt to ""
                else
                    set NewExt to "." & name extension of aFile
                end if

                repeat with I from 1 to count of LSource --loop trhough each source of the rule
                    if FName contains (item I of LSource) then set NewName to ChosenName & (item I of LDest) & NewExt
                end repeat
                if NewName is not "" then set name of aFile to NewName -- only if name must be changed !
            end repeat -- loop through files of LFolder
        end if -- folder exists
    end tell
end Process_SubFolder

使用此结构,您可以添加任意数量的规则!

当然,我假设您永远不会在子文件夹中出现两次相同的名称!在 Image Set 2 中情况并非如此,您将有 2 个新名称 = ChosenNameB 的文件:它会产生错误!!