代码之家  ›  专栏  ›  技术社区  ›  Sindre Sorhus

AppleScript语法错误

  •  1
  • Sindre Sorhus  · 技术社区  · 15 年前

    我尝试使用applescript从mac应用程序输出一个todo项目列表。

    但我得到一个语法错误: 应为表达式,但找到了__到__。

    因为事物使用“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
    

    这样做有可能吗?
    还有更好的方法吗?

    2 回复  |  直到 15 年前
        1
  •  4
  •   Antal Spector-Zabusky    15 年前

    当然,这是很容易做到的。问题是外面 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
    

    您也可以更换 告诉 具有 using terms from 我认为这应该管用。而且,你从不使用 listName _ "Inbox" 具有 列表名 ?

    但是,您应该能够替换 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 & ... .

        2
  •  0
  •   Jeremy W. Sherman    15 年前

    我认为问题在于,AppleScript不知道您正在尝试在处理程序中使用特定的术语。试着把 repeat 在一个 using terms from application Things 块,像这样:

    on getTodos(listName)
        using terms from 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 using terms from
    end getTodos
    
    推荐文章