function - AppleScript语法错误

标签 function applescript syntax-error

我试图使用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/

相关文章:

JavaScript 删除动态创建的 tr

user-interface - 在 AppleScript 中选择弹出菜单按钮

PHP 限制 session var sql 错误

applescript - iTerm applescript 在两个单独的垂直 Pane 中写入

objective-c - Applescript 和 Cocoa 窗口位置

sql - SQL语句出了点问题

mysql - mySQL 查询函数将十进制等级转换为字母等级的语法错误

sql-server - 传递 SQL 函数数据库名称

javascript - Windows 8 Javascript 异步编程

javascript - 在javascript中确定调用函数