Connecting to shared iTunes Libraries on my Local network.

Hello.

This seems like it should be the easiest thing in the world but everything I’ve found online references the most arcane solutions with bizarre dependencies.

I’ve written a script that when activated shuffles music from a shared library. nothing special at all (and probably written badly (don’t know if the repeat is necessary if i know the name of the library) but the script only works if i click on the shared library first which means i might as well hit “play” myself too. : )

tell application "iTunes"
	activate
	repeat with theSource in every source
		if kind of theSource as text is "shared library" then
			set theLibrary to user playlist "shared music" of theSource
		end if
	end repeat
	set sound volume to 100
	tell theLibrary
		set shuffle to true
		set song repeat to all
	end tell
	play some track of theLibrary
end tell

It all seems overly complicated and unnecessary… i can see that the libraries are being shared in iTunes so i should be able to connect to them in some way. i think. hehe.

EDITED to remove all the stuff that has no bearing on the fundamental problem I’m experiencing and to give focus to the follow up post.

I’ve spent a bit more time with this problem and downloaded the trial version of UI Browser to narrow down proper structure for the GUI scripting portion of the problem, but still can’t identify the reason it’s still a problem.

I’m using iTunes 10.2 on 10.6.6 and the shared library i’m looking to connect to is in row 17 in my iTunes right now

if i run this script when the shared library is visible AND connected it returns the name of the shared library.
If I run this script when the shared library is visible and NOT connected it returns an “Invalid index. number -1719” error.

activate application "iTunes"
tell application "System Events"
tell process "iTunes"
get the value of static text 1 of row 17 of outline 1 of scroll area 2 of window 1
end tell
end tell

any thoughts on how to get iTunes to basically “click” on that shared library to activate it? or at the very least recognize that the row exists.

I finally found the solution to this problem via the Apple Discussion boards.

https://discussions.apple.com/thread/2815804

I wanted to post it here in case somebody else ever came across the problem.

I was given the following code as a solution, but there’s something about it didn’t quite jibe with the rest of my script (i hadn’t mentioned that it had to continue after doing the job this little snippet handles) so i’m posting the final version of the code that i used below it.

tell application "iTunes"
 activate
 tell front browser window to if minimized then set minimized to false
 if my connectSource() then repeat 12 times
 tell source "MY SHARED LIBRARY" to if exists then
 tell user playlist "MY SHARED PLAYLIST" to if exists then
 set shuffle to true
 set song repeat to all
 play some track
 return
 end if
 end if
 delay 5 -- wait while loading, loop one minute maximum, 
 -- after 1 minute, maybe the connection failed or the playlist doesn't exists
 end repeat
end tell

on connectSource()
 tell application "System Events" to tell scroll area 2 of window 1 of process "iTunes"
 tell (first row of outline 1 whose value of it's static text 1 is "MY SHARED LIBRARY")
 set b to exists
 if b then select -- connect
 end tell
 end tell
 return b
end connectSource

and here’s my adaptation that suited my needs:

tell application "iTunes"
  activate
          set sound volume to 100
          tell front browser window to if minimized then set minimized to false
          tell application "System Events" to tell scroll area 2 of window 1 of process "iTunes"
                    tell (first row of outline 1 whose value of it's static text 1 is "MY SHARED LIBRARY")
                              set b to exists
                              if b then select -- connect
                              delay 2
                    end tell
          end tell
          tell source "MY SHARED LIBRARY" to if exists then
                    tell user playlist "MY SHARED PLAYLIST" to if exists then
                              set shuffle to true
                              set song repeat to all
                              play some track
                    end if
          end if
end tell