自动操作 - 处理"Choose from List"输出



我试图自动化一些过程与automator,但我不熟悉shell脚本或苹果脚本,我需要一点更复杂的功能比内置的automator功能。下面是我要做的:

用户使用"Choose from list"命令从选项列表中进行选择。然后,"从列表中选择"命令以以下格式(作为文本)将所选对象传递给stdin:

(
"Wemo Light 1",
"Wemo Light 2",
"Wemo Light 3",
"Wemo Light 4"
)

我想将每个参数(每个Wemo灯)传递给shell脚本或apple脚本,然后运行二进制'/usr/local/bin/Wemo switch "$STRING" off',其中$STRING是每个灯传递的名称。

在PHP中是这样做的:

$lights = fopen('php://stdin', 'r');
$lights = preg_grep(/"[A-Za-z0-9]+"/,$lights);
foreach ($lights as $light) {
   shell_exec("/usr/local/bin/wemo switch $light off");
}

(但是PHP不会帮助我,它必须是另一个shell脚本或apple脚本!)任何建议和帮助是感激的!

编辑:stdin实际上是applescript列表格式。因此,applescript似乎是一个自然的选择。还研究…

像这样?

set theChoices to {"Wemo Light 1", "Wemo Light 2", "Wemo Light 3", "Wemo Light 4"}
set userChoice to choose from list theChoices with title "Insert your Title" with prompt "Insert your prompt" with multiple selections allowed
if userChoice is false then
    error number -128
else
    repeat with aChoice in userChoice
        set myCommand to "/usr/local/bin/wemo switch " & quoted form of aChoice & " off"
        do shell script myCommand
    end repeat
end if

最新更新