是否使用Automator/Applescript在特定字符后裁剪文件名?

是否使用Automator/Applescript在特定字符后裁剪文件名?,applescript,automator,Applescript,Automator,我有一个文件夹,其中包含大约5000个文件,名称如下: Invoice 10.1 (2012) (Digital) (4-Attachments).pdf Carbon Copy - Invoice No 02 (2010) (2 Copies) (Filed).pdf 01.Reciept #04 (Scanned-Copy).doc 我想通过删除从第一个括号开始的所有内容来重命名这些文件,使它们看起来像这样: Invoice 10.1.pdf Ca

我有一个文件夹,其中包含大约5000个文件,名称如下:

    Invoice 10.1 (2012) (Digital) (4-Attachments).pdf 
    Carbon Copy - Invoice No 02 (2010) (2 Copies) (Filed).pdf
    01.Reciept #04 (Scanned-Copy).doc
我想通过删除从第一个括号开始的所有内容来重命名这些文件,使它们看起来像这样:

    Invoice 10.1.pdf
    Carbon Copy - Invoice No 02.pdf
    01.Reciept #04.doc
我发现很多脚本可以删除最后的x字母,但是没有任何脚本可以从特定的字符中裁剪出来

理想情况下,我想使用自动机,但我想这可能太复杂了。有什么想法吗?

试试:

set xxx to (choose folder)
tell application "Finder"
set yyy to every paragraph of (do shell script "ls " & POSIX path of xxx)
repeat with i from 1 to count of yyy
    set theName to item i of yyy
    set name of (file theName of xxx) to (do shell script "echo " & quoted form of theName & " | sed s'/ (.*)//'")
end repeat
end tell
可以使用,但无需为此使用
sed
——使用
offset
,普通的AppleScript即可:

set fullString to "Invoice 10.1 (2012) (Digital) (4-Attachments).pdf"
set trimmedString to text 1 thru ((offset of "(" in fullString) - 1) of fullString
-- trim trailing spaces
repeat while trimmedString ends with " "
    set trimmedString to text 1 thru -2 of trimmedString
end repeat
这将返回“Invoice 10.1”。要将文件名拆分为名称和扩展名,并重新添加扩展名,可以使用System Events的磁盘文件夹套件,该套件将提供方便的
名称扩展名
属性,您可以在修剪名称后存储并重新添加

假设您使用一些Automator操作来获取要处理的文件,完整的处理工作流将是在文件选择部分之后添加一个AppleScript操作,代码如下:

repeat with theFile in (input as list)
    tell application "System Events"
        set theFileAsDiskItem to disk item ((theFile as alias) as text)
        set theFileExtension to name extension of theFileAsDiskItem
        set fullString to name of theFileAsDiskItem
        -- <insert code shown above here>
        set name of theFileAsDiskItem to trimmedString & "." & theFileExtension
    end tell
end repeat
在文件插入时重复(以列表形式输入)
告诉应用程序“系统事件”
将FileAsDiskItem设置为磁盘项((文件作为别名)作为文本)
将FileExtension设置为FileAsDiskItem的名称扩展名
将fullString设置为FileAsDiskItem的名称
-- 
将FileAsDiskItem的名称设置为trimmedString&“&”&文件扩展名
结束语
结束重复
如果希望Automator工作流进一步处理文件,还必须为重命名的文件创建别名列表,并从AppleScript操作返回该列表(而不是
input
,这当然不再有效)