1

下面是我重命名文件的代码。该代码基本上检查特殊字符并替换为"_"(下划线)。

代码

set thefile to choose file

    set Filename to my replace_chars(thefile, "   ", " ")
    set Filename to my replace_chars(thefile, "  ", " ")
    set Filename to my replace_chars(thefile, " ", "_")
    set Filename to my replace_chars(thefile, ",", "_")
    set Filename to my replace_chars(thefile, "!", "_")
    set Filename to my replace_chars(thefile, "~", "_")
    set Filename to my replace_chars(thefile, "*", "_")
    set Filename to my replace_chars(thefile, "/", "_")
    set Filename to my replace_chars(thefile, ":", "_")
    set Filename to my replace_chars(thefile, "(", "_")
    set Filename to my replace_chars(thefile, ")", "_")
    set Filename to my replace_chars(thefile, "___", "_")
    set Filename to my replace_chars(thefile, "__", "_")

tell application "Finder"

    set the name of file thefile to "Testing.png"
end tell

on replace_chars(this_text, search_string, replacement_string)
    set AppleScript's text item delimiters to the search_string
    set the item_list to every text item of this_text
    set AppleScript's text item delimiters to the replacement_string
    set this_text to the item_list as string
    set AppleScript's text item delimiters to ""
    return this_text
end replace_chars

它抛出"Cant get every text item of alias"请指教。

4

1 回答 1

2

choose file返回一个别名。您可以使用以下命令获取文件的基本名称tell app "Finder" to name of

set f to choose file
tell application "Finder" to set n to name of f
set n to replace(n, "a", "b")
tell application "Finder" to set name of f to n

on replace(input, x, y)
    set text item delimiters to x
    set ti to text items of input
    set text item delimiters to y
    ti as text
end replace

据我所知,如果您稍后在同一脚本中不依赖它,则无需恢复文本项分隔符属性。

于 2013-09-13T09:34:00.170 回答