题
在 PowersHell 中编写了以下内容作为快速 iTunes 演示:
$iTunes = New-Object -ComObject iTunes.Application
$LibrarySource = $iTunes.LibrarySource
foreach ($PList in $LibrarySource.Playlists)
{
write-host $PList.name
}
这效果很好,可以拉回播放列表名称列表。但是,在尝试关闭 iTunes 时会出现警告
一个或多个应用程序正在使用 iTunes 脚本接口。你确定你要退出吗?
显然,我可以忽略该消息并按[退出],或者只是等待 20 秒左右,但是有没有一种干净的方法来告诉 iTunes 我已经完成了它的工作?
Itunes 7.7.1, Windows XP
解决方案
这是我在 Powershell 脚本上所做的一件事,将播客添加到 iTunes。我在服务器上使用 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.退出()
不隶属于 StackOverflow