Control itunes on windows, from domoticz / echo alexa etc..

In this subforum you can show projects you have made, or you are busy with. Please create your own topic.

Moderator: leecollings

Post Reply
sion
Posts: 113
Joined: Friday 08 January 2016 12:32
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Control itunes on windows, from domoticz / echo alexa etc..

Post by sion »

Hi All.
For a while i have wanted to control itunes from domoticz.
The methods do do this are not new - but i haven't seen it laid out on here, so thought i would share incase it's useful for anyone.
I should also note that this is not full control, but you can start itunes, play, pause, next, back, play track, and select and play playlists ( on shuffle if you want ). The only thing i wish we could achieve now is toggling airplay speakers on/off. if anyone has any ideas let me know :)

we have an itunes server running on a sumvision cyclone - windows 10. This runs very well .
This serves a number of apple tv's through airplay.
We have just been using the ios "itunes remote" app to control itunes, and this works ok - but since getting the amazon echo, thought it would be nice to control itunes by voice through that.

There are lots of "apple scripts" to control itunes very well on mac. and i there are also many more features. But for those like me with no mac, we can use visual basic ( filename.vbs ), and also powershell ( filename.ps1).

play, pause and skip commands
to create the play, pause and skip commands; i created a vbs for each. ( i will come to playlists later on - dont use the one on here if you want random play )
a full list of vb scripts i found here: https://github.com/ralesi/modal.ahk/tre ... and/iTunes
I wont copy out all the scripts here, but you can just copy and paste the while file into notepad, then save as your_file_name.vbs
i used itunes_play.vbs etc.. I would recommend creating a folder for them all.

I run my main domoticz on a Pi. I couldnt find a straightforward way of firing the vbs files on the windows box from my pi. I think there is a way, and some people have used eventghost. Event ghost looks like it could be good for future use, but i couldn't get my head around it quickly and lost patience, so just installed a 2nd domoticz on the windows box, alongside itunes.

on the windows domoticz, i now created push on buttons for each vbs file i wanted to trigger ( play, pause, next etc.. ).
THen on the on action: script://C:\Users\sion\Documents\scripts\next.vbs

Now when i click on the play switch in domoticz, itunes opens up ( if its not already open ) and starts playing :)

Playlists
Currently i cannot get it to play applemusic playlists. Although as im typing this, i wonder if its because i haven't downloaded them to the local machine? i will test this later. But all other playlists work,
I could not get the vbs playlist method to work correctly on shuffle, so i found a poweshell method.

create this .ps1 file:

Code: Select all

<#
.SYNOPSIS
    Plays an iTunes playlist.
.DESCRIPTION
    Opens the Apple iTunes application and starts playing the given iTunes playlist.
.PARAMETER  Source
    Identifies the name of the source.
.PARAMETER  Playlist
    Identifies the name of the playlist
