AppleScript的on message received块报错,请求排查解决
问题解决:AppleScript Messages事件处理参数错误
错误原因
你遇到的错误是因为Messages应用的message received事件处理程序的参数语法不符合AppleScript规范。AppleScript中,应用事件的自定义参数需要通过given关键字声明命名参数,不能直接用from、for这类标识符作为参数前缀。另外代码里的False需要改为AppleScript标准的小写false。
修正后的完整代码
on write_to_file(this_data, target_file, append_data) -- (string, file path as string, boolean) try set the target_file to the target_file as text set the open_target_file to ¬ open for access file target_file with write permission if append_data is false then ¬ set eof of the open_target_file to 0 write this_data to the open_target_file starting at eof close access the open_target_file return true on error try close access file target_file end try return false end try end write_to_file using terms from application "Messages" on message received theMessage given sender:theBuddy, chat:theChat write_to_file(theMessage, "message.txt", false) write_to_file(theBuddy, "person.txt", false) return end message received end using terms from application "Messages"
关键修改点
- 将
on message received theMessage from theBuddy for theChat改为on message received theMessage given sender:theBuddy, chat:theChat,符合AppleScript应用事件的参数声明规则 - 将
False改为小写的false,匹配AppleScript的布尔值语法规范
内容的提问来源于stack exchange,提问作者A random person
相关产品推荐
相关产品推荐

