AppleScript用于更改文件夹内的文件名

时间:2016-04-01 11:59:49

标签: macos applescript

我需要编写一个可以更改文件夹内文件名的AppleScript。这是我的方案。

Folder1
   |
Folder2
   |
imageA.png,imageB.png

现在我需要编写一个AppleScript,可以将imageA.png的名称更改为A.png,将imageB.png更改为B.png。

我的AppleScript应该是什么?这不是我的确切场景,但我在上面给出了一个例子,所以我可以从中实现更多。

我试过下面的脚本并且它运行良好但是当文件名不存在时我收到错误,意味着它不会忽略它。

tell application "Finder"

    set the name of file "iOs_142:About1.png" to "About1.png"
    set the name of file "iOs_142:FAQs.png" to "FAQs1.png"

end tell

1 个答案:

答案 0 :(得分:0)

如果您真的坚持使用AppleScript执行此操作,则应该提供一个模板以继续:

(* Taken from http://applescript.bratis-lover.net/library/string/#replaceString *)
on replaceString(theText, oldString, newString)
        local ASTID, theText, oldString, newString, lst
        set ASTID to AppleScript's text item delimiters
        try
                considering case
                        set AppleScript's text item delimiters to oldString
                        set lst to every text item of theText
                        set AppleScript's text item delimiters to newString
                        set theText to lst as string
                end considering
                set AppleScript's text item delimiters to ASTID
                return theText
        on error eMsg number eNum
                set AppleScript's text item delimiters to ASTID
                error "Can't replaceString: " & eMsg number eNum
        end try
end replaceString

tell application "Finder"
        set fs to files of folder POSIX file "/your/folder" as list
end tell
repeat with f in fs
        set n to replaceString(name of f, "to be replaced", "")
        tell application "System Events" to set name of f to n
end repeat