.PARAMETER  Shuffle
    Turns shuffle on (else don't care).
.EXAMPLE
   .\Start-PlayList.ps1 -Source 'Library' -Playlist 'Party'
.INPUTS
   None
.OUTPUTS
   None
#>
[CmdletBinding()]
param (
    [Parameter(Mandatory=$true)]
    $Source
    ,
    [Parameter(Mandatory=$true)]
    $Playlist
    ,
    [Switch]
    $Shuffle
)

try {
    $iTunes = New-Object -ComObject iTunes.Application
}
catch {
    Write-Error 'Download and install Apple iTunes'
    return
}

$src = $iTunes.Sources | Where-Object {$_.Name -eq $Source}
if (!$src) {
    Write-Error "Unknown source - $Source"
    return
}

$ply = $src.Playlists | Where-Object {$_.Name -eq $Playlist}
if (!$ply) {
    Write-Error "Unknown playlist - $Playlist"
    return
}

if ($Shuffle) {
    if (!$ply.Shuffle) {
        $ply.Shuffle = $true
    }
}

$ply.PlayFirstTrack()

[System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$iTunes) > $null
[GC]::Collect()
save it as Start-PlayList.ps1 in the folder you saved all the vbs scripts.
This file is like the back end code. you don't need to edit it.
you may however need to enable running powershell on the pc. For windows 10 see here:https://www.tenforums.com/tutorials/545 ... -10-a.html - you may need to google for other windows versions.

to run selected playlists, we need to now create a .bat file for each one we want to use.
Inside the .bat file is the command that runs the powershell script, and inputs the variables for the playlist name etc..
The .bat file should look like this:

Code: Select all

@ECHO OFF
PowerShell.exe -Command "& '\Users\sion\Documents\scripts\Start-PlayList.ps1'" -Source "library" -Playlist "playlist_name" -Shuffle
if you have changed the name of your library, change it in the file.
Swat out playlist_name for the name of your playlist. If you playlist is more than one word, you need to double quote it, as it doesnt seem to deal with spaces well.

Code: Select all

-Source "library" -Playlist "'playlist name'" 
Save this as playlist_name.bat in the same folder.

now, point a domoticz button to the .bat file. when pressed, it fires up itunes, and starts shuffling the playlist.
Repeat this for any other playlists you want.

So now we have domoticz controlling itunes on windows. From here, having alexa / echo controlling itunes is as simple as running HA Bridge on either machine, and then add the domotics buttons to it. ( see wiki http://www.domoticz.com/wiki/Alexa )

You may need to be creative with your button names, as "alexa, turn on play" and even "alexa, turn on itunes play" confused it.
i used Resume, Halt, and skip. playlist names weren't an issue.
running domoticz 3.4834 on rpi2, with
Hue bridge / bulbs.
lightwave rf sockets, mood controlers, sensors & contacts.
Home bridge / Siri.
Ha bridge / echo Alexa.
Hard wired alarm system - setting home / away status.

Next: harmony hub.
ilpier
Posts: 31
Joined: Friday 24 February 2017 12:01
Target OS: Windows
Domoticz version:
Contact:

Re: Control itunes on windows, from domoticz / echo alexa etc..

Post by ilpier »

Hi Sion,

nice work,unfortunately I want to use spotify!
I found a VBS that does the same thing for spotify and when started by windows operating properly but if started from domoticz Virtual Switch does not work.
In LOG everything seems to start correctly but it really does not access anything.
Play Command.JPG
Play Command.JPG (28.64 KiB) Viewed 1947 times
Log Play.JPG
Log Play.JPG (17.92 KiB) Viewed 1947 times
Do you have a suggestion for me?
sion
Posts: 113
Joined: Friday 08 January 2016 12:32
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Control itunes on windows, from domoticz / echo alexa etc..

Post by sion »

Is the windows account an administrator one? ( I'm not sure if that matters, but it could be a permission thing?

Initially I was running the vbs files from a .bat file. So domoticz would link to the .bat file, and that triggers the vbs. so you could try that. Though I found in the end that linking straight to the vbs worked.



If you can't get it to work, it could be worth having a look at powershel. I found lots more options than vbs.
running domoticz 3.4834 on rpi2, with
Hue bridge / bulbs.
lightwave rf sockets, mood controlers, sensors & contacts.
Home bridge / Siri.
Ha bridge / echo Alexa.
Hard wired alarm system - setting home / away status.

Next: harmony hub.
ilpier
Posts: 31
Joined: Friday 24 February 2017 12:01
Target OS: Windows
Domoticz version:
Contact:

Re: Control itunes on windows, from domoticz / echo alexa etc..

Post by ilpier »

Yes,adminitrator!
i can try to run a VBS from a .bat but same things,under windows work in domoticz "Executing script.." but nothing happens..
sion
Posts: 113
Joined: Friday 08 January 2016 12:32
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Control itunes on windows, from domoticz / echo alexa etc..

Post by sion »

Not really sure to be honest why it's not working. Works fine for me.
When you press the button in domoticz that triggers the .bat file, can you see the file open briefly on screen? ( would just be a black box that opens briefly. )

Also, justcto check, when toy just double click the bat file, that works correctly?
running domoticz 3.4834 on rpi2, with
Hue bridge / bulbs.
lightwave rf sockets, mood controlers, sensors & contacts.
Home bridge / Siri.
Ha bridge / echo Alexa.
Hard wired alarm system - setting home / away status.

Next: harmony hub.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest