Applescript 在 iTunes 中超时
Posted
技术标签:
【中文标题】Applescript 在 iTunes 中超时【英文标题】:Applescript timed out in iTunes 【发布时间】:2012-10-06 05:00:40 【问题描述】:我写了一个简单的applescript来自动将文件添加到iTunes中并填充元数据。当我直接从编辑器运行它时,它可以工作,但从 iTunes 运行它,我会得到“AppleEvent Timed Out”。
代码如下:
set mainFolder to choose folder
tell application "Finder"
-- Loop through all shows
set shows to every folder of mainFolder
repeat with show from 1 to count of shows
-- Set Show Name
set showName to name of item show of shows
-- Set Artist
display dialog "Who is the artist for " & showName & "?" default answer showName
set showArtist to the text returned of the result
-- Set Genre
display dialog "What is the genre for " & showName & "?" default answer ""
set showGenre to the text returned of the result
-- Loop through all season
set seasons to every folder in item show of shows
repeat with season from 1 to count of seasons
set seasonName to name of item season of seasons
-- Set Season Number
set seasonNumber to text 1 thru ((offset of "-" in seasonName) - 2) of seasonName as integer
-- Set Year
display dialog "What year was Season " & seasonNumber & " of " & showName & " in?" default answer "2012"
set showYear to the text returned of the result
-- Set Season Name
set seasonName to text ((offset of "-" in seasonName) + 2) thru ((offset of "." in seasonName) - 1) of seasonName as text
-- Set Total Episodes in Season
set totalEpisodes to count of every file in item season of seasons
-- Loop through all episodes
set episodes to every file in item season of seasons
repeat with episode from 1 to count of episodes
set episodeName to name of item episode of episodes
-- Set Episode Number
set episodeNumber to text 1 thru ((offset of "-" in episodeName) - 2) of episodeName as integer
-- Set Episode Name
set episodeName to text ((offset of "-" in episodeName) + 2) thru ((offset of "." in episodeName) - 1) of episodeName as text
tell application "iTunes"
set newAddition to (add (item episode of episodes as alias))
tell newAddition
set video kind to TV show
set name to episodeName
set album to seasonName
set track number to episodeNumber
set track count to totalEpisodes
set disc number to "1"
set disc count to "1"
set show to showName
set season number to seasonNumber
set episode number to episodeNumber
-- Manual Entries
set artist to showArtist
set genre to showGenre
set year to showYear
-- Change episode ID based on season and episode number
if (seasonNumber < 10) then
if (episodeNumber < 10) then
set episode ID to ("S0" & seasonNumber as text) & "E0" & episodeNumber as text
else
set episode ID to ("S0" & seasonNumber as text) & "E" & episodeNumber as text
end if
else
if (episodeNumber < 10) then
set episode ID to ("S" & seasonNumber as text) & "E0" & episodeNumber as text
else
set episode ID to ("S" & seasonNumber as text) & "E" & episodeNumber as text
end if
end if
end tell -- End newAddition
end tell -- End iTunes
end repeat -- End Episode Repeat
end repeat -- End Season Repeat
end repeat -- End Show Repeat
end tell -- End Finder Repeat
【问题讨论】:
【参考方案1】:假设您在计算文件名的偏移量和不同组成部分时没有任何错误,代码本身似乎是合理的。但是,我可以看到 2 个可能的错误来源可以解决您的问题。
首先,您在 Finder 中设置了 iTunes 的 tell 代码块。本质上,您是在告诉 Finder 告诉 iTunes 做某事。这是可能的错误的来源。您应该将您的告诉块彼此分开。比如你有这个...
tell application "Finder"
-- do something
tell application "iTunes"
-- do something
end tell
end tell
当你应该拥有它的时候......
tell application "Finder"
-- do something
end tell
tell application "iTunes"
-- do something
end tell
其次,您的“episode id”代码中的括号有误。比如这个……
("S0" & seasonNumber as text)
应该是这个……
"S0" & (seasonNumber as text)
因此,我已将 iTunes 内容分离到一个子例程中并修复了括号。请注意,我尚未测试此代码。我想我将所有正确的变量都传递给了子程序,但我不能确定。我希望这会有所帮助。
set mainFolder to choose folder
tell application "Finder"
-- Loop through all shows
set shows to every folder of mainFolder
repeat with show from 1 to count of shows
-- Set Show Name
set showName to name of item show of shows
-- Set Artist
display dialog "Who is the artist for " & showName & "?" default answer showName
set showArtist to the text returned of the result
-- Set Genre
display dialog "What is the genre for " & showName & "?" default answer ""
set showGenre to the text returned of the result
-- Loop through all season
set seasons to every folder in item show of shows
repeat with season from 1 to count of seasons
set seasonName to name of item season of seasons
-- Set Season Number
set seasonNumber to text 1 thru ((offset of "-" in seasonName) - 2) of seasonName as integer
-- Set Year
display dialog "What year was Season " & seasonNumber & " of " & showName & " in?" default answer "2012"
set showYear to the text returned of the result
-- Set Season Name
set seasonName to text ((offset of "-" in seasonName) + 2) thru ((offset of "." in seasonName) - 1) of seasonName as text
-- Set Total Episodes in Season
set totalEpisodes to count of every file in item season of seasons
-- Loop through all episodes
set episodes to every file in item season of seasons
repeat with episode from 1 to count of episodes
set episodeName to name of item episode of episodes
-- Set Episode Number
set episodeNumber to text 1 thru ((offset of "-" in episodeName) - 2) of episodeName as integer
-- Set Episode Name
set episodeName to text ((offset of "-" in episodeName) + 2) thru ((offset of "." in episodeName) - 1) of episodeName as text
my addEpisodeToItunes((item episode of episodes) as alias, seasonName, seasonNumber, episodeName, episodeNumber, totalEpisodes, showName, showArtist, showGenre, showYear)
end repeat -- End Episode Repeat
end repeat -- End Season Repeat
end repeat -- End Show Repeat
end tell -- End Finder Repeat
on addEpisodeToItunes(theEpisode, seasonName, seasonNumber, episodeName, episodeNumber, totalEpisodes, showName, showArtist, showGenre, showYear)
tell application "iTunes"
set newAddition to add theEpisode
tell newAddition
set video kind to TV show
set name to episodeName
set album to seasonName
set track number to episodeNumber
set track count to totalEpisodes
set disc number to "1"
set disc count to "1"
set show to showName
set season number to seasonNumber
set episode number to episodeNumber
-- Manual Entries
set artist to showArtist
set genre to showGenre
set year to showYear
-- Change episode ID based on season and episode number
if (seasonNumber < 10) then
if (episodeNumber < 10) then
set episode ID to "S0" & (seasonNumber as text) & "E0" & (episodeNumber as text)
else
set episode ID to "S0" & (seasonNumber as text) & "E" & (episodeNumber as text)
end if
else
if (episodeNumber < 10) then
set episode ID to "S" & (seasonNumber as text) & "E0" & (episodeNumber as text)
else
set episode ID to "S" & (seasonNumber as text) & "E" & (episodeNumber as text)
end if
end if
end tell -- End newAddition
end tell -- End iTunes
end addEpisodeToItunes
【讨论】:
感谢您抽出宝贵时间回答我的问题。我试过你的代码,你指出了一些好的错误。不幸的是,您的脚本遇到了与我的脚本相同的问题。如果我直接从编辑器运行它,它工作正常,如果我从 iTunes 运行它,它会显示“AppleEvent Timed It”。【参考方案2】:我是这样解决的:
另存为应用程序而不是脚本。
我从这里得到了解决方案: http://forums.ilounge.com/applescripts-itunes-mac/245120-need-help-add-tracks-files-via-applescript.html
我不知道为什么会这样,我试过使用 try-catch,超时......仍然没有工作。它可以作为一个应用程序运行吗?!
【讨论】:
以上是关于Applescript 在 iTunes 中超时的主要内容,如果未能解决你的问题,请参考以下文章
使用 Applescript 在 iTunes 中搜索和删除特定曲目
用于刷新单个 iTunes 播客的 Applescript 代码