user3831357
user3831357

Reputation: 149

Automator - Processing of "Choose from List" output

I'm trying to automate some processes with automator, but I'm unfamiliar with shell scripts or apple scripts and I need a little more complex functionality than the built in Automator functions. Here's what I'm trying to do:

A user selects from a list of options with the "Choose from List" command. The "Choose from List" command then passes along stdin the chosen objects in the following format (as text):

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

I would like to pass each one of the parameters (Each of the Wemo Lights) to a shell script or an apple script and then run the binary '/usr/local/bin/wemo switch "$STRING" off' where $STRING is the passed name for each light.

This is how you would do it in 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");
}

(But PHP won't help me, it has to be another shell script or apple script!) Any advice and help is appreciated!

Edit: the stdin is actually in applescript list format. So applescript seems like the natural choice. Still researching...

Upvotes: 2

Views: 551

Answers (1)

adayzdone
adayzdone

Reputation: 11238

Like this?

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

Upvotes: 0

Related Questions