View unanswered posts | View active topics It is currently Wed May 22, 2013 5:01 pm



Reply to topic  [ 11 posts ] 
 Some help with iTunes 
Author Message

Joined: Mar 19, 2006
Posts: 56
Post Some help with iTunes
My system is a headless Mini. It runs Indigo, Serial Bridge, and iTunes. The Mini has no attached speakers but uses the speakers attached to Apple TV as well as Airport Express units.

I need some help or hints as how to write the following scripts.

Using Airfoil I would like to send Indigo messages to the home speaker systems. To do this I need to interrupt or maybe just pause the itunes and save it's state, then issue the Indigo message, and then restart or continue the iTunes because the Airfoil cannot capture speakers connected to iTunes.

I also what to write embedded scripts that start an iTunes playlist and one that stops iTunes.

Thanks in advance.


Mon Oct 06, 2008 12:19 pm
Profile

Joined: Nov 11, 2006
Posts: 120
Location: Reno, NV
Post 
Hi dfiedler,
I'm just in the process of finalizing an Applescript that does some of what you are asking for with Airfoil and Indigo. I'm in the process of putting together instructions. Here is a list of commands that I currently have:

AF_Sync_Indigo() Updates Indigo's variables with Airfoil's settings
AF_SpeakersONoffTOGGLE(aSpeaker) Toggle named speaker's on/off status
AF_SpeakersON(aSpeaker) Turn named speaker on
AF_SpeakersOFF(aSpeaker) Turn named speaker off
AF_VolumeChange(aSpeaker, delta) Change named speaker's (not iTunes) volume by given amount (0 to 100)
AF_VolumeAbsoluteChange(aSpeaker, value) Change named speaker's (not iTunes) volume to absolute amount (0 to 100)
iTunesStartRandom(incomingPlaylist) Start playing specific itunes playlist at random location in playlist
SetTimer(delayname, delaytime, ASscriptCode) Start a timer with given name, time, and Applescript code to run when it expires. Good for turning speakers on for a set amount of time.
Speak(Text) Will speak this text to speakers if Indigo variable "Airfoil_voice_response" is "true" [default]
DevicePower(aSpeaker, on_off) Turn Indigo Device (e.g. appliancelinc) on/off when turning associated Airfoil speaker on or off.

Changing volume and starting playlists will cause a verbal response (e.g. "Volume up", "Volume at maximum")
Several of the commands accept "all" as speaker name, thereby acting on all speakers at once.

My script will set up all the indigo variables it needs when it runs. All you have to do is manually specify a linkage between the Airfoil speaker name, a common name you'd like to call it (e.g. Living room speaker), and Indigo device name (if needed, not required). The neat thing is that when you specify a speaker name to act on, you can either use its Airfoil name, your user defined name, or its Indigo device name -- they are all equivalent. I needed to do this as I'm not smart enough to make life easy and name my bedroom airport express as "Bedroom".

If you or anyone else would like to help me test it, please pm me. The script works, I've just been tweaking it. I'd like to test this script with some others before I publicly post it.

Also, by Indigo message, I'm assuming you would like some variable of Indigo's spoken over the speakers?

You might also want to look at the iTunes Attachment in the Script Libraryhttp://perceptiveautomation.com/filelib/index.php. It provides some good basic functionality to start/stop, change volume, etc in iTunes.

Rick


Mon Oct 06, 2008 2:23 pm
Profile
Site Admin
User avatar

Joined: Mar 19, 2008
Posts: 6659
Location: Austin, Texas
Post 
I also have a library of miscellaneous Indigo functions, some of which extend the current iTunes Attachment Script. I worked on these before I started working with Matt directly so they're not (yet) part of the official script release. Function which you'd find useful:

pauseiTunesAndSay("Speak this message")

This function would pause iTunes (if it's running and if it's currently playing), speak the message passed in using the system default voice, then start iTunes playing again. My house uses this frequently during the day to speak timers (which are also in the library), iCal event notifications, and incoming caller names (via the PhoneValet).

Rather than try to manage Airfoil's speakers directly, I simply have all the sound output of my server directed through Airfoil to my standard set of speakers. I've thought about changing that so that it's a little more selective about what sound plays through the house, but since the server is just that, a server, it never really plays a sound that I don't want to hear... :)

I'll post the script up in the User Contribution Library later today and post on this thread when I'm done.

