The title of this post is more general than the actual task. Specifically, I implemented a new method to play a different song for each weekday the first time a motion sensor is triggered.
A while back I played a song from iTunes whenever a specific event was detected in mControl (I’ve used two different methods to handle this). After I upgraded from version 2 to version 3 of mControl I held off on adding this capability back into the macros. The method for using iTunes worked fine but this time around I wanted to find something simpler to use that could be enhanced via scripting.
This time I used a free command-line utility called cmdmp3 that can play an MP3 file. In mControl I added a new item to a macro (Run Application), which executes a batch file that contains the necessary command-line parameters to play the songs. The batch file actually plays a different song for each weekday.
I’ve included the basic framework for the batch file. The code used to get the current day of the week and then run specific sections of the batch file was obtained from a forum post.
The batch file could use some refinement. For example, I had planned to place each song path in its own variable, but I had some problems getting the variables to work properly as parameters for cmdmp3. If I had worked that out then each section would simply have a song variable assigned to another variable that would serve as the parameter for cmdmp3.
Eventually I’d like to make the song selection randomized, which would considerably shorten the necessary code for the batch file by eliminating the test for the day of the week along with the related sections.
@echo off for /f %%a in ('date /t') do set DAY=%%a if %DAY%==Sun goto :sun if %DAY%==Mon goto :mon if %DAY%==Tue goto :tue if %DAY%==Wed goto :wed if %DAY%==Thu goto :thu if %DAY%==Fri goto :fri if %DAY%==Sat goto :sat :sun c:\tools\cmdmp3win.exe "filepath" goto next :mon c:\tools\cmdmp3win.exe "filepath" goto next :tue c:\tools\cmdmp3win.exe "filepath" goto next :wed c:\tools\cmdmp3win.exe "filepath" goto next :thu c:\tools\cmdmp3win.exe "filepath" goto next :fri c:\tools\cmdmp3win.exe "filepath" goto next :sat c:\tools\cmdmp3win.exe "filepath" goto next :next
Everything after “:next” can include whatever you may want to add after the song is played. If you don’t need to add anything then you can simply leave it as it is.
Updated 11/21/2011: I seem to have run into a familiar problem – executing external programs from within mControl. The batch file itself is sound but either mControl has a bug or the other program I’m using to execute external applications isn’t working properly. It may just be a configuration issue but I’ll need to spend some time on it before I can provide a proper solution for making this work from mControl.