使用 AppleScript 以 Numbers 格式导出 PDF

Exporting PDF's in Numbers with AppleScript

我正在调整我在 iWork 自动化上找到的代码以满足我从 AppleScript 导出 PDF 的需要,并且可能 运行 受到软件的一些限制,但想看看这里是否有人可以提供一些帮助..

问题 1 - 我的传播sheet 中有 2 个 sheet。 Sheet 1 号包含任务,sheet 2 号是引用第一个 sheet 填写 table 的发票。使用我拥有的代码,它复制了 spreadsheet 并删除了 sheet #1 以便仅导出发票 - 这是我在 AppleScript 导出单个 sheet.我如何才能将公式添加到发票的 table 中,以便在删除 sheet #1 时保留内容?

问题 2 - 我想在导出的 PDF 上添加自定义标签,即 'invoice pending payment',但据我所知,我们仅限于使用 apple 的集成颜色标签,是这样吗?我知道有一个程序 'Hazel' 可以实现我想要的,但我宁愿自己编写脚本也不愿使用其他应用程序。

问题 3 - 有什么方法可以更改此脚本,使其不会在 GUI 中拉出 Numbers 并在命令行后台完全 运行 吗?

我正在使用的脚本如下所示。

谢谢。

set {year:y, month:m, day:d} to (current date)
set dateString to (d) & "_" & (m as integer) & "_" & (y as text)
set destinationFolder to ("Macintosh HD:Users:Test:Finance:Income:Invoices:" & year of (current date) & ":") as text
set theFile to choose file of type "numbers"

tell application "Finder"
    set docName to name of theFile
    if docName ends with ".numbers" then ¬
        set docName to text 1 thru -9 of docName & "_" & dateString & "_invoice"
    set theDuplicate to duplicate file (theFile as text) -- create duplicate
    try -- try block, because maybe destination file already exists
        make new file at folder destinationFolder with properties {name:(docName & ".pdf")}
    end try
end tell

tell application "Numbers"
    activate
    set Doc to open (theDuplicate as text as alias) -- open the duplicate 
    delete sheet 1 of Doc
    set PDFExportFileName to destinationFolder & docName & ".pdf"
    export Doc to file PDFExportFileName as PDF
    close documents saving no -- quit without saving
end tell

-- delete the temporary duplicate (if need)
tell application "System Events" to delete file (theDuplicate as text)

设置:一个文档,两个 sheet,每个 sheet 一个 table。 sheet 2 的第 3 列单元格包含引用 sheet 1 上的单元格的公式。

  1. 将遍历这些单元格并将公式替换为 'formatted value'。然后,删除sheet1,将剩下的文档导出到桌面。如果您有多个包含此类公式的列,请为 'tell t21' 块中的相关列添加一个重复循环。

  2. 唉,applescript 不能直接访问文件的扩展属性,但是它可以管理一个可以编辑它们的 shell 脚本(参见 'xattr' 和 'kMDItemUserTags').但那是一只熊,应该是它自己的问题。 stackexchange 上已经有几个这样的问题,但我不记得看到过完整的答案。

  3. 这些活动需要公开号码。但是,它不需要是最前面的应用程序,因此 'activate' 行是可选的。

注意上面的假设,这里没有错误处理。

tell application "Numbers" -- v5.1
    set w1 to window 1
    set d1 to document 1
    set s1 to sheet 1 of d1
    set t1 to table 1 of s1
    set s2 to sheet 2 of d1
    set t21 to table 1 of s2
    
    -- overwrite formula with value
    tell t21 -- sheet 2 tab 1
        repeat with rc3 from 1 to (count of rows in column 3) -- arbitrarily put formulae in column 3; modify to suit your data;
            set value of cell ("C" & rc3) to formatted value of cell ("C" & rc3)
        end repeat
    end tell
    
    -- export to pdf
    delete s1
    set dPath to path to desktop as text
    export d1 as PDF to dPath & "expo.pdf"
    close w1 -- will ask to save, choose revert
    
end tell

顺便说一句,您实际上不需要复制文件 — 只是不要保存它(或者 revert to last saved 如果您不小心保存了)。然而,虽然复制没有任何问题,但此脚本假定您正在使用可用的文档。

我尝试尽可能少地修复您的原始脚本,并添加了标记功能。测试留给你:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set {year:y, month:m, day:d} to (current date)
set dateString to (d) & "_" & (m as integer) & "_" & (y as text)
set destinationFolder to "" & (path to home folder) & "Test:Finance:Income:Invoices:" & year of (current date) & ":"
set theFile to choose file of type "numbers"

tell application "Finder"
    set docName to name of theFile
    if docName ends with ".numbers" then ¬
        set docName to text 1 thru -9 of docName & "_" & dateString & "_invoice"
    try -- because maybe destination file already exists
        make new file at folder destinationFolder with properties {name:(docName & ".pdf")}
    end try
end tell

tell application "Numbers"
    open theFile
    tell (table 1 of sheet 2 of document 1) -- overwrite formula with value
        repeat with rc3 from 1 to (count of rows in column 3) -- arbitrarily put formulae in column 3; modify to suit your data;
            set value of cell ("C" & rc3) to formatted value of cell ("C" & rc3)
        end repeat
    end tell
    delete sheet 1 of document 1
    set PDFExportFileName to destinationFolder & docName & ".pdf"
    export document 1 to file PDFExportFileName as PDF -- export to pdf
    close documents saving no -- quit without saving
end tell

-- add text tag
set aURL to current application's |NSURL|'s fileURLWithPath:(POSIX path of PDFExportFileName)
aURL's setResourceValue:{"invoice pending payment"} forKey:(current application's NSURLTagNamesKey) |error|:(missing value)