views:

1486

answers:

2

Wrote the following in PowersHell as a quick iTunes demonstration:

$iTunes = New-Object -ComObject iTunes.Application
$LibrarySource = $iTunes.LibrarySource
foreach ($PList in $LibrarySource.Playlists)
{
  write-host $PList.name
}

This works well and pulls back a list of playlist names. However on trying to close iTunes a warning appears

One or more applications are using the iTunes scripting interface. Are you sure you want to quit?

Obviously I can just ignore the message and press [Quit] or just wait the 20 seconds or so, but is there a clean way to tell iTunes that I've finished working with it?

Itunes 7.7.1, Windows XP

+3  A: 

Here is one thing that I did on my a Powershell script that adds podcasts to iTunes. I use Juice on a server to download all the podcasts that I listen to. The script uses .Net methods to release the COM objects. When I wrote my iTunes script I had read a couple of articles that stated you should release your COM objects using .NET.


    [void][System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$LibrarySource)
    [void][System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$iTunes)

I also run my scripts the majority of time from a shortcut, not from the powershell prompt.

Based on your comments, I did some testing and I determined that I would get the message when running against iTunes, if I ran my script in a way that leaves powershell running. iTunes seems to keep track of that. Running the script in a manner that exits it's process after running, eliminated the message.

One method of running your script from powershell, is to prefix your script with powershell.

powershell .\scriptname.ps1

The above command will launch your script and then exit the process that was used to run it, but still leaving you at the powershell prompt.

bruceatk
Didn't work for me unfortunately. I've tried opening and closing itunes several times to make sure nothing else is attempting to use itunes, then I run my script with your lines above and quit with the same results.
Paul Hargreaves
I use my script everyday and I don't get that message. Do you have any other utilities that access iTunes? Do you use any other iTunes objects? Maybe you need to reboot to get it cleaned up once you have gotten that message?
bruceatk
Tried rebooting, upgrading to iTunes 8.0. No other iTunes objects running - just a simple reboot, open itunes, run script, close powershell, close itunes, warning message appears.
Paul Hargreaves
If Powershell stays open then you get the message. From the powershell command line I tried doing "powershell .\scriptname.ps1" and the script ran against powershell left me at the powershell command line and I didn't get the message when exiting iTunes. I'll edit my answer.
bruceatk
Aha! powershell <script> did the trick! The mind boggles...
Paul Hargreaves
Also with the powershell <script> method the ::releaseobject calls doesn't seem to be needed.
Paul Hargreaves
A: 

You should be able to set $itunes to $null. Alternatively, $itunes should have a quit method you can call. $itunes.quit()

Steven Murawski
$itunes.quit() unfortunately would close itunes, not always something I'd want to do. The powershell <script> by bruceatk worked for me though so thanks for replying.
Paul Hargreaves
@Paul Glad it worked for you.. I gave up on Itunes a long time ago :)
Steven Murawski