AppleScript-如果文件在Excel工作表中列出,则添加另一个列值作为前缀

原始副本

事实:

  • 我有一个包含一堆.pdf文件的文件夹。名称看起来像(2-581.pdf,131-681.pdf,1138-4.pdf,...)
  • .pdf文件分为两部分:partA-partB.pdf,以“-”分隔
  • 我有一个Excel工作表,其中列出了文件夹中的所有结果
  • Excel工作表中列出了许多pdf,而不是该文件夹中的pdf。
  • Excel工作表的前2行是标题和标题
  • Excel工作表的第一列是test#,它是前缀
  • Excel工作表的第4列是原始.pdf文件名的partA,第5列partB

理想的结果-我运行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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Applescript(如果未添加文件)

来自分类Dev

Applescript如果包含一个单词

来自分类Dev

Applescript逐行读取文件

来自分类Dev

AppleScript:在JavaScript中循环

来自分类Dev

替换Applescript中的单词

来自分类Dev

在Applescript中按住标签

来自分类Dev

Applescript 与另一个应用程序文件选择交互

来自分类Dev

AppleScript:在另一个程序中打开当前的iTunes音轨[Riffstation]

来自分类Dev

如何在AppleScript中返回列表项的名称并使列表与另一个匹配?

来自分类Dev

AppleScript:如何将邮箱移动到Apple Mail中的另一个邮箱

来自分类Dev

如果Applescript失败了,那就结束了

来自分类Dev

如何使用AppleScript将列内容从一个Excel文档转移到另一个Excel文档

来自分类Dev

使用AppleScript从AppleScript文件(.scpt)读取代码

来自分类Dev

运行AppleScript的缺少值错误

来自分类Dev

如何启动AppleScript应用程序并从另一个AppleScript运行外壳程序脚本?

来自分类Dev

Applescript的相对文件路径

来自分类Dev

使用Applescript /终端复制文件

来自分类Dev

使用Applescript(小牛)移动文件

来自分类Dev

AppleScript文件夹导航

来自分类Dev

使用applescript保存illustrator文件

来自分类Dev

使用Applescript /终端复制文件

来自分类Dev

AppleScript:找不到存在的文件

来自分类Dev

Cocoa-AppleScript-使用xcode中的applescript更改xib标签

来自分类Dev

AppleScript编辑器中显示的AppleScript类名称

来自分类Dev

AppleScript编辑器中显示的AppleScript类名称

来自分类Dev

AppleScript-需要在AppleScript中运行命令

来自分类Dev

Applescript 获取 Excel 列的所有值

来自分类Dev

在AppleScript中删除匹配的文本

来自分类Dev

在IntelliJ IDEA中构建的applescript

Related 相关文章

热门标签

归档