0

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

问题 1 - 我的电子表格中有 2 张工作表。表号 1 包含任务,表号 2 是引用第一张表填写表格的发票。使用我拥有的代码,它会复制电子表格并删除表格#1,以便仅导出发票 - 这是我在 AppleScript 导出单个表格方面发现的唯一解决方法。我如何能够将公式烘焙到发票表中,以便在删除表 #1 时,内容仍然存在?

问题 2 - 我想在导出的 PDF 上添加一个自定义标签,即“发票待付款”,但据我所知,我们仅限于使用苹果的集成颜色标签,是这样吗?我知道有一个程序'Hazel'可以实现我想要的,但我宁愿自己编写脚本而不是使用另一个应用程序。

问题 3 - 是否有任何方法可以更改此脚本,使其不会在 GUI 中提取数字并在命令行的后台完全运行?

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

谢谢。

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)
4

2 回答 2

1

设置:一个文档,有两张纸,每张纸上一张桌子。工作表 2 的第 3 列的单元格包含引用工作表 1 上的单元格的公式。

  1. 将遍历这些单元格并将公式替换为“格式化值”。然后,删除工作表 1,并将剩余的文档导出到桌面。如果您有多个包含此类公式的列,则为“tell t21”块中的相关列添加重复循环。

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

  3. 这些活动要求 Numbers 是开放的。然而,它不应该是最前面的应用程序,因此“激活”行是可选的。

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

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如果你不小心这样做了)。然而,虽然复制没有任何问题,但此脚本假定您正在处理可用的文档。

于 2021-06-04T13:03:50.820 回答
1

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

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)
于 2021-06-04T14:27:33.037 回答