Changing Apple Script to Python for Refeshing Image URL

Posted on
Sun Oct 04, 2015 10:34 am
pacman offline
Posts: 67
Joined: Sep 17, 2008
Location: Pacifica, CA

Changing Apple Script to Python for Refeshing Image URL

I posted this in another thread discussing Refreshing Image URL from Variable...

It took awhile to figure this out. Some information is missing regarding how to go from the Sonos plug-in inserting the URL (ZP_Art state) into a variable to getting the variable (ZP_Art URL) to update the "Refreshing Image URL" element on a web page. I used this simple AS(Apple Script):

tell application "IndigoServer"
set _Source to value of variable "SonosArt" as text
--SonosArt is the variable name
end tell
tell application "Finder"
do shell script "curl _Source -o
file:///Library/Application%20Support/Perceptive%20Automation/images/Sonos/Library_art.jpg"
--Library_art.jpg is the target file
end tell

I use a trigger to run the above script every time Sonos changes tracks (Device State Change)


The AS works embedded in a trigger but I have had problems with embedded AS in the past. Is there anyone who could show me the Python script to accomplish the same thing? I understand embedded Python script is less harmful than an embedded AS. Sorry for the redundant posts but I have spent hours trying to figure this out. Thank you.

Posted on
Sun Oct 04, 2015 5:43 pm
kw123 offline
User avatar
Posts: 8363
Joined: May 12, 2013
Location: Dallas, TX

Re: Changing Apple Script to Python for Refeshing Image URL

Code: Select all
import subprocess
_Source= indigo.variables["SonosArt"].value
subprocess.Popen("/usr/bin/curl "+_Source+"  -o file:///Library/Application%20Support/Perceptive%20Automation/images/Sonos/Library_art.jpg" ,shell=True)

have not tested it, but it compiles and should do the trick.

Karl

ps the /usr/bin/ is not needed, but good practice as in some environments /usr/bin/ is not included in the search path

Posted on
Mon Oct 05, 2015 7:54 am
pacman offline
Posts: 67
Joined: Sep 17, 2008
Location: Pacifica, CA

Re: Changing Apple Script to Python for Refeshing Image URL

Thank you very much for your help. Progress! Your script does compile with no errors but does not update file. I created a target test file:

import subprocess
_Source= indigo.variables[1616152257].value
subprocess.Popen("/usr/bin/curl "+_Source+" -o file:///Library/Application%20Support/Perceptive%20Automation/images/Sonos/Test_art.jpg", shell=True)

Posted on
Mon Oct 05, 2015 9:20 am
jay (support) offline
Site Admin
User avatar
Posts: 18219
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Changing Apple Script to Python for Refeshing Image URL

I'm not sure you can use the file:// url in curl. Try just:

Code: Select all
/Library/Application%20Support/Perceptive%20Automation/images/Sonos/Test_art.jpg


as the output (-o) option.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Oct 05, 2015 7:35 pm
pacman offline
Posts: 67
Joined: Sep 17, 2008
Location: Pacifica, CA

Re: Changing Apple Script to Python for Refeshing Image URL

Nope! Still won't write to Test_art.jpg. Compiles without error. I'll keep poking around. The embedded AS works and I can live with it. Next option is an AS external to IndigoServer called by an Action Group. This does present sort of the same problem because the shell script doesn'f read the variable _Source. Still open to sugestions. Thank you.

Posted on
Tue Oct 06, 2015 9:16 pm
kw123 offline
User avatar
Posts: 8363
Joined: May 12, 2013
Location: Dallas, TX

Re: Changing Apple Script to Python for Refeshing Image URL

what do you actually want to do? copy the contents of the variable into a file?
what is in _Source / SonosArt , could you give an example?

if you want to redirect the output to a file:
Code: Select all
subprocess.Popen("/usr/bin/curl  '"+_Source+"'  >   '/Library/Application Support/Perceptive Automation/images/Sonos/Test_art.jpg'", shell=True)

replaced the %20 by encapsulating the path in quotes 'x x y ' alto for _Source in case it contains spaces.

Karl

Posted on
Sat Oct 10, 2015 12:12 pm
pacman offline
Posts: 67
Joined: Sep 17, 2008
Location: Pacifica, CA

Re: Changing Apple Script to Python for Refeshing Image URL

Karl,

_Source is a script variable to place the contents of an Indigo Variable (IV). Sonos is a music player/system that has an IndigoServer plug-in to integrate actions and data into my Control Pages. Indigo can save the URL path of the album art of a music track ( image file) to an IV triggered when the track changes. The refreshing URL Element on a Control Page will not update from an IV so I am trying to grab the referenced image file from the URL saved in the IV and save/replace the image file referenced by the Refreshing URL Control Page element. Make sense?

The embedded Apple Script does work and I am not sure if I need to change the way I do things except that embedded AS can be an issue. I have almost all my AS in an external folder and called by an Action Group. Those I keep within the Indigo structure are mostly if-then logic choices executing Action Groups and I have learned to use Python script replacement but I know AS much better than Python.

Thanks for your help. I like to learn and it is usually from other people.

namaste

Posted on
Sat Oct 10, 2015 10:42 pm
pacman offline
Posts: 67
Joined: Sep 17, 2008
Location: Pacifica, CA

