我编写了这个简单的脚本,用于更改桌面上文件夹Images的文件名。该脚本在第一次运行时工作,但是当我使用不同的字符串为变量newName重新运行它时(相同的字符串会抛出错误:filename已经存在)。它只更改文件夹"的"项目x的偶数名称。 有人可以告诉我这是如何发生的,以及如何避免这种奇特的行为。我很难搞清楚这一点。 非常感谢你提前。
tell application "Finder"
set theFolder to ((path to desktop folder) & "Images") as string as alias
set myImages to the name of every file of theFolder
set indexOfFolder to (count items in myImages) as number
set newName to "whateverName" as string
repeat with x from 1 to indexOfFolder
set name of item x of theFolder to newName & x & ".JPG"
end repeat
end tell
编辑:经过一些更多测试后,问题似乎并不仅仅指向未改变的不均匀数字。有超过20个文件,它似乎从20开始切换到不会重命名的偶数。
答案 0 :(得分:0)
在我看来,你正在处理文件夹的名称,以及文件夹的字符(文件夹的项目)
请与此比较,以便正常工作:
tell application "Finder"
set theFolder to ((path to desktop folder as text) & "Images")
set myImages to the name of every file of folder theFolder
set indexOfFolder to (count myImages)
set newname to "Z picture " as string
repeat with x from 1 to indexOfFolder
set name of item x of folder theFolder to newname & x & ".JPG"
end repeat
end tell
它仍然没有错误,下一个差异是您在将内容收集到myImages
变量时使用文件,但是您解决了文件夹的项目,这是两个不同的对象类。我还最后将文件夹设置为最后一个实现,以避免冗余。请求还要注意我放置强制的地方,以及我已经删除的强制措施,因为它们是多余的。
tell application "Finder"
set theFolder to folder ((path to desktop folder as text) & "Images")
set myImages to the name of every file of theFolder
set indexOfFolder to (count myImages)
set newname to "Z picture " as string
repeat with x from 1 to indexOfFolder
set name of file x of theFolder to newname & x & ".JPG"
end repeat
end tell
即使文件更改名称,此版本也应该可以正常工作,因为文件列表是预先获得的。
tell application "Finder"
set theFolder to folder ((path to desktop folder as text) & "Images")
set myImages to every file of theFolder
set indexOfFolder to (count myImages)
set newname to "Z picture " as string
repeat with x from 1 to indexOfFolder
set name of item x of myImages to newname & x & ".JPG"
end repeat
end tell