如何解决使用 AppleScript 以数字形式导出 PDF
我正在修改我在 iWork 自动化上找到的代码,以满足我从 AppleScript 导出 PDF 的需求,并且可能遇到了软件的一些限制,但想看看这里是否有人可以提供一些帮助。
问题 1 - 我的电子表格中有 2 张工作表。工作表 1 包含任务,工作表 2 是发票,它引用了要填写表格的第一张工作表。使用我拥有的代码,它会复制电子表格并擦除工作表 #1,以便仅导出发票 - 这是我在 AppleScript 导出单个工作表方面找到的唯一解决方法。我如何能够将公式烘焙到发票表格中,以便在删除第 1 张表时保留内容?
问题 2 - 我想在导出的 PDF 上添加自定义标签,即“发票待付款”,但据我所知,我们仅限于使用苹果的集成颜色标签,是这种情况吗?我知道有一个程序“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)
解决方法
设置:一份文档,两张纸,每张纸上一张桌子。工作表 2 第 3 列的单元格包含引用工作表 1 上的单元格的公式。
-
将遍历这些单元格并用“格式化值”替换公式。然后,删除sheet 1,将剩余的文档导出到桌面。如果您有多个包含此类公式的列,请为“tell t21”块中的相关列添加一个重复循环。
-
唉,applescript 不能直接访问文件的扩展属性,但是它可以管理可以编辑它们的 shell 脚本(参见“xattr”和“kMDItemUserTags”)。但这是一只熊,应该是它自己的问题。 stackexchange 上已经有几个这样的问题了,虽然我不记得看到完整的答案。
-
这些活动要求 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
如果您不小心保存)。然而,虽然复制没有任何问题,但此脚本假定您正在处理可用的文档。
我尝试尽可能少地修复您的原始脚本,并添加了标记功能。测试留给您:
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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。