我正在尝试制作一个文件夹,该文件夹将自动创建文件夹,然后根据第一个数字对文件进行排序。我必须排序的文件都以类似的格式出现,其名称为, Feb 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
现在,我只需要根据第一个数字创建新文件夹并将匹配文件放入。我也尝试为此制作一个脚本(请记住我从未编码过,不知道我'm做(,毫不奇怪,它无效:(
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
我发现了一些类似的问题,但我仍然无法正常工作。如果有人有任何想法或资源来帮助解决这个问题,我会大力认可它。
您非常接近正确的脚本。但是,与其进行2个循环,一个用于更改名称,另一个用于移动文件,同时只执行1个循环更快。
另外,关键是要了解,在循环中,如果更改名称,则查找器将更改对文件的引用释放,那么在更改名称后尝试移动它时,它会失败。但是,由于您知道文件夹和新名称,因此您仍然可以参考该"新"文件。
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
测试并确定。