如何使用 AppleScript 将对象记录到控制台 [英] How to log objects to a console with AppleScript

查看:47
本文介绍了如何使用 AppleScript 将对象记录到控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想在我的 AppleScript 的整个生命周期中记录对象的状态.在其他语言中,对象的 toString() 方法将呈现等效的文本,我可以使用它们.在 AppleScript 中,情况似乎并非如此.

I am just trying to log the state of objects throughout the life of my applescript. In other languages, object's toString() methods will render the text equivalent and I can use those. In AppleScript, this does not seem to be the case.

convert applescript object to string (similar to toString)

将 finder 对象(及其属性)输出到 AppleScript 编辑器的结果"窗口,但前提是它是执行的最后一条语句.

Will output the finder object (and its properties) to the "Results" window of AppleScript Editor, but only if it is the last statement which gets executed.

如果我有一个 trace() 语句(它需要一条消息用于日志记录):

If I have a trace() statement (which takes a message for logging purposes):

on trace(message)
do shell script "cat >>~/log/applescript.txt <<END_OF_THE_LOG
" & (message as text) & "
END_OF_THE_LOG"
end trace

并尝试记录相同的对象,我得到

and try to log the same object, I get

Can’t make properties of application "Finder" into type text.

我对登录控制台的更好方法持开放态度,但想了解如何在脚本中间编写对象的属性(如 AppleScript 编辑器所做的那样)以进行测试.

I'm open to better ways of logging to a console, but would like to find out how to write an object's properties (like AppleScript Editor does) in the middle of a script for testing either way.

推荐答案

只需在 AppleScript 编辑器中使用 log 语句即可.当您在 Applescript 编辑器中查看结果时,在窗口底部按事件"按钮.通常按下结果"按钮,然后您只能看到您提到的最后一条语句的结果.因此,将按钮更改为事件".这将向您展示脚本运行时发生的所有事情,以及您在整个代码中放置的所有 log 语句.请注意,log 语句不必是文本.您可以记录任何对象.

Just use the log statement in AppleScript Editor. When you view the results in Applescript Editor, at the bottom of the window press the "Events" button. Normally the "Results" button is pressed and then you only see the result of the last statement as you mention. So change the button to "Events". This will show you everything that is happening as the script runs, and additionally all of the log statements that you put throughout the code too. Note that the log statements do not have to be text. You can log any object.

这是调试脚本并查看发生了什么的最佳方式.作为一个例子,试试这个并查看事件".如果您查看事件,实际上认为您不需要很多 log 语句,因为所有内容都已记录!

This is the best way to debug your script and see what is happening. As an example try this and look at the "Events". Realistically thought you don't need a lot of log statements if you view the Events because everything is already logged!

set someFolder to path to desktop
log someFolder

tell application "Finder"
    set someItem to first item of someFolder
    log someItem

    set itemProps to properties of someItem
    log itemProps
end tell

这篇关于如何使用 AppleScript 将对象记录到控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