Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos 是否可以编写AppleScript来删除文件名中字符的第一个实例?_Macos_Terminal_Applescript_Automator - Fatal编程技术网

Macos 是否可以编写AppleScript来删除文件名中字符的第一个实例?

Macos 是否可以编写AppleScript来删除文件名中字符的第一个实例?,macos,terminal,applescript,automator,Macos,Terminal,Applescript,Automator,在下图中,我试图编辑cat目录中的每个文件名,使其仅包含一个“.”例如,对于所有其他文件名,我需要第一个文件名为cat1.jpg,以此类推 你们是否介意为我提供一些指导,告诉我如何实现AppleScript(使用Automator?)来重命名此cat目录中的所有文件 回答标题中的问题,在repeat循环中遍历字符串后,要从字符串中删除字符串的第一个实例,您可以: 使用偏移量: set s to "foo.bar.txt" set o to offset of "." in s set r to

在下图中,我试图编辑cat目录中的每个文件名,使其仅包含一个“.”例如,对于所有其他文件名,我需要第一个文件名为cat1.jpg,以此类推

你们是否介意为我提供一些指导,告诉我如何实现AppleScript(使用Automator?)来重命名此cat目录中的所有文件


回答标题中的问题,在
repeat
循环中遍历字符串后,要从字符串中删除字符串的第一个实例,您可以:

使用偏移量:

set s to "foo.bar.txt"
set o to offset of "." in s
set r to (text 1 thru (o - 1) of s) & (text (o + 1) thru -1 of s)
使用Applescript的文本项分隔符:

set AppleScript's text item delimiters to "."
set l to text items of s
set r to (item 1 of l) & (text items 2 thru -1 of l as string)
set AppleScript's text item delimiters to ""

假设所有要重命名的文件都位于桌面上名为“Cat”的文件夹中

以下AppleScript代码将删除“”的第一个实例。。。在原处留下一个空白。因此,“cat1.jpg”将被重命名为“cat1.jpg”等


这将是对您的问题的完整AppleScript解决方案。但是,如果这对您来说只是一次事件,那么通过Finder对文件进行批量重命名将是一个更快的解决方案。

为什么不使用标准的rename Finder功能呢?它可以替换名称中的文本,在您的情况下,将“cat.”替换为“cat”。此重命名功能在文件夹窗口的弹出菜单按钮(屏幕副本工具栏右起第三个按钮)中可用。不知道我为什么不在Finder中搜索“全部替换”功能!非常感谢。成功了!从OP的整个帖子来看,我认为可以肯定的是,他并不精通AppleScript代码。在我看来,在这种情况下,只需提及使用
重复循环
,并仅为文本项分隔符显示代码。。。提供了一个不完整的解决方案。为什么不同时提供
重复循环
的代码?另外,您的解决方案缺少实际更改filenames.wch的代码,因为SO不应该是一个问题:我真的不知道编码,您能给我写一个脚本来为我做这样那样的事情吗。如果这样的话,问题就应该被解决了。重点是鼓励他们做这项工作。没有显示任何代码的问题是可疑的,所以我给他们的好处是回答唯一棘手的部分。
property theFolder : ((path to desktop) as text) & "Cat"
property originalFileName : missing value
property originalFileNameExtension : missing value

tell application "Finder" to set theFiles to (files of folder theFolder) as alias list

repeat with i from 1 to count of theFiles
    set thisItem to item i of theFiles
    tell application "System Events"
        set fileInfo to thisItem's properties
        set {originalFileName, originalFileNameExtension} to ¬
            {name of fileInfo, name extension of fileInfo}
    end tell

    set theOffset to offset of originalFileNameExtension in originalFileName
    set shortName to text 1 thru (theOffset - 2) of originalFileName

    set text item delimiters to "."
    set tempText to every text item of shortName
    set text item delimiters to " "
    set cleanedName to (tempText as text) & "." & originalFileNameExtension

    tell application "System Events" to set name of thisItem to cleanedName
end repeat