Applescript Arrays?

Posted on
Tue Sep 13, 2016 3:43 am
Vangelis offline
Posts: 167
Joined: Mar 18, 2014
Location: Southampton (UK)

Applescript Arrays?

Was wondering if this is possible to do in AppleScript (happy if easier in Python?)

I want to have Indigo announce entry to my front door via Sonos. I have the trigger for door entry and know how to put a variable into Sonos, but cannot work out how the script it. Basically I was thinking along the lines of an array? whereby the array would contain strings of words that could be announced...

e.g. array = "Hello There,
Welcome Home
Hi there how was your day
Door open"

The script would generate a random number between 1 and 4, and have this number pick one of the four array responses, this would then be announced by Sonos

So far I have this as my random number generator, but have seen an error whereby the Indigo Variable is read-only :(

property SonosAnnounce : 1
tell application "IndigoServer"
set value of variable SonosAnnounce to (random number from 1 to 4)
end tell

Any ideas?

Vangelis

Posted on
Tue Sep 13, 2016 5:53 am
DaveL17 offline
User avatar
Posts: 6744
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Applescript Arrays?

I'm not able to do the Sonos part from here, but the random part is super simple in Python:

Code: Select all
from random import randint

messages = ["Hello There",
            "Welcome Home",
            "Hi there how was your day",
            "Door open"
            ]

x = randint(0, len(messages)-1)

print messages[x]

As far as I know, Python doesn't have a native array structure, but a list will work quite well for your purpose. List elements are accessed by their 'index' -- in this case, 0 through 3 (4 items total; the first element in a list is the 'zeroth' element.) As you add strings to the list (paying attention to how list items are delineated) you don't have to worry about changing the random number generator as its upper boundary is automatically set by the number of elements in the list (minus 1, which allows you to include the zeroth element.) In other words, if we don't subtract 1, the generator would eventually call the Fifth Element which is busy saving the Universe.

Hope this helps.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Tue Sep 13, 2016 7:37 am
Vangelis offline
Posts: 167
Joined: Mar 18, 2014
Location: Southampton (UK)

Re: Applescript Arrays?

Cool - Thanks DaveL17 :D

My next question - in Python - how would I write that message[x] variable to one already created in Indigo? (I was getting a read-only issue before)

Vangelis

Posted on
Tue Sep 13, 2016 1:10 pm
jay (support) offline
Site Admin
User avatar
Posts: 18200
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Applescript Arrays?

Arrays vs Lists vs Sets, oh my!

The traditional definition of an Array is much more like what you see in C - fixed sized items in a fix sized list. Python lists (like C++ and Objective C arrays) are different in that they can contain anything and are mutable (size can easily change). Python also has a tuple which is an immutable list (basically) and so is more space efficient. Python also has sets which are faster at testing membership but slower to iterate over.

Short version: they're all basically the same thing... :lol:

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Tue Sep 13, 2016 1:13 pm
jay (support) offline
Site Admin
User avatar
Posts: 18200
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Applescript Arrays?

Vangelis wrote:
My next question - in Python - how would I write that message[x] variable to one already created in Indigo? (I was getting a read-only issue before)


Code: Select all
indigo.variable.updateValue(12345, value=messages[x]) # 12345 is the ID of the Indigo variable

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Wed Sep 14, 2016 1:50 am
rhanson offline
Posts: 192
Joined: Apr 30, 2013

Re: Applescript Arrays?

Here's the no-math option:

Code: Select all
import random

messages = ["Hello There",
            "Welcome Home",
            "Hi there how was your day",
            "Door open"
            ]

indigo.variable.updateValue(12345, value=random.choice(messages))

Posted on
Wed Sep 14, 2016 2:53 am
Vangelis offline
Posts: 167
Joined: Mar 18, 2014
Location: Southampton (UK)

Re: Applescript Arrays?

Added the following script :

def messages():

from random import randint

messages = ["Hello There",
"Welcome Home",
"Hi there how was your day",
"Door open"
]
def randint():
x = randint(0, len(messages)-1)
def x():
indigo.variable.updateValue(671930218, value=messages[x])

I had to add the 'def' text in order to define the Global Variables (Indigo compiler kept complaining) Although it compiles and runs, I see no update to the Indigo Variable (ID).

Any thoughts?

Vangelis

Posted on
Wed Sep 14, 2016 3:59 am
DaveL17 offline
User avatar
Posts: 6744
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Applescript Arrays?

There should be no need to add any methods to the code (unless it's part of a much larger script.) Did you per chance copy and paste the code into the Indigo embedded script window? Sometimes there are hidden characters that the compiler doesn't like. This code works for me when run as an embedded script:

Code: Select all
from random import randint

messages = ["Hello There",
            "Welcome Home",
            "Hi there how was your day",
            "Door open"
            ]

x = randint(0, len(messages)-1)

indigo.variable.updateValue(671930218, value=messages[x])

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Wed Sep 14, 2016 4:42 am
DaveL17 offline
User avatar
Posts: 6744
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Applescript Arrays?

Almost forgot the Sonos part. You probably have this figured out already, but here's one way to do it.

Create a trigger that fires whenever your messages variable changes.
Add any conditions that you might want.
Create a Sonos Group Announcement, and in the message field use variable substitution:

Code: Select all
%%v:671930218%%

The group announcement field will speak both the variable value (i.e., "Welcome home.") but also any static text that you add as well.

Code: Select all
%%v:671930218%% Vangelis. It's good to see you again.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Wed Sep 14, 2016 8:11 am
Vangelis offline
Posts: 167
Joined: Mar 18, 2014
Location: Southampton (UK)

Re: Applescript Arrays?

Yay - That worked (must have been corruption somewhere in the cut'n'paste)

Thanks all

Vangelis

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 8 guests