AppleScript - 如果文件在 excel sheet 中列出,添加另一个列值作为前缀

AppleScript - If file is listed in excel sheet add another column value as prefix

事实:

理想的结果- 我 运行 automator / applescript 并将其指向包含 .pdf 的文件夹和包含信息的 excel sheet。它 运行s 并重命名文件夹中在 excel 中找到的所有 .pdf。重命名的文件将由 test#、space 和原始名称组成。 excel sheet 中的行将以某种方式突出显示/标记,表明已找到。

我搜索了几个不同的论坛以及这个论坛,并尝试将代码拼凑在一起以尝试使某些东西起作用。我不会粘贴这段代码,因为我没有在我的场景中使用它。

我合并的以下代码是从另一个帮助论坛复制的,并尝试针对我的情况进行调整。它适用于前 6 个项目,然后就停止了(我想是因为第 7 个项目不在文件夹中)。此外,为了让它工作,我必须在 excel 中手动创建原始文件名和新文件名。我这样做是因为我无法让它在 excel.

之外工作

欢迎提供任何帮助、建议和可能解决方案的链接。我有很多测试结果要通过许多不同的 excel sheets,这会节省我很多时间。我今天输了,试图自己解决这个问题。我要么想得太多,要么想不通。谢谢!

set KeyFileName to choose file with prompt "Choose the Excel file"
set WorkingDirectory to (choose folder with prompt "Please locate the folder  with the processed .pdf's.") as string
set KeyFilePath to WorkingDirectory & KeyFileName
set FileNamesKey to my GetFileNamesKey(KeyFilePath)
set theResult to ChangeFileNames(WorkingDirectory, FileNamesKey)
return theResult
end run

to GetFileNamesKey(KeyFilePath)
    tell application "Microsoft Excel"

    set ResultsRange to used range of active sheet of active workbook

    set FileNames to {}
    repeat with i from 1 to count of rows in ResultsRange
        set NewFileName to value of third column of row i of ResultsRange
        set OldFileName to value of second column of row i of ResultsRange
        set FileNames to FileNames & {{OldFileName, NewFileName}}

    end repeat

    return FileNames

end tell
end GetFileNamesKey

to ChangeFileNames(WorkingDirectory, FileNamesKey)

tell application "Finder"
    repeat with aFile in FileNamesKey
        set OldFileName to item 1 of aFile
        set NewFileName to item 2 of aFile



        set PathToFile to WorkingDirectory & OldFileName
        try
            set FileExtension to name extension of file (WorkingDirectory & (item 1 of aFile))
            set name of file PathToFile to NewFileName & "." & FileExtension
        on error
            exit repeat
            return false
        end try
    end repeat
end tell

return true
end ChangeFileNames
return input
end run

如果我很清楚,你有一个 excel 文件,其中 col.A= #test, col.B= oldFileName (xx-yyy.pdf), col.C= NewName(空?),Col.D= PartA (xx),Col.E= PartB (yyy)

您想遍历该文件的所有行,搜索来自 Col.B 的文件是否存在于所选文件夹中,如果存在,您想要更改文件名(=colA + " " + 旧名称)并使其在 Excel 行可见。

如果没问题,那么下面的脚本就会执行您想要的操作。如果在所选文件夹中找到文件,我以两种方式标记 Excel 行:在 C 列中设置新文件名(不确定它之前是否为空?)并将其颜色设置为红色。我添加了许多评论以使其清楚(我希望!):

set KeyFileName to choose file with prompt "Choose the Excel file"
set WorkingDirectory to (choose folder with prompt "Please locate the folder  with the processed .pdf's.") as string

tell application "Microsoft Excel"
open KeyFileName
set ResultsRange to used range of active sheet of active workbook
-- Data bloc starts row 2 with headers and then row 3 to x
-- col.A= #test, col.B= oldFileName (xx-yyy.pdf), col.C= NewName (empty), Col.D= PartA (xx), Col.E= PartB (yyy)
repeat with i from 2 to count of rows in ResultsRange
    -- loop through each Excel row only starting row 2 of the range to skip hearders
    set OldFileName to value of second column of row i of ResultsRange
    set NewName to (value of the first column of row i of ResultsRange) & " " & OldFileName
    set theFile to WorkingDirectory & OldFileName
    tell application "Finder" to set FileExist to (file theFile exists) --check file exists
    if FileExist then
        tell application "Finder" to set name of file theFile to NewName --change file name in the folder
        set (value of third column of row i of ResultsRange) to NewName -- set new name in Excel col.C
        set (color index of interior object of third column of row i of ResultsRange) to 3 -- = Red !
    end if
end repeat --- next Excel row
end tell

可以跳过的第 "Open KeyFileName" 行是 Excel 文件已经打开。此外,脚本在更改后不会保存 Excel 文件!