Import Python variable into Applescript

Posted on
Sun Apr 22, 2018 6:51 pm
Monstergerm offline
Posts: 274
Joined: Sep 01, 2009

Import Python variable into Applescript

I want to import the value of a python variable into applescript using the py-applescript module.

In particular I have a python variable speech = "Good Morning" that I want to use in applescript with the say speech command.

I know how to do this with :
import subprocess
speech = "Good Morning"
subprocess.call('say %s -v Alex -r 180' %(speech), shell=True)

What do I have to do to accomplish something like this?
import applescript
speech = "Good Morning"
my_apple_script = '''
say speech using Alex
'''
cmd = applescript.AppleScript(source=my_apple_script)
reply = cmd.run()

Posted on
Sun Apr 22, 2018 6:59 pm
Colorado4Wheeler offline
User avatar
Posts: 2794
Joined: Jul 20, 2009
Location: Colorado

Re: Import Python variable into Applescript

Try this:

Code: Select all
speechText = "Hello World"
indigo.server.speak(speechText)


And do away with AppleScript entirely in this instance.

My Modest Contributions to Indigo:

HomeKit Bridge | Device Extensions | Security Manager | LCD Creator | Room-O-Matic | Smart Dimmer | Scene Toggle | Powermiser | Homebridge Buddy

Check Them Out Here

Posted on
Mon Apr 23, 2018 11:35 am
Monstergerm offline
Posts: 274
Joined: Sep 01, 2009

Re: Import Python variable into Applescript (and using "say"

Great idea C4W, I didn't think about this.

But since we are on this topic about speech and speaking ...

I also need to play a system aif sound, which I currently accomplish with afplay. So, I don't think indigo.server.speak can do this. Right?

And what about selecting the voice and speech rate? Can this be added to the indigo.server.speak command? If not I still would have to go the subprocess.call route.

And what are the pros and cons of adding waitUntilDone=False or True?
As noted in other forum posts the 10 second limit for completing a speech command sometimes causes problems if the computer or Indigo Server is busy and takes longer to speak the text. Does the False or True setting help here?

Also, based on some limited conversions of my scripts from "true applescript speech scripts" to running the subprocess.call command, I find that the announcements are not as smooth, they are stuttering, letters are missing from words in the middle of the announcements, pauses are introduced.

Posted on
Tue Apr 24, 2018 10:40 am
jay (support) offline
Site Admin
User avatar
Posts: 18212
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Import Python variable into Applescript

I don't believe AppleScript allows you to specify voice, etc., does it?

In any event, to pass data from Python to an AppleScript, check out the Call Handlers section of our AppleScript integration guide. That will illustrate one way of doing it.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Tue Apr 24, 2018 2:16 pm
Monstergerm offline
Posts: 274
Joined: Sep 01, 2009

Re: Import Python variable into Applescript

Yes, Applescript allows you to select voice, speak rate, pitch, modulation, insert silence etc

I think all of this is not possible with indigo.server.speak(speechText).

I looked at the the Call Handlers section of the AppleScript integration guide as suggested by Jay. But I don't think this works for my purpose or I do not understand it correctly.

I have variables generated by a python script that I need to pass into an applescript that runs inside the python script (either via subprocess or py-applescript). I know how to pass the variable value via subprocess (see earlier post). But how do I do this with py-applescript?

As an example, one of the variables is speechText that I need to have spoken with complete control over voice, speak rate etc.

Posted on
Tue Apr 24, 2018 2:28 pm
Colorado4Wheeler offline
User avatar
Posts: 2794
Joined: Jul 20, 2009
Location: Colorado

Re: Import Python variable into Applescript

Just spitballing, but this should be pretty close.

Code: Select all
import applescript

textSpeech = indigo.variables[12345].value
ascript_string = '''
say "{}" using "Vicky"
'''.format(textSpeech)
script = applescript.AppleScript(source=ascript_string)
script.run()


[MODERATOR NOTE] fixed a couple of issues with the script.

My Modest Contributions to Indigo:

HomeKit Bridge | Device Extensions | Security Manager | LCD Creator | Room-O-Matic | Smart Dimmer | Scene Toggle | Powermiser | Homebridge Buddy

Check Them Out Here

Posted on
Tue Apr 24, 2018 3:50 pm
jay (support) offline
Site Admin
User avatar
Posts: 18212
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Import Python variable into Applescript

It's right there in the wiki article. Start with an AppleScript that defines some handlers like this:

Code: Select all
on saySomething(speechString)
    say speechString
end saySomething


You can either load it from a string or from a file (as shown in the example). If you load it from a string in the Python script, then you'd do this:

Code: Select all
import applescript
script_source='''on saySomething(speechString)
   say speechString
end saySomething
'''
script = applescript.AppleScript(source=script_string)
script.call("saySomething", indigo.variables[IDOFVARTOSPEAKHERE].value)


You could of course also add parameters for voice, volume, etc., if you want to pass those in.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 3 guests