gucio1234 71 Share Posted May 23, 2017 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. Quote Link to post Share on other sites
0 Sankotronic 1,333 Share Posted May 23, 2017 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! Quote Link to post Share on other sites
0 StarkJohan 6 Share Posted June 1, 2017 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. Quote Link to post Share on other sites
0 Jamie mccrostie 81 Share Posted June 1, 2017 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 ) Quote Link to post Share on other sites
0 StarkJohan 6 Share Posted June 1, 2017 (edited) 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. /* 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 June 1, 2017 by StarkJohan 1 Quote Link to post Share on other sites
0 StarkJohan 6 Share Posted June 1, 2017 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. Quote Link to post Share on other sites
0 StarkJohan 6 Share Posted June 2, 2017 (edited) 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 Edited June 2, 2017 by StarkJohan Quote Link to post Share on other sites
0 gucio1234 71 Author Share Posted June 2, 2017 @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 ... Quote Link to post Share on other sites
0 StarkJohan 6 Share Posted June 2, 2017 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 Quote Link to post Share on other sites
0 gucio1234 71 Author Share Posted June 2, 2017 @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 Quote Link to post Share on other sites
0 gucio1234 71 Author Share Posted June 2, 2017 (edited) @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 June 2, 2017 by gucio1234 Quote Link to post Share on other sites
0 StarkJohan 6 Share Posted June 2, 2017 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. Quote Link to post Share on other sites
0 gucio1234 71 Author Share Posted June 5, 2017 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 1 Quote Link to post Share on other sites
0 StarkJohan 6 Share Posted June 5, 2017 Yeah I actually added a slider a couple of days ago as well Quote Link to post Share on other sites
0 Trr 0 Share Posted October 1, 2017 (edited) 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 October 2, 2017 by Trr Quote Link to post Share on other sites
0 Trr 0 Share Posted October 3, 2017 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. Quote Link to post Share on other sites
0 gucio1234 71 Author Share Posted October 3, 2017 @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. 1 Quote Link to post Share on other sites
0 amatt 5 Share Posted October 22, 2017 @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" Quote Link to post Share on other sites
0 gucio1234 71 Author Share Posted October 22, 2017 @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"; } ?> Quote Link to post Share on other sites
0 amatt 5 Share Posted October 22, 2017 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 Quote Link to post Share on other sites
Question
gucio1234 71
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
32 answers to this question
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.