【问题标题】:Rename files based on numbers spreadsheet using Applescript/Automator使用 Applescript/Automator 根据数字电子表格重命名文件
【发布时间】:2018-11-28 11:57:28
【问题描述】:

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

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

/50/exportedImage-01.svg

/33/exportedImage-01.svg

/25/exportedImage-01.svg

该脚本运行良好,并且执行了它应该做的事情,但是它非常缓慢并且在 50 多个文件处停止。因为我有几百个文件要重命名,所以问题是:

有没有办法改变脚本以使其更高效/更强大?

工作流程如下:

  • 读取数字文件 - B 列中的新名称 (featuredImage),C 列中的旧名称 (exportedImage)
  • 遍历第一个文件夹中的文件并根据 B 列重命名它们
  • 遍历第二个文件夹中的文件并根据 B 列重命名它们
  • 遍历第三个文件夹中的文件并根据 B 列重命名它们

这是我目前得到的:

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,因为这个脚本是更大的自动化工作流程的一部分。

任何帮助表示赞赏,谢谢!

【问题讨论】:

  • 我建议你使用'do shell script'指令来运行shell命令。对于 shell 命令,您可以使用“查找”来查找具有您要查找的名称的文件,并使用管道来重命名找到的文件。这样做,您应该在脚本顶部请求 3 个文件夹(50、33 和 25)。然后你只循环遍历你的行数,并在该循环内使用 do shell 脚本查找/重命名处理 3 个文件夹中的每一个。使用这些 shell 命令重命名 100 个文件只需几秒钟。

标签: applescript spreadsheet automator


【解决方案1】:

我需要一些时间来完成我的第一条评论。让我解释一下如何使用“查找”命令。

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

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

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

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

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

我还通过循环遍历所有 Excel 数据行(第二个冒号 = 导出的图像)更改了您的逻辑。在该循环中,对于每个图像/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'的使用使这个脚本比你的更快!!

【讨论】:

  • 非常感谢您的脚本。改变了一些东西(数字而不是 Excel),但自动机给了我一个错误:/bin/bash: --: invalid option.
  • Automator 出现错误是什么意思?此脚本未使用 Automator。我猜您将此脚本插入 Automator 工作流程以使其成为服务。您应该更好地在没有 Automator 的情况下测试脚本本身。然后你会看到错误在哪里。可能是 curPath 和 newPath 未填充正确的路径/文件名。也可能是您的某些文件名(导出或精选)包含 Unix 不喜欢的特殊字符。 1)将您的脚本作为独立脚本放入脚本编辑器中。添加日志行以查看 carPath 和 newPath 的值。让我知道结果
  • 感谢您的解释!我在脚本编辑器中测试了脚本,甚至在提示输入数字文件之前,它给了我一个错误:End of line expected, but class name found.
  • 我相信问题出在您已更改/添加以使用 Numbers 的行。 Excel 脚本完美运行。通常光标会显示哪一行不适合编译。如果没有,你必须尝试削减代码,直到它符合,然后你就会知道哪个部分不正确。
  • 啊,谢谢...我第二条评论中提到的错误是指tell active sheet to set myData to value of used range这一行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多