Jump to content
  • 0

HC2 & iTunes on OS X integration ... any idea ?


Question

I have found several topics on HC2 and iTunes.  However there were only brief ideas and I did not find any complete, well documented solution.

 

Is there anybody who managed to integrate  iTunes running on OS X system and Fibaro HC2 box in the way that VD would be able to control iTunes ?

 

It would be great to have VD with buttoons to play, stop, select playlist, etc.  directly from HC2.

 

Any hint is highly appreciated.

 

Link to post
Share on other sites

Recommended Posts

  • 0

I'm interested in same solution. Haven't find time to play with it yet, but if there is already solution out there will be nice for sharing!

Link to post
Share on other sites
  • 0

I did a POC for this a few years back but never got around to using it. IIRC the setup was as follows.

 

1. Run the built in web server on the mac that also runs iTunes. 

2. Create applescripts for each command needed. One for play, one for next track and so on.

3. Create a simple php api that triggers the applescripts.

4. Trigger the php api from HC2.

 

Should be easy enough to recreate. I'll have a look at it.

Link to post
Share on other sites
  • 0
5 minutes ago, StarkJohan said:

I did a POC for this a few years back but never got around to using it. IIRC the setup was as follows.

 

1. Run the built in web server on the mac that also runs iTunes. 

2. Create applescripts for each command needed. One for play, one for next track and so on.

3. Create a simple php api that triggers the applescripts.

4. Trigger the php api from HC2.

 

Should be easy enough to recreate. I'll have a look at it.

 

"Bring it on" ..... (encouragement ) 

Link to post
Share on other sites
  • 0

Found my old php files. It's really simple. The issue is getting the permissions right for exec in php.

Ha! I can't post the code to the forum because of some lame protection. Not even in code brackets. Lets try an image.

Screen Shot 2017-06-01 at 21.48.42.jpg

/*
tell app "iTunes" get name of playlists
tell app "iTunes" get count of playlists
tell app "iTunes" to stop
tell app "iTunes" to pause
tell app "iTunes" to play
tell app "iTunes" to playpause
tell app "iTunes" to play next track
tell app "iTunes" to play previous track
tell app "iTunes" to get name of current track
tell app "iTunes" to get the album of the  current track

set current_track to the current track
set track_album to the album of the  current track
set track_artist to the artist of the current track
set track_comments to the comment of the current track
set track_name to the name of the current track
*/

I  other words, no applescript files needed. Just build a php api to exec the osascript commands and you're done. Getting track info and playlists and info like that is possible as well as setting system volume and a bunch of other stuff. 

 

Here's some command references. http://alvinalexander.com/apple/itunes-applescript-examples-scripts-mac-reference

Edited by StarkJohan
  • Like 1
Link to post
Share on other sites
  • 0

Quick and dirty POC php api for getting basic track info to a VD. Adding player controls is easy and lots of stuff in iTunes is scriptable using Applescript.

 

Can't attach code because of forum "security" (probably because of the exec command). Will have to attach images.

Screen Shot 2017-06-01 at 23.37.43.jpg

Screen Shot 2017-06-01 at 23.42.01.jpg

Link to post
Share on other sites
  • 0

OK. Did some further adding this morning. Using this example you'll be able to do basic controls. Adding more advanced functionality should be rather easy if needed but I have no use for anything else right now. Playing specific playlists or specific songs, enable shuffle, anything applescript can do really. Have a look in the Script Editor -> Window -> Library -> iTunes for a full list of available commands.

 

VD attached. 

PHP API: https://pastebin.com/ATCPJmtw

 

 

iTunes.vfib.json

Screen Shot 2017-06-02 at 08.55.03.jpg

Edited by StarkJohan
Link to post
Share on other sites
  • 0

@StarkJohan,

 

Thank you very much for your contribution. I will follow your way, however I need some more explanation/guidance.

 

I found the guide to enable apache web server on  OS X :

 

https://medium.com/@JohnFoderaro/how-to-set-up-apache-in-macos-sierra-10-12-bca5a5dfffba

 

I managed to activate apache so my web server is up and running.

 

I have set the IP of my MAC an port 80 for the VD settings.

I understood from your VD that "/testzon/itunesremote/" is the path to the files on the OS X web server. That is OK. One has to modify this according to the file location.

 

I have placed your .php file into the location of the files for my web server.

 

And the question is how to link .php file with the buttons of VD so the proper command is called.

 

Presently when I press PLAY button of the VD I see the response in the debug window of the example index.html file.

 

thnak you in adavance for further guidance ...

 

Link to post
Share on other sites
  • 0
1 hour ago, gucio1234 said:

I have set the IP of my MAC an port 80 for the VD settings.

I understood from your VD that "/testzon/itunesremote/" is the path to the files on the OS X web server. That is OK. One has to modify this according to the file location.

 

