I use iTunes on Windows only because I have to. I don't really care for it as a music organizer and besides, I rarely listen to music on my computer. I have an iPod though and what I do like about iTunes is that I can just plug in my iPod and it will automatically sync everything without any intervention from me. I also like that iTunes allows me to subscribe to podcasts. It annoys me though that unless I have iTunes running it will not update my podcast subscriptions. Since I only use iTunes for syncing my iPod, iTunes is never running.

Luckily iTunes has an COM automation interface that you can use to force it to update your podcasts. With a bit of PowerShell scripting I am now able to automatically update all of my podcasts in the middle of the night when I don't have to be bothered with the fact that iTunes is running. I used PowerShell for this scripting task as again, it seems perfect for a task like this.

My script is a little more complicated than just starting up iTunes and then starting the podcast downloads. Since I am scheduling this process to run nightly I also wanted to be able to close iTunes once it was finished. Unfortunately the iTunes COM automation interface provides no way to tell whether or not iTunes is busy downloading new podcasts. However it seems that iTunes creates a pretty predictable temporary download folder structure, which it then removes when it has finished downloading all of the podcasts that it updates. I used this bit of knowledge in my script to detect when iTunes was busy downloading new podcasts. I just watch for those directories, waiting for iTunes to finish and then I shut down iTunes. So far this has worked pretty well although I'm sure it is not complete robust under all circumstances.

Here is the PowerShell script I developed. If you want to use it you'll have to tweak the directory path that is checked in the script to match your computer. I should also mention that on at least one of my machines iTunes creates the temporary 'downloads\podcast' directory in a different place under the iTunes music folder. I have not found a setting in iTunes that determines where the temporary folder gets created so you may have to poke around a little while iTunes is downloading podcast updates to figure out where it is on your machine if this does not work.

 

# update iTunes pod casts

function test-Downloading
{
    if(test-path 'C:\Documents and Settings\MusicBox\My Documents\My Music\iTunes\iTunes Music\Downloads\Podcasts')
    {
        return $True
    }
    return $False
}


$iTunes = new-Object -comobject iTunes.Application
if($iTunes -ne $null)
{
    'iTunes started' | out-Host

    # start iTunes podcasts update
    'updating podcasts' | out-Host
    $iTunes.UpdatePodcastFeeds()

    # set a time out time
    $TimeOut = (get-Date).AddMinutes(30)

    # wait a few minutes and check if it is downloading
    'waiting for downloads to start...' | out-Host
    start-sleep (30)

    # loop while iTunes seems to be downloading (presence of temporary download folder)
    'checking for download activity' | out-Host
    while(test-Downloading -and ((get-Date) -lt $TimeOut))
    {
        # give it some more time
        'download activity detected, waiting...' | out-Host
        start-sleep (60)
    }

    if((get-Date) -ge $TimeOut)
    {
        'downloading timed-out' | out-Host
    }

    # now quit iTunes after a little settling time
    start-Sleep (30)
    'quitting iTunes' | out-Host
    $iTunes.Quit()
    $iTunes = $null
}

Comments (9) -

Fortezza
Fortezza
1/28/2007 8:47:35 PM #

This looks interesting. Assuming that have .Net 2.0, how would I run this script? Do I just save it as a certain extension, edit the paths, and then double-click on it? If so, what is that extension?

Thank You,

Fortezza

David Jade
David Jade
1/29/2007 6:36:46 PM #

First you'll have to install the PowerShell scripting language, which is separate from .Net 2.0. You can get this free from Microsoft.

Then once you have fixed up the paths in the above scrip, you can use PowerShell to launch the script, which should be saved as a .PS1 file. Note that by default PowerShell will not execute a script when you double-click on it. You have to set up a file association to do that. There is another article on my blog for how to set this up:
mutable.net/.../PowerTips-for-PowerShell.aspx

