简体   繁体   English

如何调试 AppleScript?

[英]How do I debug AppleScript?

What tips and tricks do you have for debugging AppleScript?调试 AppleScript 有哪些技巧? Is there a debugger?有调试器吗? If not, what is the best way to insert "prints" to display the value of variables?如果不是,插入“打印”以显示变量值的最佳方法是什么? Is there a way to "pretty print" more complicated data structures?有没有办法“漂亮地打印”更复杂的数据结构?

对于非常简单的“printf 风格”调试需求,请尝试

display dialog "my variable: " & myVar
  1. Script Debugger脚本调试器
  2. XCode代码
  3. Getting the properties of an object (see below) to understand why it fails, when run from script editor.从脚本编辑器运行时,获取对象的属性(见下文)以了解它失败的原因。 You can also use the class word, to see what class a property is.您还可以使用类词来查看属性是什么类。 The Dictionary for an app is a good starting point.应用程序的字典是一个很好的起点。

    One technique that often would have helped me, (and that I still sometimes do) is to tell something to return their properties, like this:一种经常对我有帮助的技术(我有时仍然这样做)是告诉某些东西返回它们的属性,如下所示:

     tell application "TextEdit" get properties end tell
  4. Log statements and Console.app, for debugging of runtime events.日志语句和 Console.app,用于调试运行时事件。 (further below). (进一步如下)。 You can ofcourse turn debugging on and off by setting a property您当然可以通过设置属性来打开和关闭调试

    Below is a techniuqe I use for tracking runtime errors, in applets, mail rules, and what have you.下面是我用来跟踪运行时错误、小程序、邮件规则和你有什么的技术。 When it fails, the error number and message is logged into TestDrive.log, and can be found in the left margin of Console.app…当它失败时,错误号和消息会记录到TestDrive.log中,可以在Console.app的左边距中找到……

     tell application "TextEdit" try set a to text 0 of its name on error e number n my logit("OOPs: " & e & " " & n, "TestDrive") end try end tell to logit(log_string, log_file) do shell script ¬ "echo `date '+%Y-%m-%d %T: '`\\"" & log_string & ¬ "\\" >> $HOME/Library/Logs/" & log_file & ".log" end logit

If you're building any amount of AppleScripts, ScriptDebugger is the best tool I can recommend.如果您正在构建任何数量的 AppleScript, ScriptDebugger是我推荐的最佳工具。 Having said that...话说回来...

Xcode is a free option that can be used to develop AppleScripts and can step through code with the debugger. Xcode是一个免费选项,可用于开发 AppleScripts,并且可以使用调试器单步调试代码。 The ability is primarily included so you can build Cocoa applications with AppleScript Studio , but you could use it for any AppleScript development.该功能主要包括在内,以便您可以使用AppleScript Studio构建 Cocoa 应用程序,但您可以将其用于任何 AppleScript 开发。

If you're looking for something simpler, you might check out Smile , which isn't really a debugger, but does offer features useful for debugging that aren't available in the standard Script Editor.如果您正在寻找更简单的东西,您可以查看Smile ,它并不是真正的调试器,但确实提供了对调试有用的功能,而这些功能在标准脚本编辑器中不可用。

If a display dialog is too small you can use TextEdit to show big returns:如果显示对话框太小,您可以使用 TextEdit 来显示大回报:

tell application "TextEdit"
    activate
    make new document
    set text of document 1 to myResults
end tell

Source http://forums.macrumors.com/showthread.php?t=446171来源http://forums.macrumors.com/showthread.php?t=446171

To get the names of windows and other GUI elements and properties, I've found UI Browser invaluable.为了获得窗口和其他 GUI 元素和属性的名称,我发现UI 浏览器非常有用。 You can use it to inspect whatever you want to control with AppleScript to find the designations of the elements you want to control你可以用它来检查你想用 AppleScript 控制的任何东西,以找到你想控制的元素的名称

Not free, but easily worth it for a serious developer.不是免费的,但对于认真的开发人员来说很值得。

Use the log command.使用log命令。 Example:例子:

log "Hello world!"

The output can then be seen in the "Messages" windows in the official editor.然后在官方编辑器的“消息”windows中可以看到output。

Reference 参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM