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

时间:2018-11-28 11:57:28

标签: applescript spreadsheet automator

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

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

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

任何帮助表示感谢,谢谢!

1 个答案:

答案 0 :(得分:1)

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

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

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

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

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

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

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