That's correct. This should obviously be more dynamically scripted for a "proper" VD... I never said it was pretty :)

 

You also need to activate PHP for OSX. Loading the php5 module in apache should do the trick. Edit the apache config: "/etc/apache2/httpd.conf"

 

Uncomment this line:

LoadModule php5_module libexec/apache2/libphp5.so

 

And restart to reload config:

sudo apachectl restart

 

To check if it works, just create a simple php file like this pastebin code in your webroot and browse to it. It should display info on your php installation (a huge purple table).

https://pastebin.com/AqZHM7W5

 

To "link" my php api just name the file index.php and put in the location you prefer. In my case this is <ip-of-my-mac>/testzon/itunesremote/index.php

Link to post
Share on other sites
  • 0

@StarkJohan,

 

Thanks for the fast response. The trick I missed was to rename your .php file to index.php

 

I tested it with PLAY button and works like a charm.  No I will modify the rest of the VD buttons

 

:):):)

 

Link to post
Share on other sites
  • 0

@StarkJohan,

 

One more thing :

 

All is working fine for me, but ...

I have no refresh of the 'status'  for the first label of VD.

 

When I see debug of "GET INFO" button I see the proper response received from iTunes (Artist, Track, Current Position, Current Track Time, Current Volume).

Unfortunately these data is not updated for the first label "STATUS".

I see the updated "STATUS" label only if I modify the VD (eg. by adding not significant space in the code) and save the VD.

 

the line :

fibaro:call(id, "setProperty", "ui.status.value", response.cur_artist.." - "..response.cur_track .." "..response.cur_pos .." of "..response.cur_track_time)

should update the label "status" but it is not.

 

Any hint ... ?

 

 

UPDATE:

I have replaced all icons of "blue box" for this VD with the nice iTunes icon and "GET INFO" is updated as expected.

 

 

 

 

Edited by gucio1234
Link to post
Share on other sites
  • 0

So it all works now? Including the status label? If so, thanks for letting me know and good job :) 

 

Any other specific function you're looking for that you can't figure out on your own, please let me know and I might be able to help out.

Link to post
Share on other sites
  • 0

I have added a volume slider. That makes good start for integration with @jompa68's "Alarm Clock and Wake Up Timer" :

Now the iTunes VD may be driven by the alarm scene in the same way as sonos speaker.

 

I have few comments to ACWUT solution but I will share them on @jompa68's thread.

 

 

iTunes.vfib

  • Like 1
Link to post
Share on other sites
  • 0

Hi!

Please could you help me:

1)If i use VD nothing happens, in debugger  got 400 code

2)if i put /testzon/itunesremote/?cmd=..urlencode("play")  in safari, then i get "cmd not accepted"

3)

What do i wrong?

 

Thank you

 

solved

 

add in address  /~username/testzon/itunesremote/?cmd=

 

 

Edited by Trr
Link to post
Share on other sites
  • 0
tell application “iTunes”
	set apDevices to (get name of AirPlay devices)
	–> {“Computer”, “Doug’s AirPort Express”}
	set current AirPlay devices to AirPlay device “Computer”
end tell

Apple script for choose airplay speakers. Added to VD this option, works good.

Link to post
Share on other sites
  • 0

@Trr  It is good you control airplay speakers from iTunes,  however I have a little different approach (which I believe is more flexible). It was detailed described in one of my threads.

 

I have created Audio Mixer so I can adjust volumes separately for each AirPlay speaker and I can play TTS messages and/or music from iTunes. 

  • Like 1
Link to post
Share on other sites
  • 0

@gucio1234, @StarkJohan

 

I am having some trouble with getting this to work. Can someone help me figure it out?

 

Here is my setup.

 

1. have Mac OS X server setup

2. OSX server handles the .php files

3. enabled php via terminal

4. put the .php file from stark in the OS X website folder (renamed it itunes.php)

5. imported VD from stark

6. updated file path in the fibaro VD

 

 

when I run the debug in VD it gives 400 error

 

when I enter <IP address(hostname)>/itunes.php into a web browser I get the error(s) below.

 

"Notice: Use of undefined constant cmd - assumed 'cmd' in /Library/Server/Web/Data/Sites/Default/itunes.php on line 26

Notice: Use of undefined constant cmd - assumed 'cmd' in /Library/Server/Web/Data/Sites/Default/itunes.php on line 38
cmd not accepted"

Link to post
Share on other sites
  • 0

@amatt,

 

I have path to PHP script defined in my VD as :

 

Cytuj

response, status, errorCode = HC2:GET("/~username/itunesremote/?cmd="..command);

 

My PHP script to drive iTunes is index.php

 

and it is placed on my Mac as :

 

