我试图创建一个自动创建文件夹的文件夹,然后根据第一个数字将文件排序。我要排序的文件都有类似的格式,名称如 2月4日2.3 U#03(3).mrd 。我的目的是编写一些AppleScript来创建一个基于数字(2.3)的文件夹,然后将所有带有(2.3)的文件放入该文件夹,并对所有其他文件执行相同的操作。
我做了一些根据看起来有效的数字对文件进行排序,
set text item delimiters to {" "}
tell application "Finder"
set aList to every file in folder "Re-namer"
repeat with i from 1 to number of items in aList
set aFile to (item i of aList)
try
set fileName to name of aFile
set firstPart to text item 1 of fileName
set secondPart to text item 2 of fileName
set thirdPart to text item 3 of fileName
set fourthPart to text item 4 of fileName
set fifthPart to text item 5 of fileName
set newName to thirdPart & " " & secondPart & " " & firstPart & " " & fourthPart & " " & fifthPart
set name of aFile to newName
end try
end repeat
end tell
现在我只需要根据第一个数字创建新文件夹并将匹配的文件放入。我也尝试为此制作一个脚本(请记住,我之前从未编码过,也不知道是什么我做了,并且毫不奇怪它没有工作:(
tell application "Finder"
open folder "Re-namer"
set loc to folder "Re-namer"
set aList to every file in loc
repeat with i from 1 to number of items in aList
set aFile to (item i of aList)
if not (exists folder named "text item 1" in loc) then
make new folder in loc with properties {name:"text item 1"}
else
move aFile in folder "text item 1"
end if
end repeat
end tell
我发现了一些类似的问题,但我仍然无法让它发挥作用。如果有人有任何想法或资源来帮助解决这个问题,我会非常感谢它。
答案 0 :(得分:0)
你非常接近正确的剧本。但是,不是做2个循环,一个用于更改名称而另一个用于移动文件,所以只需要同时执行两个循环,就可以更快地完成两个循环。
另外,关键是要理解,在循环中,如果更改名称,Finder会松开对更改文件的引用,然后在更改名称后尝试移动时失败。但既然你知道了文件夹和新名称,你仍然可以参考那个" new"文件。
set MyRenamer to choose folder "Select the folder"
set TPath to MyRenamer as string
set text item delimiters to {" "}
tell application "Finder"
set aList to every file in MyRenamer
repeat with F in aList
set fileName to name of F
set firstPart to text item 1 of fileName -- like "Feb"
set secondPart to text item 2 of fileName -- like "4"
set thirdPart to text item 3 of fileName -- like "2.3"
set fourthPart to text item 4 of fileName -- like "U#03"
set fifthPart to text item 5 of fileName -- like "(3).mrd"
set newName to thirdPart & " " & secondPart & " " & firstPart & " " & fourthPart & " " & fifthPart
set name of F to newName
if (not (exists folder thirdPart in MyRenamer)) then
make new folder in MyRenamer with properties {name:thirdPart}
end if
move (TPath & newName) as alias to folder (TPath & thirdPart & ":") -- rebuild the new file path as alias and move it.
end repeat
end tell
经过测试,确定。