Applescript:GUI编程,用于为Appleworks递归打印为PDF
目标:成千上万的旧Clarisworks和Appleworks文档需要成为PDF 问题:脚本页面不是选项;它不能可靠地保留格式。它必须是Appleworks。这个脚本在起作用时起作用;但它跳过文件的原因我无法确定Applescript:GUI编程,用于为Appleworks递归打印为PDF,pdf,printing,io,applescript,Pdf,Printing,Io,Applescript,目标:成千上万的旧Clarisworks和Appleworks文档需要成为PDF 问题:脚本页面不是选项;它不能可靠地保留格式。它必须是Appleworks。这个脚本在起作用时起作用;但它跳过文件的原因我无法确定 set appleworksFolder to choose folder tell application "Finder" set folderItems to (files of entire contents of appleworksFolder) repea
set appleworksFolder to choose folder
tell application "Finder"
set folderItems to (files of entire contents of appleworksFolder)
repeat with I from 1 to number of items in folderItems
set the_doc to item I of folderItems
set doc_name to name of the_doc as text
(* Some files are missing metatags and try to save as "Untitled Document",
this block ensures a file name is unique, later *)
tell application "Finder"
set the clipboard to doc_name & ".pdf"
end tell
(* Each file exists in a folder with a path.txt file that will later
be used to put the file back where it was originally stored prior
to this conversion process *)
if name of the_doc is not "path.txt" then
try
tell application "Finder"
(* Many files no longer have name extensions and appear as UNIX
executables if not repaired *)
try
set nmex to name extension of the_doc as text
on error
set nmex to "ok"
end try
if nmex is not "cwk" or "CWK" then
set the_doc_str to the_doc as text
set doc_path to POSIX path of the_doc_str
do shell script "mv '" & doc_path & "' " & "'" & doc_path & ".cwk'"
end if
delay 1
(* In case Appleworks hangs or otherwise bungs up, I force-quit
it at the end of the script; this ensures its closed before
it tries to open the next file *)
if (application process "Appleworks 6" of application "System Events" exists) then
do shell script "killall 'LaunchCFMApp'"
delay 1
end if
tell application "AppleWorks 6"
open the_doc
end tell
(* Some of the documents are huge, this delay gives the app time to load
since this is all GUI-scripted *)
delay 5
tell application process "Appleworks 6" of application "System Events"
(* This is where I think I am encountering problems; there are two
possible warnings that may or may not appear on opening the doc;
that Appleworks needs to append a version number to the file (if
its old) or that the file may be damaged and thus would need to be
skipped. I get system beeps sometimes in this part, but I don't know
why! *)
if (button "OK" of window 1 of application process "AppleWorks 6" of application "System Events" exists) then
delay 0.5
keystroke return
delay 0.5
end if
delay 2
if (button "OK" of window 1 of application process "AppleWorks 6" of application "System Events" exists) then
delay 0.5
keystroke return
delay 0.5
end if
delay 2
(* If the document loads, the Appleworks welcome pane won't be there;
this part of the script works flawlessly, when it happens. Sometimes
documents are outside of print margins, hence the press-ok-button
conditional *)
if not (window "Starting Points" of application process "AppleWorks 6" of application "System Events" exists) then
tell application process "Appleworks 6" of application "System Events"
keystroke "p" using command down
delay 1
click menu button "PDF" of window "Print" of application process "AppleWorks 6" of application "System Events"
delay 1
click menu item "Save as PDF…" of menu "PDF" of menu button "PDF" of window "Print" of application process "AppleWorks 6" of application "System Events"
delay 1
keystroke "v" using command down
click button "Save" of window "Save" of application process "AppleWorks 6" of application "System Events"
delay 8
keystroke "w" using command down
delay 0.5
if (button 1 of window 1 of application process "AppleWorks 6" of application "System Events" exists) then
delay 0.5
keystroke "d" using command down
delay 0.5
end if
delay 0.5
end tell
end if
do shell script "killall 'LaunchCFMApp'"
delay 3
end tell
end tell
end try
end if
end repeat
end tell
我想让这个婴儿在一个周末只运行数千个文件并创建PDF,但每次运行一夜,我都会发现数百个正确处理的文档,数百或数千个跳过的文档,通常还有脚本本身的打印对话,这显然来自于在Appleworks上下文之外使用Command+P。我是一个苹果书呆子,可以肯定,这几个星期来一直让我发疯 这并不是你问题的真正解决方案,因为它非常具体和量身定制,但我有一些可能有用的提示:
- 您不需要关闭“起点”窗口。当您告诉AppleWorks打开文档时,它将被取消
- 尽量少用模拟按键。例如,与其模拟
打开打印对话框,不如模拟在其cmd+P
菜单中单击应用程序的文件
命令:Print…
与执行click menu item "Print…" of menu "File" of menu bar 1 of application process "AppleWorks 6"
关闭文档不同,正确的方法是cmd+W
tell application "AppleWorks 6" to close front document saving no
- 如果您确实需要模拟击键,请确保它们被正确的应用程序接收,方法是在
命令之前,使用击键
,以防此时它不是最前面的应用程序告诉应用程序“AppleWorks 6”激活
- 在检查其他对话框窗口时,如果((*…*)窗口1的按钮1存在)使用
是一个坏主意
,因为在我的机器上,例如,在“打印”对话框打开时检查此选项会导致脚本挂起数分钟(可能是永久挂起),而且,因为通常建议按名称而不是数字来称呼窗口(因为
只是最前面的窗口,很快就会变成另一个窗口)。因此,最好检查是否存在名为某某的窗口window 1
- 为了处理数千个可能发生错误的文件,您需要重新构造代码并重新考虑错误处理。例如,您可以将错误信息记录到文本文件中,以便查看哪些文件被跳过以及为什么被跳过。要将程序逻辑与错误处理和细节分开,可以使用处理程序,因此主循环可以如下所示:
同样,您也可以记录成功消息。然后,在单个处理程序中,您应该指定关于如何准确执行每个步骤的详细信息。它们还可以提供一些要记录的错误信息:set logfile to alias "Macintosh HD:Users:user:Desktop:errors.log" open for access logfile with write permission repeat with the_doc in folderItems try open_with_appleworks(the_doc) print_to_pdf() close_document_and_quit_appleworks() on error error_message write error_message & "\n" to logfile close_document_and_quit_appleworks() end try end repeat close access logfile
to open_with_appleworks(the_doc) tell app "AppleWorks 6" try (* do your stuff here *) on error err_msg (* re-signal the error to main loop! *) error "Failed to open " & (the_doc as text) & err_msg end try end tell end open_with_appleworks
- 硬编码延迟会大大降低脚本的速度:虽然只有部分文档可能需要几秒钟才能打开,但脚本将始终等待5秒钟,即使文档是在瞬间打开的。最好使用循环检查文档是否已打开:
几乎所有其他延迟都是一样的,大多数延迟都是可以避免的,因为它们在实践中会更短或更长。与其拖延半秒钟,希望警告或对话框在那时出现,不如检查它是否存在repeat while not (exists front document) -- just wait -- or, delay 0.1 -- if you really want to end repeat
- (最后一个提示,然后我会保持沉默)不幸的是,您似乎已经经历了将所有要处理的文件复制到一个文件夹的麻烦,还记得它们来自于某个“path.txt”。。。如果文件位于某个文件夹的子文件夹中,并且可以通过过滤器找到,则可以避免此:您只需使用该特定搜索()创建一个“smsart文件夹”,然后在AppleScript
,然后选择该特定智能文件夹中的所有文件,你很好。然后,脚本可以通过询问文件中设置以选择允许多个选择的文件
以应用程序“Finder”
获取f的父级
(这将返回一个Finder
引用,该引用可以轻松地转换为文件
和别名
地址)来找到POSIX文件
的位置。然后,由于您知道通过“打印”文档生成的PDF文件的名称和位置,因此可以将该PDF移动到其源文档所在的文件夹中文件f
说明:“dialog”
,您可以稍后检查该属性(例如存在窗口1,窗口1的说明是“dialog”
)