如何从 macOS 子文件夹中删除 (1)?

问题描述 投票:0回答:1

由于一次失败的尝试,通过 Google Drive 将我的所有应用程序和文档文件夹到新的 iMac,我最终得到了大量文件夹、子文件夹和文件,显示了我尝试对它们进行复制的迭代,导致文件夹/末尾附加 (N) 的文件,其中 N 是尝试次数,即“文件夹 (1)”或“文件夹 (2)”,有时是多个,即“文件夹 (1) (1)”。

我发现了一个 AppleScript,我认为它可以纠正以下问题:

set stopCharacters to " (1)"
set stopCharacterCount to (count stopCharacters)

set sourceFolder to choose folder default location (path to home folder)

tell application "Finder"
    set sourceFiles to (every file in sourceFolder ¬
        whose name contains stopCharacters and name ends with "pdf") as alias list
end tell

repeat with aFile in sourceFiles
    tell application "Finder" to set aFileName to name of aFile
    set stopOffset to (offset of stopCharacters in aFileName) + stopCharacterCount - 1
    set newFileName to text 1 thru stopOffset of aFileName & ".pdf"
    try
        tell application "Finder" to set name of aFile to newFileName
    on error errorMessage
        set dialogText to "The source file " & quote & aFileName & quote & ¬
            " could not be renamed. The error message was:" & return & return & errorMessage
        display dialog dialogText buttons {"Cancel", "Skip"} default button 1
    end try
end repeat

对于 AppleScript,我是个新手,但我没能让它发挥作用。如果我必须经历并从所有东西中删除(1),那不会是世界末日,但如果我可以的话,它肯定会让我的生活更轻松。

有人可以帮我或提供线索吗?

directory applescript subdirectory batch-rename finder
1个回答
0
投票

问题似乎是您将不需要的字符串重新添加到文件的新名称中。试试这个。

use scripting additions -- as some commands are part of standard additions
set webbo to ".pdf" -- variable will allow for better ease of use
set stopCharacters to " (1)"
set stopCharacterCount to (count stopCharacters)

set sourceFolder to choose folder default location (path to home folder)

tell application "Finder"
    set sourceFiles to (every file in sourceFolder ¬
        whose name contains stopCharacters and name ends with webbo) as alias list
end tell
log sourceFiles

-- set newNameList to {}
repeat with aFile in sourceFiles
    tell application "Finder" to set aFileName to name of aFile
    set stopOffset to (offset of stopCharacters in aFileName) - 1 -- should be offset of last character to keep
    log stopOffset
    set newFileName to (text 1 thru stopOffset of aFileName)
    log {aFileName, newFileName}
    try
        tell application "Finder" to set name of aFile to (newFileName & webbo)
        -- set end of newNameList to newFileName
    on error errorMessage
        set dialogText to "The source file " & quote & aFileName & quote & ¬
            " could not be renamed. The error message was:" & return & return & errorMessage
        display dialog dialogText buttons {"Cancel", "Skip"} default button 1
    end try
end repeat
-- newNameList -- list of new file names

注释

  • offset 的确定中删除了
    stopCharacters
    。这就是为什么脚本什么也没做
  • 扩展名移至脚本的后面部分,以便更轻松地查看脚本此时正在执行的操作
  • 所有添加的
    log
    操作都是可选的
  • 如果您有大量数字需要删除 [例如 (14)、(15)],那么您可以将
    choose folder
    之后的脚本部分放入其自己的循环中,该循环为 stopCharacters 提供括号内的数字。 stopCharacters 行也需要位于循环内部。
  • 您还可以让脚本通过子文件夹运行。如果您有很多文件/文件夹,并且您发现性能很糟糕,那么您可以使用系统事件而不是 Finder 来循环浏览所有文件/文件夹,这应该花费更少的时间。

为了将来参考,请解释运行脚本做了什么,而不仅仅是解释它不起作用,即使结果没有任何改变。当试图弄清楚别人的计算机上发生了什么时,它会很有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.