Applescript - 替换字符“无法获取别名的每个项目"; [英] Applescript - Replace character "Cant get every item of alias"

查看:21
本文介绍了Applescript - 替换字符“无法获取别名的每个项目";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Below is my code to rename file. The code basically check for special character and replace with "_"(underscore).

代码

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

它抛出无法获取别名的每个文本项"请指教.

It throws "Cant get every text item of alias" Please advice.

推荐答案

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

choose file returns an alias. You can get the basename of the file with 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

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

这篇关于Applescript - 替换字符“无法获取别名的每个项目";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