我想打开另一个单独的应用程序,以迭代方式打开该应用程序的项目/文档,然后关闭该应用程序。我还想关闭在打开文档期间弹出的所有模态和非模态对话框。我想关闭所有对话框,包括崩溃对话框,以防应用程序失败/崩溃。
使用可可或苹果脚本来实现这一目标的最佳方法是什么,我可以从哪里获得更详细的信息?
我想打开另一个单独的应用程序,以迭代方式打开该应用程序的项目/文档,然后关闭该应用程序。我还想关闭在打开文档期间弹出的所有模态和非模态对话框。我想关闭所有对话框,包括崩溃对话框,以防应用程序失败/崩溃。
使用可可或苹果脚本来实现这一目标的最佳方法是什么,我可以从哪里获得更详细的信息?
If the app has a scripting interface, of course the best way is to do that.
You generally don't want to iterate in AppleScript, but rather to operate on all of the results of a query.
For example, for almost any application that implements the "standard suite", you can just:
tell app "TextEdit" to close windows
This is much simpler (and faster, and more likely to be implemented correctly in the target app) than:
tell app "TextEdit"
repeat with theWindow in windows
close theWindow
end repeat
end tell
Of course this may pop up save/abandon changes dialogs, and it may skip over or include dialogs and inspectors, and so on, depending on the application's user model.
More importantly, it won't work if the app doesn't support scripting (and the standard suite).
Also, it won't help at all with closing a crash report—that window is owned by CrashReporter, not the original application (which is a good thing, because you can't talk to the original application anymore, now that it's crashed…).
The alternative is the UI Scripting features in System Events. This will only work if assistive access is enabled. It can also be a bit fiddly to figure out which windows are the ones you want to deal with, and which controls are the ones you want.
For example:
tell app "System Events"
click button 1 of windows of application process "TextEdit"
end tell
This works by finding every window (no matter what kind) owned by the TextEdit process, and simulating a click on the first button in that window (the red close button).
If you google "AppleScript UI Scripting" you should find lots of different guides. The first hit I found was http://www.makeuseof.com/tag/applescripts-ui-scripting-mac/ and it looks like a decent place to start.