jay


Mon Oct 06, 2008 3:59 pm
Profile WWW
Site Admin
User avatar

Joined: Mar 19, 2008
Posts: 6659
Location: Austin, Texas
Post 
jay wrote:I'll post the script up in the User Contribution Library later today and post on this thread when I'm done.


After looking over my library, it has become clear that to post the whole thing would not be in everyone's best interest. The main reason is that much of what's in my current library is going to be integrated into various scripts in the next major release, and I want to minimize any confusion that might occur because of that.

So, instead, I'm just gonna post the code here that's relevant and you can decide if you like it and where you want to put it. It should be in a script that's in the Scripts/Attachments folder:

Code: Select all
(*
 Miscelllaneous Indigo Attachments.scpt
 Version 1.0

  Copyright 2008 Jay Martin

 This attachment library for Indigo is a collection of helper scripts that I've
 found useful. Some functions use (and extend) some of the current attachment 
 scripts that ship with Indigo (see note below). Currently, there are just a
 couple of iTunes related functions, but as I develop more functionality
 i expect that it'll grow.
 
 This script is freely distributable/modifiable with no guarantees
  as to it's function.
*)
------------------------------------------------------
-- Properties, globals, etc
------------------------------------------------------
(* First, since this attachment script extends other attachment scripts distributed with
Indigo, we need to locate those scripts. This part WILL break if Matt changes
file paths or functions. **You have been warned**. Hopefully though, all this trouble
will further the reuse of code. Matt assures me that the installer only overwrites the
existing scripts, so all should be fine.
*)
set applicationSupportFolder to path to application support as string
set theFilePath to applicationSupportFolder & ¬
   "Perceptive Automation:Indigo 2:Scripts:Attachments:iTunes attachment.scpt"
set iTunesAttachmentScript to load script alias theFilePath
-- Let's use the same timeout property as the iTunes attachment script
property iTunesTimeout : 1 -- don't hang server up for more than 1 seconds...
-- Name of variable to determine if we should actually say a message
property isMutedVariable : "HouseNotificationsAreMuted"
-- Some handy constants
property comma : ","

(*********************************
* iTunes Functions

 These functions augment and/or facilitate interactions with iTunes and the
 shipping iTunes Attachment.scpt
*)
------------------------------------------------------
-- (boolean) isiTunesPlaying()
(*
This function simply returns true if iTunes is playing, false if not.
*)
------------------------------------------------------
on iTunesPlaying()
   try
      with timeout of iTunesTimeout seconds
         tell my iTunesAttachmentScript
            set isITunesRunning to iTunesIsRunning(false)
         end tell
         if (isITunesRunning) then
            tell application "iTunes"
               set tempState to player state
               if ((tempState = playing) or (tempState = fast forwarding) or (tempState = rewinding)) then
                  return true
               else
                  return false
               end if
            end tell
         end if
      end timeout
   on error number errNum
      if errNum is -1712 then
         tell my iTunesAttachmentScript to iTunesLogTimeout()
      else
         -- Exceptions can happen if iTunes is terminated while the script is running,
         -- or if it is terminated before the system updates the process list. We'll just
         -- silently ignore all these exceptions.
         -- tell my iTunesAttachmentScript to iTunesLog("inside on error with errNum: " & errNum)
      end if
   end try
   return false
end iTunesPlaying
------------------------------------------------------
-- (boolean) iTunesPauseIfPlaying
(*
This function pauses iTunes if it is playing, and returns a boolean that indicates if it needed pausing.
*)
------------------------------------------------------
on iTunesPauseIfPlaying()
   if (iTunesPlaying()) then
      tell my iTunesAttachmentScript to iTunesPause()
      return true
   else
      return false
   end if
end iTunesPauseIfPlaying
------------------------------------------------------
-- (boolean) iTunesPlayPause()
(*
This function pauses iTunes if it is playing, and vice versa. Simple toggle.
*)
------------------------------------------------------
on iTunesPlayPause()
   if (iTunesPlaying()) then
      tell my iTunesAttachmentScript to iTunesPause()
   else
      tell my iTunesAttachmentScript to iTunesPlay()
   end if
