applescript - Apple Script - 搜索文件夹和重命名文件
问题描述
我正在寻找一些关于添加更大的苹果脚本的帮助,我看到了很多类似的查询,但没有一个完全符合要求,所以如果有人可以帮助或指导我找到答案,那将是一个巨大的帮助,
我想遵循这个一般前提
`“Choose Name” 默认答案 “” 将 ChosenName 设置为结果返回的文本
将 ImagesFolder 设置为(选择带有提示“选择图像文件夹:”的文件夹)`
我正在苦苦挣扎的那一点
如果 ImagesFolder 包含名为“Image Set 1”的文件夹,则查看文件夹“Images Set 1”并使用此逻辑重命名内容
if file name conatins 0001_ rename file to ChosenName & “front”</p>
if file name conatins 0002_ rename file to ChosenName & “Back”</p>
if file name conatins 0003_ rename file to ChosenName & “Top”</p>
if file name conatins 0004_ rename file to ChosenNamet & “Bottom”</p>
别的
如果 ImagesFolder 包含一个名为“Image Set 2”的文件夹,则查看文件夹 images 2 并使用此逻辑重命名内容
如果文件名包含 0001_ 将文件重命名为 ChosenName & “F”</p>
if file name conatins 0002_ rename file to ChosenName & “B”</p>
if file name conatins 0003_ rename file to ChosenName & “T”</p>
如果文件名包含 0004_ 将文件重命名为 ChosenNamet & “B”</p>
(如果有帮助,我用来识别这些文件的唯一字符始终是最后一个字符)
谢谢P
解决方案
该脚本可以满足您的需要。您需要对其进行扩展以管理“Image Set 2”文件夹及其扩展名,但只需复制 Tell“Finder”块中的内容就很容易了。
因为你有多个文件夹,我用一个子程序来处理你的文件夹,每次都调用新规则。例如,第一条规则是处理“图像集 1,搜索 0001,0002,0003,0004 并将每个替换为 Front,Back,Top,Bottom。
规则2是处理“图像集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 的文件:它会产生错误!
推荐阅读
- ssh - 使用命令字符串运行 ssh 如何在幕后工作?
- c# - Unix Timestamp 有 UTC 差异
- c - 为什么 recvfrom 通过广播 IP 没有收到数据?
- c++ - 如何构造一个具有恒定引用循环的类?
- c++ - 尝试为 gstreamer 构建 test-launch.c 但未能构建
- docker - 在 Docker 容器中安装 Kubernetes
- sql - 如何获取到今天为止 30 天内的更新记录?
- .net-5 - Visual Studio for Mac 试图在构建属性中找到 .NET Full Framework
- python - Python OpenPyxl 检测所有合并单元格时遇到问题
- c# - C# 委托和事件