Hello,
I'm new to AppleScript.
I recently needed a script for adding an album of audio files and associated tags to iTunes. The first version (using a single loop) can cause an "Applescript Error: iTunes got an error: File Permission Error -54." to occur. Which as I understand it, is a file open error - caused because my program is attempting to add the tags while iTunes is still processing one of the newly inserted audio files.
This is the listing for version one: ------ on export_to_iTunes(albumTitle, albumArtist, albumGenre, albumYear, tracklists) -- tracklists is an array of {title, artist, fileRef}
tell application "iTunes" repeat with trackNumber from 1 to count of tracklists by 1 set thisTrack to item trackNumber of tracklists set trackName to item 1 of thisTrack as string set trackArtist to item 2 of thisTrack as string set fileRef to item 3 of thisTrack as POSIX file set trackRef to add fileRef to playlist 1 set album of trackRef to albumTitle set album artist of trackRef to albumArtist set genre of trackRef to albumGenre set year of trackRef to (albumYear as number) set name of trackRef to trackName set artist of trackRef to trackArtist set track count of trackRef to count of tracklists set track number of trackRef to trackNumber end repeat end tell end export_to_iTunes ------
I managed to resolve this by dividing the addition of the audio files and the tags into two separate loops to create version two:
------ on export_to_iTunes(albumTitle, albumArtist, albumGenre, albumYear, tracklists) -- tracklists is an array of {title, artist, fileRef}
tell application "iTunes" set trackRefList to {} repeat with trackNumber from 1 to count of tracklists by 1 set thisTrack to item trackNumber of tracklists set fileRef to item 3 of thisTrack as POSIX file set end of trackRefList to add fileRef to playlist 1 end repeat repeat with trackNumber from 1 to count of tracklists by 1 set trackRef to item trackNumber of trackRefList set thisTrack to item trackNumber of tracklists set trackName to item 1 of thisTrack as string set trackArtist to item 2 of thisTrack as string set album of trackRef to albumTitle set album artist of trackRef to albumArtist set genre of trackRef to albumGenre set year of trackRef to (albumYear as number) set name of trackRef to trackName set artist of trackRef to trackArtist set track count of trackRef to count of tracklists set track number of trackRef to trackNumber end repeat end tell end export_to_iTunes ------
Now this second version seems to work without the error caused by version one, but now the audio files are added all over the iTunes library first - and then organised into one album as the tags are added in the second loop. Which is visually unappealing, as it gives the impression that the newly added tracks are out of order, until all the tracks have been added.
The first version is visually much more appealing (when it works) - as each audio file was added (almost immediately) to its correct album. It gave the user the visual feedback they were expecting - the tracks are added and organised in the same way as if they were ripping a CD.
So I'm after advice on how to prevent the "File Permission Error -54" from occurring in the first, single loop version.
Is there a way of checking that iTunes has finished with the audio file before adding its tags? or is there an even better way of resolving this problem?
Thanks in advance, Dave
|