Applescript - 替换字符“无法获取别名的每一项”

时间:2013-09-13 09:20:53

标签: applescript

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

代码

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" 请提供建议。

1 个答案:

答案 0 :(得分:1)

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

据我所知,restoring text item delimiters property is not necessary如果你以后在同一个剧本中不依赖它。