【问题标题】:How do I pass labeled (or named) parameters to an existing AppleScript script file via the command line (using osascript)?如何通过命令行(使用 osascript)将标记(或命名)参数传递给现有的 AppleScript 脚本文件?
【发布时间】:2021-09-18 10:20:06
【问题描述】:

我的问题

我有一个名为 script.scpt 的 AppleScript 脚本文件。

这是我打算使用标记参数(AKA 命名参数)的脚本的第一行:

on run given foo:fooStr

我打开终端并尝试通过键入以下内容来运行脚本:

osascript "/Users/UserName/Library/Mobile Documents/com~apple~ScriptEditor2/Documents/script.scpt" given foo:"bar"

我得到了结果错误:

execution error: The foo parameter is missing for run. (-1701)

我已经尝试了我能想到的所有其他变体,用于在命令行中向脚本提供标记/命名参数,但没有成功。

我的问题

如何通过命令行将标记/命名参数传递给现有的 AppleScript 脚本文件?提前致谢。


背景

我之前使用以下行编写了脚本:

on run argv

然后以“典型”的方式进行操作,根据在命令行上提供时的预定顺序位置,在脚本中获取我需要的值:

set fooStr to item 1 of argv

命令行所在的位置:

osascript "/Users/UserName/Library/Mobile Documents/com~apple~ScriptEditor2/Documents/script.scpt" "bar"

【问题讨论】:

  • 你没有。 run 处理程序不像用户定义的处理程序 - 它是一个事件处理程序,它将参数作为单个列表。您可以像这样加载脚本并单独调用其运行处理程序,但不能从 shell 脚本中调用。

标签: macos terminal applescript named-parameters osascript


【解决方案1】:

使用标准的 *nix shell 约定并自己解析参数列表中的任何选项。

简单的手卷示例:

property _syntax : "cmd [-A] [-B VALUE] [-h] [-] [ARG ...]"

on run argv
    -- parse options
    set opt_A to false
    set opt_B to ""
    considering case
        repeat while argv is not {} and argv's first item begins with "-"
            set flag to argv's first item
            set argv to rest of argv
            if flag is "-A" then
                set opt_A to true
            else if flag is "-B" then
                set opt_B to argv's first item
                set argv to rest of argv
            else if flag is "-h" then
                log _syntax
                return
            else if flag is "-" then
                exit repeat
            else
                error "Unknown option: " & flag
            end if
        end repeat
    end considering
    -- do any validation here
    doStuff(opt_A, opt_B, argv)
end run

to doStuff(foo, bar, baz)
    -- do work here
end doStuff

(如果您的选项特别复杂,或者您编写了很多这样的脚本,您可能更喜欢使用现有的选项解析器,例如通过 AppleScript-ObjC 的NSUserDefaults 的NSArgumentDomain,或@987654321 中的parse command line arguments @.)

【讨论】:

    【解决方案2】:

    run 处理程序与常规的用户定义处理程序略有不同 - 作为 event 处理程序,它将参数作为单个列表。您可以使用标记参数定义它,但是当某些东西执行脚本时,会使用命令行参数的标准 POSIX 约定。

    命令行比标记参数早了几十年;它使用选项开关代替。你也可以实现类似的东西,但这是否值得取决于你想要做什么。

    开关的替代方法是在安排参数后调用运行目标的处理程序(脚本的run 处理程序将像往常一样声明)。一种方法是将参数记录与默认值合并,然后按所需顺序传递键列表,例如:

    to runWithArgs given args:argRecord -- run shell script with arguments from merged records
        set defaults to {foo:"foo", bar:"bar", baz:"baz", another:"four"} -- or whatever
        if class of argRecord is not record then set argRecord to {}
        set merged to {foo, bar, baz, another} of (argRecord & defaults) -- the desired keys and order
        set arguments to ""
        repeat with anItem in merged
            set arguments to arguments & space & quoted form of (anItem as text)
        end repeat
        do shell script "echo" & arguments -- osascript, etc
    end runWithArgs
    
    runWithArgs given args:{whatever:"no, not this one", another:4, foo:"this is a test"}
    

    不使用命令行的替代方法是使用load script,并调用各个处理程序(您不一定需要使用run 处理程序)。例如:

    set scpt to load script "/path/to/foo.scpt"
    tell scpt to run given foo:"bar"
    tell scpt to someHandler given someLabel:"whatever"
    

    【讨论】:

    • “运行处理程序有点不同……作为事件处理程序,它将参数作为单个列表。” -- 这与run 作为事件处理程序无关;这只是最初的 AppleScript 工程师选择将参数传递给特定处理程序的方式。区别纯粹是概念上的:“命令”处理程序接收从脚本发送的 [action] 消息; “事件”处理程序接收发送到脚本的[通知]消息。无论哪种方式,实际的机制都是相同的。 (在这里无关紧要,因为 OP 询问如何将带标签的参数传递给 AppleScript 从 *nix shell。)
    猜你喜欢
    • 2012-06-19
    • 2017-04-20
    • 2013-10-11
    • 1970-01-01
    • 2011-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-17
    相关资源
    最近更新 更多