Re: Changing Apple Script to Python for Refeshing Image URL

This is what the reference URL looks like:
http://cont-1.p-cdn.com/images/public/r ... W_500H.jpg
Here is the target file path within Refreshing URL:
file:///Library/Application%20Support/Perceptive%20Automation/images/Sonos/Library_art.jpg

Screenshot of iPhone Control Page attached...
Attachments
IMG_4018.PNG
IMG_4018.PNG (126.22 KiB) Viewed 4213 times

Posted on
Sun Oct 11, 2015 10:00 am
kw123 offline
User avatar
Posts: 8363
Joined: May 12, 2013
Location: Dallas, TX

Re: Changing Apple Script to Python for Refeshing Image URL

could you try:

Code: Select all
cmd="/usr/bin/curl  '"+_Source+"'"
indigo.server.log(cmd)
text= subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE).communicate()
indigo.server.log(unicode(text))


that should give you the response from the curl statement and also any error message

Karl

Posted on
Sun Oct 11, 2015 2:49 pm
pacman offline
Posts: 67
Joined: Sep 17, 2008
Location: Pacifica, CA

Re: Changing Apple Script to Python for Refeshing Image URL

I am sorry, I don't understand!

how do I state the source variable?
_Source= indigo.variables[1616152257].value

and the target
/Library/Application Support/Perceptive Automation/images/Sonos/Test_art.jpg

into your script
cmd="/usr/bin/curl '"+_Source+"'"
indigo.server.log(cmd)
text= subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE).communicate()
indigo.server.log(unicode(text))

You are talking to a real novice here. Thanks again.

Posted on
Sun Oct 11, 2015 3:13 pm
kw123 offline
User avatar
Posts: 8363
Joined: May 12, 2013
Location: Dallas, TX

Re: Changing Apple Script to Python for Refeshing Image URL

Code: Select all
_Source= indigo.variables[1616152257].value
cmd="/usr/bin/curl  '"+_Source+"'"
indigo.server.log(cmd)
text= subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE).communicate()
indigo.server.log(unicode(text))


this is a test to (a) show the value of _Source and (b) the response from curl
it will not change the file /Library/Application Support/Perceptive Automation/images/Sonos/Test_art.jpg


Karl

Posted on
Sun Oct 11, 2015 8:36 pm
pacman offline
Posts: 67
Joined: Sep 17, 2008
Location: Pacifica, CA

Re: Changing Apple Script to Python for Refeshing Image URL

Karl,

Your script to test:

_Source= indigo.variables[1616152257].value
cmd="/usr/bin/curl '"+_Source+"'"
indigo.server.log(cmd)
text= subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE).communicate()
indigo.server.log(unicode(text))


Here is the log text after executing:

Oct 11, 2015, 7:33:25 PM
Script /usr/bin/curl 'http://cont-sjl-1.pandora.com/images/public/amz/4/2/2/7/731452547224_500W_500H.jpg'
Script Error embedded script: global name 'subprocess' is not defined
Script Error Exception Traceback (most recent call shown last):

embedded script, line 4, at top level
NameError: global name 'subprocess' is not defined

Posted on
Sun Oct 11, 2015 9:27 pm
pacman offline
Posts: 67
Joined: Sep 17, 2008
Location: Pacifica, CA

Re: Changing Apple Script to Python for Refeshing Image URL

BINGO!

import subprocess
_Source=indigo.variables[1850137102].value
subprocess.Popen("/usr/bin/curl '"+_Source+"' > '/Library/Application Support/Perceptive Automation/images/Sonos/Test_art.jpg'", shell=True)


Note: variable ID different than before as I created a second IV for this test. I tried to have two Refreshing URL elements on same page, one using "Library_art.jpg" and the other "Test_art.jpg" and I had a weird delay. I created second Control Page to test and the Python script works.

Thanks so much for your help. Hopefully this post thread helps others.

Posted on
Mon Oct 12, 2015 4:48 am
DaveL17 offline
User avatar
Posts: 6753
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Changing Apple Script to Python for Refeshing Image URL

I'm a bit confused. If you're doing this to show the album art of the song that's playing on an Indigo control page, why not just use the refreshing image URL entered right into the control page editor?

Code: Select all
file:///Library/Application%20Support/Perceptive%20Automation/images/Sonos/kitchen_art.jpg

There is clearly something I don't understand about why you need to put the URL into a variable.
Dave

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

[My Plugins] - [My Forums]

Posted on
Mon Oct 12, 2015 8:31 am
pacman offline
Posts: 67
Joined: Sep 17, 2008
Location: Pacifica, CA

Re: Changing Apple Script to Python for Refeshing Image URL

Refreshing URL page element does not update from an Indigo Variable.

Sonos Actions does not export current tract "Art.jpg" to a disk location (I don't see how this is done if you can). I am able to use the Indigo Variable Action : Insert Device State into a Variable to grab the referenced image file (URL) and then execute our Python script to update the image file reference in the Refreshing URL element.
Attachments
SS.jpg
SS.jpg (270.46 KiB) Viewed 4032 times

Who is online

Users browsing this forum: No registered users and 5 guests