事实:
理想的结果-我运行automator / applescript并将其指向包含.pdfs的文件夹和包含信息的excel表。它运行并重命名excel中找到的文件夹中的所有.pdf。重命名的文件将由测试号,空格和原始名称组成。Excel工作表中的行将以某种方式突出显示/标记,表明已找到该行。
我已经搜索了几个不同的论坛以及这个论坛,并试图将代码拼凑起来,以使某些东西能够正常工作。我不会粘贴此代码,因为在我的场景中没有任何代码可以正常工作。
我合并的以下代码是从另一个帮助论坛复制而来,并试图针对我的情况进行调整。对于前6个项目,它的工作随后就停止了(我认为是因为第7个项目不在文件夹中)。另外,要使其正常工作,我必须在excel中手动创建原始文件名和新文件名。我这样做是因为我无法使它在excel之外工作。
欢迎任何帮助,建议,可能解决方案的链接。我在许多不同的excel表格上都有大量的测试结果,这将为我节省大量时间。我今天输了,试图自己弄清楚这一点。我不是在想这个,就是在想。谢谢!
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
如果Excel文件已经打开,则可以跳过“打开KeyFileName”行。更改后,脚本也不会保存Excel文件!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句