end iTunesPlayPause
------------------------------------------------------
-- pauseiTunesAndSay(string theMessage)
(*
This function pauses iTunes if it is playing, uses the speech manager to speak the string that's
passed in, then starts iTunes back up if it was paused in the first place. Kinda a stateful (and
temporary) pause. I'm using Indigo Date/Time Actions to do other things besides control insteon/x10
devices, and often i want it to speak  something, so I wanted it to pause iTunes if it was playing. This
function returns nothing.
*)
------------------------------------------------------
on pauseiTunesAndSay(theMessage)
   using terms from application "IndigoServer"
      if not (variable isMutedVariable exists) then
         make new variable with properties {name:isMutedVariable, value:false}
      end if
      if not (value of variable isMutedVariable) then
         set didPause to my iTunesPauseIfPlaying()
         say theMessage
         if (didPause) then
            tell my iTunesAttachmentScript to iTunesPlay()
         end if
      end if
   end using terms from
end pauseiTunesAndSay
------------------------------------------------------


Use, extend, etc. To integrate telling AirFoil to use a different source and/or speaker set, you'd just need to add some stuff before and after the "say theMessage" line which is 7 lines up from the bottom. :)

jay


Mon Oct 06, 2008 5:54 pm
Profile WWW

Joined: Jul 06, 2010
Posts: 499
Location: Petoskey, MI
Post Re: Some help with iTunes
Can Indigo actually speak a variable value on it's own by referencing the variable ID somehow or does that have to be handled from a script?


Fri Mar 30, 2012 9:42 am
Profile
Site Admin
User avatar

Joined: Mar 19, 2008
Posts: 6659
Location: Austin, Texas
Post Re: Some help with iTunes
That has to be done from a script, but the script is pretty simple. For example:

Code: Select all
newStr = indigo.activePlugin.substituteVariable("Variable test1 is %%v:1893500335%%")
indigo.server.speak(newStr)


The first line uses the plugin defined substituteVariable() method to perform the substitution. That method searches for specially formatted tokens that represent variables in the string that you pass in and returns a string back with the variable value substituted.

You can then use the server method to speak it. We have plugin defined methods described in the plugin.py section of the plugin developer's guide. We should probably document that in the scripting guide as well.

Also, I'll add it to the list of things to add to the Action Collection - would make a useful addition there.

_________________
Jay (Indigo Support)
Image


Fri Mar 30, 2012 10:47 am
Profile WWW

Joined: Jul 06, 2010
Posts: 499
Location: Petoskey, MI
Post Re: Some help with iTunes
Good deal. Thanks for the script! I had the same airfoil hang problem with system audio as I had before... I think I'm just going to have it switch to indigo server for the spoken notifications from now on.... that will cut down on some other things I'm doing to make it work as well.

In what application would action collection of this help with? I've read up on action collection generally but don't use them for anything as of yet.


Fri Mar 30, 2012 12:08 pm
Profile
Site Admin
User avatar

Joined: Mar 19, 2008
Posts: 6659
Location: Austin, Texas
Post Re: Some help with iTunes
Rather than using a script you could just select the "Speak Message" action in the Action Collection plugin's Actions list. The configuration would look almost identical to the iTunes "Pause and Say" action - you specify the message with the embedded variable substitution tokens. No scripting.

_________________
Jay (Indigo Support)
Image


Fri Mar 30, 2012 12:14 pm
Profile WWW

Joined: Jul 06, 2010
Posts: 499
Location: Petoskey, MI
Post Re: Some help with iTunes
Gotcha... yup, that would be handy. By the way, you were right about the logic with trying to control all the different sources through airfoil... there are a lot of extra things to try to manage.


Fri Mar 30, 2012 12:19 pm
Profile

Joined: Jul 06, 2010
Posts: 499
Location: Petoskey, MI
Post Re: Some help with iTunes
This is related because I'm using a variable change as a trigger to speak that variable... so does indigo see a "change" as new information getting put in as the value even if it's the same as it was previously or does it only see it as a change if the value actually changes?


Fri Mar 30, 2012 12:32 pm
Profile
Site Admin
User avatar

Joined: Mar 19, 2008
Posts: 6659
Location: Austin, Texas
Post Re: Some help with iTunes
The change trigger only fires when the value actually changes - so setting the same value will not fire the trigger.

_________________
Jay (Indigo Support)
Image


Fri Mar 30, 2012 1:36 pm
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 11 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.   Template designed by STSoftware.