我试图使用AppleScript从Mac应用程序Things中输出待办事项列表。
但是我收到语法错误:
预期的表达,但找到“to”。
由于Things使用名称“to dos”而AppleScript不喜欢此名称,因为to
是保留关键字。如果重复代码直接在tell
语句内部而不在函数处理程序中,则它可以正常工作。
有没有办法解决?
set output to ""
on getTodos(listName)
repeat with todo in to dos of list listName
set todoName to the name of todo
set output to output & todoName
end repeat
end getTodos
tell application "Things"
getTodos("Inbox")
getTodos("Today")
end tell
这样有可能吗?
还有更好的方法吗?
最佳答案
当然,这很容易做到。问题在于,在tell application "Things" ... end tell
块之外,AppleScript不知道其中有什么特别的东西,甚至都看不到。然后,您需要做的就是在tell
中移动on getTodos(listName) ... end getTodos
块:
on getTodos(listName)
tell application "Things"
repeat with todo in to dos of list "Inbox"
set todoName to the name of todo
set output to output & todoName
end repeat
end tell
end getTodos
您也许还可以将
tell
替换为using terms from
;我认为应该可以。另外,您永远不会使用listName
-您是要用"Inbox"
替换listName
吗?但是,您应该能够用单行替换
getTodos
on getTodos(listName)
tell application "Things" to get the name of the to dos of list listName
end getTodos
这种快捷方式是AppleScript擅长的事情之一。另外,请注意,此新版本不会修改
output
,而只会返回列表;无论如何,我认为这是一个更好的决定,但是您始终可以执行set output to output & ...
。
关于function - AppleScript语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4217714/