iTunes的警告消息因脚本退出

时间:2020-03-05 18:50:10  来源:igfitidea点击:

在PowersHell中编写以下内容作为iTunes的快速演示:

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

这样效果很好,并可以拉回播放列表名称列表。
但是在尝试关闭iTunes时出现警告

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

显然,我可以忽略该消息并按[Quit]或者仅等待20秒左右,但是有没有一种干净的方法来告诉iTunes我已经完成使用它了?

Itunes 7.7.1, Windows XP

解决方案

回答

这是我在将Podcast添加到iTunes的Powershell脚本中所做的一件事。我在服务器上使用Juice下载我听的所有播客。该脚本使用.Net方法来释放COM对象。当我编写iTunes脚本时,我读了几篇文章,指出我们应该使用.NET释放COM对象。

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

我大部分时间还通过快捷方式而不是Powershell提示符运行脚本。

根据评论,我进行了一些测试,并确定如果我以使Powershell保持运行的方式运行脚本,则在针对iTunes运行时会收到消息。 iTunes似乎对此保持了追踪。以运行后退出进程的方式运行脚本,消除了该消息。

从powershell运行脚本的一种方法是在脚本中添加powershell前缀。

powershell .\scriptname.ps1

上面的命令将启动脚本,然后退出用于运行该脚本的进程,但仍将我们留在powershell提示符下。

回答

我们应该可以将$ itunes设置为$ null。另外,$ itunes应该具有可以调用的quit方法。 $ itunes.quit()