Macintosh HD/Users/username/Sites/itunesremote/index.php

 

Here is my index.php :

<?php

$get = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING);

function itunes($cmd) {
	return exec('osascript -e \'tell app "iTunes" to '.$cmd.'\'');
}	 

// Accepted commands
$commands = array(
		"play",
		"playpause",
		"pause",
		"stop",
		"play next track",
		"play previous track",
		"next track",
		"previous track"
		);

// Add sound levels to accepted command array in steps of 1
for ($x = 0; $x <= 100; $x++) {
	$commands[] = "set sound volume to ".$x; 
}

if ($get[cmd] == 'status') {
	$info[cur_artist] = itunes("get artist of current track");
	$info[cur_track] = itunes("get name of current track");
	$info[cur_pos] = intval(itunes("get player position"));
	$info[cur_track_time] = itunes("get time of current track");
	$info[cur_volume] = itunes("get sound volume");

	// Get time position in min:sec without leading zero
	$info[cur_pos] = ($info[cur_pos] < 60 ? "0:".gmdate("s", $info[cur_pos]) : ltrim(gmdate("i:s",$info[cur_pos]),0)); 

	echo json_encode($info);

} else if (in_array($get[cmd],$commands)) {
	// GET-var "cmd" is in $command array, now carry out command.
	itunes($get[cmd]);
	echo "cmd OK";
} else {
	http_response_code(400);
	echo "cmd not accepted";
}


?>

 

 

Link to post
Share on other sites
  • 0
17 minutes ago, gucio1234 said:

@amatt,

 

I have path to PHP script defined in my VD as :

 

 

My PHP script to drive iTunes is index.php

 

and it is placed on my Mac as :

 

Macintosh HD/Users/username/Sites/itunesremote/index.php

 

Here is my index.php :

<?php

$get = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING);

function itunes($cmd) {
	return exec('osascript -e \'tell app "iTunes" to '.$cmd.'\'');
}	 

// Accepted commands
$commands = array(
		"play",
		"playpause",
		"pause",
		"stop",
		"play next track",
		"play previous track",
		"next track",
		"previous track"
		);

// Add sound levels to accepted command array in steps of 1
for ($x = 0; $x <= 100; $x++) {
	$commands[] = "set sound volume to ".$x; 
}

if ($get[cmd] == 'status') {
	$info[cur_artist] = itunes("get artist of current track");
	$info[cur_track] = itunes("get name of current track");
	$info[cur_pos] = intval(itunes("get player position"));
	$info[cur_track_time] = itunes("get time of current track");
	$info[cur_volume] = itunes("get sound volume");

	// Get time position in min:sec without leading zero
	$info[cur_pos] = ($info[cur_pos] < 60 ? "0:".gmdate("s", $info[cur_pos]) : ltrim(gmdate("i:s",$info[cur_pos]),0)); 

	echo json_encode($info);

} else if (in_array($get[cmd],$commands)) {
	// GET-var "cmd" is in $command array, now carry out command.
	itunes($get[cmd]);
	echo "cmd OK";
} else {
	http_response_code(400);
	echo "cmd not accepted";
}


?>

 

 

 

@gucio1234

in your path in the VD for the php is does not indicate the "/Sites" folder is the on purpose or accident?

 

I have the same .php file code as you

 

what was this code below? apple script? do you have to have an apple script to run this?

 

/*
tell app "iTunes" get name of playlists
tell app "iTunes" get count of playlists
tell app "iTunes" to stop
tell app "iTunes" to pause
tell app "iTunes" to play
tell app "iTunes" to playpause
tell app "iTunes" to play next track
tell app "iTunes" to play previous track
tell app "iTunes" to get name of current track
tell app "iTunes" to get the album of the  current track


set current_track to the current track
set track_album to the album of the  current track
set track_artist to the artist of the current track
set track_comments to the comment of the current track
set track_name to the name of the current track
*/

 

 

I am trying to get one button working then I can workout the rest. I am working on the pause button here is my code below for the VD still get the error 400 even if the itunes.php was changed to index.php

 

-- READ THIS VIRTUAL DEVICE (id, ip, port)    
local ip = fibaro:get(fibaro:getSelfId(), 'IPAddress');
local port = fibaro:get(fibaro:getSelfId(), 'TCPPort');
HC2 = Net.FHttp(ip,port);

-----------------------------------------------------
local command = urlencode("pause");
-----------------------------------------------------


response, status, errorCode = HC2:GET("Users/username/Library/Server/Web/Data/Sites/itunesremote/index.php/?cmd=" ..command);

if tonumber(status) == 200 -- Command accepted
then
      fibaro:debug("200 OK, Response: " .. response)
else
      fibaro:debug("Errorcode: "..status)
      fibaro:log("ErrorCode: "..status);
end

 

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...