使用 Applescript/Automator 根据数字电子表格重命名文件

Rename files based on numbers spreadsheet using Applescript/Automator

我有一个 applescript,用于根据数字电子表格提供的名称重命名文件。

每张图片都有三种不同的尺寸,每种尺寸都在一个文件夹中。所以有

/50/exportedImage-01.svg

/33/exportedImage-01.svg

/25/exportedImage-01.svg

该脚本工作得很好并且做了它应该做的事情,但它非常慢并且在超过 50 个文件时停止。因为我有几百个文件要重命名,所以问题是:

有什么方法可以改变脚本使其更 efficient/powerful 吗?

工作流程如下:

这是我目前得到的:

set numbersFile to choose file with prompt "Choose the Numbers file" of type {"numbers", "XLS7", "XLS8", "XLSX"}
set theFolder to choose folder with prompt "Choose the folder 50 containing the files to rename"
--get a list of the old and the new filenames
set exportedImage to {}
set featuredImage to {}
tell application "Numbers"
    open numbersFile
    tell table 1 of sheet 1 of document 1
    --tell document 1
    --tell sheet 1
    repeat with i from 1 to row count
        if value of cell ("C" & i as text) > "" then
            set exportedImage to exportedImage & value of cell ("C" & i as text)
            set featuredImage to featuredImage & value of cell ("B" & i as text)
        else
            exit repeat
        end if
    end repeat
    --end tell
end tell
close window 1
end tell

--loop through the files in folder 50 and rename them
tell application "Finder"
    repeat with k from 1 to (count of (get every item of theFolder))
        repeat with i from 1 to count of exportedImage
            if (name of item k of theFolder) as text = (item i of exportedImage) as text then
                set name of (item k of theFolder) to (item i of featuredImage as text)
            end if
        end repeat
    end repeat
end tell

--loop through the files and rename them 33
set theFolder to choose folder with prompt "Choose the folder 33 containing the files to rename"
tell application "Finder"
    repeat with k from 1 to (count of (get every item of theFolder))
        repeat with i from 1 to count of exportedImage
            if (name of item k of theFolder) as text = (item i of exportedImage) as text then
                set name of (item k of theFolder) to (item i of featuredImage as text)
            end if
        end repeat
    end repeat
end tell

--loop through the files and rename them 25
set theFolder to choose folder with prompt "Choose the folder 25 containing the     files to rename"
tell application "Finder"
    repeat with k from 1 to (count of (get every item of theFolder))
        repeat with i from 1 to count of exportedImage
            if (name of item k of theFolder) as text = (item i of exportedImage) as text then
                set name of (item k of theFolder) to (item i of featuredImage as text)
            end if
        end repeat
    end repeat
end tell

我想坚持使用 applescript,因为这个脚本是更大的自动化工作流程的一部分。

感谢任何帮助,谢谢!

我花了一些时间来完成我的第一条评论。让我解释一下如何使用 'find' 命令。

我们正在文件夹“/Users/Document/Folder50/”中查找文件名 "ExportedImage1.svg"。 shell 命令是:查找 /Users/Document/Folder50/ExportedImage1.svg

'Find' 命令可以通过使用 '-exec' 函数来增强。对于找到的每个文件,-exec 将在之后应用命令。在 exec 中,下一个命令可以使用 {} 表示在 find 命令中找到的文件。最后但并非最不重要的一点是,语法需要一个“;”最后。

示例:查找 path/file.svg -exec mv {} path/newName.svg ';'

它搜索 path/file.svg,如果找到,它会将 mv 命令(移动或重命名)应用到具有新值 path/newName.svg.[=11= 的文件 {} ]

在下面的脚本中,我使用了 Excel(因为我没有数字!),但是您可以轻松地调整脚本的第一个块以使用您的数字。

我还通过循环所有 Excel 数据行(第二个冒号=导出的图像)来更改您的逻辑。在该循环内,对于每个 image/excel 行,我循环浏览您选择的 3 个文件夹(25、33 和 50)。在该循环中,我搜索文件夹中的文件,如果找到,我使用 Excel 文件 (FeaturedImage) 的第一个冒号更改名称。

我假定文件扩展名始终为“.svg”

-- you must replaced that block by your Numbers block
set numbersFile to choose file with prompt "Choose the Numbers file" of type {"numbers", "XLS7", "XLS8", "XLSX"}
tell application "Microsoft Excel"
-- my Excel file is made of column B=FeaturedImage (will be new file names) and column C=ExportedImage (current file name)
-- I assume that name in files have no extension '.svg'
    open numbersFile
    tell active sheet to set myData to value of used range
    close window 1
    -- myData is list of rows: each item is a list made of 2 values : value in col B and value in col C
end tell


-- select the 3 folders
set folder50 to choose folder with prompt "Choose the folder 50 containing the files to rename"
set folder33 to choose folder with prompt "Choose the folder 33 containing the files to rename"
set folder25 to choose folder with prompt "Choose the folder 25 containing the files to rename"
-- build a list of the 3 folders
set myFolders to {POSIX path of folder50}
set myFolders to myFolders & {POSIX path of folder33}
set myFolders to myFolders & {POSIX path of folder25}


repeat with myRow in myData -- loop through all data rows   
    repeat with aFolder in myFolders -- loop through folders : 25, 33, 50

        -- search file name = item 2 of myRow (=ExportedImage) in folder 50 and if find, rename to item 1 of myRow
        set curPath to quoted form of (aFolder & (item 2 of myRow) & ".svg") -- the complete path/name to search
        set newPath to quoted form of (aFolder & (item 1 of myRow) & ".svg") -- the complete path/name to replace/rename
        try -- to avoid error in case file not found in the folder
            do shell script "find " & curPath & " -exec mv {} " & newPath & " ';'"
        end try
    end repeat -- next folder 25, 33, 50
end repeat -- next data row

循环的优化和 'find - exec' 的使用使这个脚本比你的 !!

快得多