Also I should note that the current version of iTunes seems to crash if it is launched from a script while the screen saver is running. The simple solution is to disable or stop the screen saver if you want to run this script from a scheduled task.

david

Nazar
Nazar
4/26/2007 4:02:10 AM #

How would you tell Itunes to download _ALL_ podcasts, not only latest available?

Pierre Poquet
Pierre Poquet
12/20/2007 12:36:59 AM #

You might want to mention that by default powershell includes something called execution policy and that policy is set by default to restricted.
You need to open powershell and type
Set-ExecutionPolicy RemoteSigned
if you want your script to run.

Once I did that your code ran like a charm.
Thank you
Pierre

Stefanija Stojanovska
Stefanija Stojanovska
11/1/2008 12:34:24 AM #

it is good

Rich
Rich
11/23/2008 7:24:37 AM #

All this talk of powershell was a bit scary for me, so I simply:
1) changed the time on the computer to 2:00 AM
2) Asked iTunes to 'Refresh'
3) Changed the time back
The default update time is now 2:00am. Not elegant, but it seems to work. I still think it is a lamentable effort on the part of the iTunes people that this can't be set manually. I don't imagine it would be very difficult, but maybe it interferes with their plan for WORLD DOMINATION.

Jim Rush
Jim Rush
3/28/2009 5:38:20 PM #

Thank you for script.  My wife is blind and she has been having difficulty using iTunes to update her podcasts.  The iTunes interface leaves a lot to be desired for somebody using only a keyboard and a screen reader.  

I had some challenges with the directory method and wanted to do something that provided more feedback.  The following script figures out what podcasts need to be downloaded and then waits for the download to completed.  Every 10 seconds it prints out the ones downloading.

# update iTunes podcasts

$iTunes = new-Object -comobject iTunes.Application
if($iTunes -ne $null)
{
    'iTunes started' | out-Host

    'Checking for new podcasts' | out-Host
    $iTunes.UpdatePodcastFeeds()
    

    $library = $iTunes.Sources | where { $_.Name -eq 'Library' }
    $podcasts = $library.Playlists | where {$_.Name -eq 'Podcasts'}

    $foundTracks = $True
    $downloadList = New-Object System.Collections.ArrayList
    while($foundTracks)
    {
        Start-Sleep -seconds 10
        $foundTracks = $False
        foreach($track in $podcasts.Tracks | where {!$_.Enabled})
        {
            $foundTracks = $True
            if(($downloadList | where {$_.Name -eq $track.Name}) -eq $null)
            {
                $track.DownloadPodcastEpisode()
                $result = $downloadList.Add($track)
                'Downloading '+$track.Name |out-Host
            }
            else
            {
                'Still downloading '+$track.Name |out-Host
            }
        }
    }

    'Done' | out-Host
}
else
{
    'Error, Unable to find a running iTunes program or start it' | out-Host
}

Shane
Shane
6/7/2009 1:15:01 AM #

Brilliant!  I have been trying to figure out a solution for this all day and yours works perfectly.

I took it a little further because I have my PC in sleep mode at night.  I used a windows scheduled task to wake my pc and run this script.

I also disabled hibernate so I could have the script put the pc back to sleep by adding the following line to your script

rundll32 powrprof.dll,SetSuspendState

Matthew
Matthew
12/31/2009 12:06:47 AM #

Fantastic, thank you.

Just had to set execution policy to run all scripts (which you know... is a security issue and all...):

"set-executionpolicy unrestricted"

And had to change the download directory...

Then it worked like a charm... which is a strange saying, do charms actually work? Hmmm maybe that's not a compliment then, but the script works :o)

Although after reading the comments the idea of simply changing your clock time, telling ITunes to sync, then changing the time back seemed to obvious... and here I was planning to stay up until 1am to start the sync so it would start future syncs at 1am... hahaha idiot...

Thank you!


Comments are closed

Flux and Mutability

The mutable notebook of David Jade