Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Win32 iTunes Alarm Clock (Ole!)

by Adam (Vicar)
on Jul 07, 2006 at 00:44 UTC ( [id://559699]=sourcecode: print w/replies, xml ) Need Help??
Category: Win32
Author/Contact Info Adam
Description: I was playing with the iTunes SDK interface via OLE and Perl. There are plenty of other ways to do this, and mine is pretty quick and dirty, but it gives you an idea of where to start. Realistically, you don't want to use sleep for an alarm clock, but I was toying around and I left it in. If you decide to make a real alarm clock with this, just use the OLE code and then schedule your script with the NT scheduler or something. My favorite parts here are that it fades up the music and that it plays an iTunes playlist. I made one that starts out with a brief bit o' classical music to fade up on, followed by that Good Morning song off of the Beatle's Sgt. Pepper's - the one that starts with a rooster crowing. Now if only I can figure out how to get those at the top of the party shuffle so that the rest of the morning music is random. I digress.
#!perl
use strict;
use Time::Local;
use Warnings;
use Win32;
use Win32::OLE;

my $start = shift || -1;  # Use -1 for immediate start.
my $playlist = shift;

my $fadeup = 1;

if ( not defined $playlist )
{
    if ( $start !~ m/^\s*-?\d+\s*$/ )
    {
        $playlist = $start;
        $start = -1;
        $fadeup = 0;
    }
    else
    {
        $playlist = "Party Shuffle";
    }
}

if ( $start != "-1" ) # Determine when to proceed, then wait until the
+n.
{
    my ( $sH, $sM ) = $start =~ m/^\s*(\d\d?)(\d\d)\s*$/;

    die "$0 '$start'\nTime should be in military time with no symbols,
+ e.g.:\n6:30 am = 630, 10:15 pm = 2215\n" 
        unless defined( $sH ) and defined( $sM );

    my $t = time();
    my @p = localtime($t); # ($sec,$min,$hour,$mday,$mon,$year,$wday,$
+yday,$isdst)
    my ( $tM, $tH ) = ( $p[1], $p[2] );
    my $s = 0;
    my $today = 0;

    if ( $sH < $tH || ( $sH == $tH && $sM <= $tM ) )
    {
        my @sP = localtime($t + 86400);
        $s = timelocal( 0, $sM, $sH, $sP[3], $sP[4], $sP[5] );
    }
    elsif ( $sH > $tH || ( $sH == $tH && $sM > $tM ) )
    {
        $s = timelocal( 0, $sM, $sH, $p[3], $p[4], $p[5] );
        $today = 1;
    }
    else
    { die "ASSERT( '$sH', '$tH' )" }

    print "Set for ", ( $today ? "today, " : "tomorrow, " ), scalar( l
+ocaltime( $s ) ), "\n";

    sleep( ( $s - $t ) - 59 ) if ( ( $s - $t ) > 59 );

    print "Ready? One Minute to Go!\n";
    sleep( 15 ) while time() < $s;
}
else
{
    # no delay? then no fade either.
    $fadeup = 0;
}

print "Starting iTunes . . .\n";
my $G_iTA = new Win32::OLE("iTunes.Application");
$G_iTA->{ "BrowserWindow" }->{ "MiniPlayer" } = 1;
$G_iTA->{ "SoundVolume" } = $fadeup ? 10 : 90;

print "Playing...\n";
$G_iTA->{ "LibrarySource" }->{ "Playlists" }->ItemByName( $playlist )-
+>PlayFirstTrack();

if ( $fadeup )
{
    print "Fading up...\n";
    for my $v ( 11 .. 90 )
    {
        sleep( 1 );
        $G_iTA->{ "SoundVolume" } = $v;
    }
    
    print "Have a nice day!\n\n";
}
else
{
    print "iTunes load complete.\n\n";
}
Replies are listed 'Best First'.
Re: Win32 iTunes Alarm Clock (Ole!)
by jartymiak (Initiate) on Sep 20, 2006 at 07:45 UTC
    Nice!

    I'm writing a Perl module to make it easier to use the Apple SDK from a Perl script on Windows.

    I posted some examples online. The code hasn't been wrapped into a proper CPAN module, yet. You will need to replace

    use iTunes::Win32::COM;

    with

    require COM;
    and drop COM.pm into the same directory with the script that uses it.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://559699]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-24 19:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found