我是 AppleScript 新手,我只练习了几周。我编写了一段代码,用于将导入的 xml 文件(实际上更像是 plist)中的信息从音乐应用程序(以前的 iTunes)传输到新的音乐库,以获取按标题、专辑和艺术家关于参数 Play Count、Play 进行匹配的曲目约会,跳过计数,跳过日期,甚至爱过。
这段代码甚至可以工作,并且可能对某人有用。但是,我对其某些部分的语法正确性存有疑问。
-- Choose file plist или xml
set xmlFilePath to (choose file with prompt "Choose file iTunes Music Library" of type {"plist", "xml"})
set xmlTracksList to {}
set xmlTracksListRef to a reference to xmlTracksList
tell application "System Events"
tell property list file (POSIX path of xmlFilePath)
tell contents
set xmlTracks to value of property list item "Tracks"
set xmlTracksRef to a reference to xmlTracks
end tell
end tell
end tell
tell application "Music"
-- separate counter for the number of processed tracks
set processedTracksCounter to 0
-- setting progressbar parameters for reading data from XML
set theXMLitemCount to length of xmlTracks
tell me
set progress total steps to theXMLitemCount
set progress completed steps to 0
set progress description to "Analysing XML items..."
set progress additional description to "Preparing to analyse."
set xmlTracksIter to 1
end tell
repeat with t in items of xmlTracksRef
tell me
set progress additional description to "Processing xml items " & xmlTracksIter & " of " & theXMLitemCount
end tell
set xmlTrackInfo to {name:(|Name| of t), album:(|Album| of t), artist:(|Artist| of t), playCount:(missing value), playDate:(missing value), skipCount:(missing value), skipDate:(missing value), lovedStatus:(missing value)}
try
if class of (|Play Count| of t) is integer then set playCount of xmlTrackInfo to (|Play Count| of t)
end try
try
if class of (|Play Date UTC| of t) is date then set playDate of xmlTrackInfo to |Play Date UTC| of t
end try
try
if class of (|Skip Count| of t) is integer then set skipCount of xmlTrackInfo to |Skip Count| of t
end try
try
if class of (|Skip Date| of t) is date then set skipDate of xmlTrackInfo to |Skip Date| of t
end try
try
if class of (|Loved| of t) is boolean then set lovedStatus of xmlTrackInfo to |Loved| of t
end try
set end of xmlTracksListRef to xmlTrackInfo
tell me
set progress completed steps to xmlTracksIter
set xmlTracksIter to xmlTracksIter + 1
end tell
end repeat
-- Finding and setting values for each track in the list (array)
set libraryTracks to tracks
set libraryTracksRef to a reference to libraryTracks
-- setting progressbar parameters to display the number of processed tracks
set theTracksCount to length of libraryTracks
tell me
set progress total steps to theTracksCount
set progress completed steps to 0
set progress description to "Updating tracks info..."
set progress additional description to "Preparing to process."
set libraryTracksIter to 1
end tell
repeat with iTracks in libraryTracksRef
tell me
set progress additional description to "Processing track " & libraryTracksIter & " of " & theTracksCount
end tell
set libName to name of iTracks
set libAlbum to album of iTracks
set libArtist to artist of iTracks
repeat with t in xmlTracksListRef
set xmlTrackName to the name of t
set xmlAlbumName to the album of t
set xmlArtistName to the artist of t
set xmlPlayCount to playCount of t
set xmlPlayDate to playDate of t
set xmlSkipCount to skipCount of t
set xmlSkipDate to skipDate of t
set xmlLoved to lovedStatus of t
if xmlTrackName is equal to libName and ¬
xmlAlbumName is equal to libAlbum and ¬
xmlArtistName is equal to libArtist then
if xmlPlayCount is not missing value then set played count of iTracks to xmlPlayCount
if xmlPlayDate is not missing value then set played date of iTracks to xmlPlayDate
if xmlSkipCount is not missing value then set skipped count of iTracks to xmlSkipCount
if xmlSkipDate is not missing value then set skipped date of iTracks to xmlSkipDate
if xmlLoved is not missing value then
if xmlLoved is true then
set loved of iTracks to xmlLoved
else if xmlLoved is false then
set disliked of iTracks to true
end if
end if
set processedTracksCounter to processedTracksCounter + 1
exit repeat
end if
end repeat
tell me
set progress completed steps to libraryTracksIter
set libraryTracksIter to libraryTracksIter + 1
end tell
end repeat
-- Output of results (just for information)
display dialog "Moved information for " & processedTracksCounter & " matching tracks"
end tell
例如,检查某些曲目是否存在某个键(例如播放计数)。看来这里用
if exists
最合适。但这会导致错误,我不得不使用try
。但即使这种语法也没有给出所需的结果,我不得不即兴创作以下结果:
try
if class of (|Play Count| of t) is integer then set playCount of xmlTrackInfo to (|Play Count| of t)
end try
我无法在任何地方找到有关如何处理此类异常的信息,因此我使用了 try,并且为了它可以返回 true,我必须添加一个奇怪的构造
class of (|Play Count| of t) is integer
。也许对于这种情况有更简单的语法?
进度条的行为也有一些奇怪的地方。一般来说,它会正确显示进度。但由于某种原因,它不会在每次迭代后更新,而是仅在添加轨道信息时更新。尽管我将更新进度条的命令移到了进行搜索并在存在匹配时添加的循环之外。我的意思是最后一行:
。 重新创建媒体库并重新启动 macOS 后,进度条按预期工作。set progress completed steps to libraryTracksIter
希望得到更有经验的程序员的帮助
如果我很好理解,您希望在 playCount 不存在或不是整数时管理错误。那么我建议如下:
try
set playCount of xmlTrackInfo to (|Play Count| of t)
on error -- it only goes here is previous statment can't be executed
Set playCount to 0
end try