ITunes truncates MP3s. I know the fix, but I want to automate it with AppleScript

applescriptbugitunes

iTunes has started truncating the MP3s that I add. Who knows why. They tend to get cut off at 4:10. There's a discussion, and a fix, here

The amazing problem-solver, abuckiew, gives an AppleScript to loop through the songs and play each one of them briefly.

If anyone uses my workaround I use the following Apple script after
adding files to my library to let each song play for appx 1 second
(change 5 to however many songs you have added):

delay 1
activate application "iTunes"
tell application "System Events"
  repeat 5 times
  key code 124
  delay 1.5
  end repeat
end tell

But I want to improve on this. What I'm looking for is an AppleScript that will remove songs from iTunes (either songs selected or a whole playlist) and then re-add them, and play each one for 1 second. I'm almost there! This is what I've got:

global okflag, selectedTracks
set separator to "."
set okflag to false
-- check if iTunes is running 
tell application "Finder"
    if (get name of every process) contains "iTunes" then set okflag to true
end tell
if okflag then
    try
        tell application "iTunes"
            copy (a reference to (get view of front window)) to thePlaylist
            -- check if "Library"; later, we will not offer to number a Library playlist
            set using_the_Library to false
            if (class of thePlaylist) is library playlist then set using_the_Library to true

            if item 1 of selection exists then
                set using_selection to true
                copy (selection's items) to theTracks
            else -- its the whole playlist
                set selectedTracks to (get a reference to thePlaylist)
                copy (thePlaylist's tracks) to theTracks
                set using_selection to false
            end if

            repeat with i from 1 to (count theTracks)

                if using_selection then
                    copy item 1 of selection to thisTrack
                else
                    copy track 1 of selectedTracks to thisTrack
                end if
                -- thisTrack now set
                copy (get thisTrack's location) to location_in_finder

                delete (some track of library playlist 1 whose database ID is (get database ID of thisTrack))
                add location_in_finder to library playlist 1

            end repeat
        end tell -- iTunes
    on error error_message number error_number
        tell me to display dialog error_message & return & "Error number " & error_number
    end try
end if -- okflag

This script takes the selected tracks (or the whole playlist, if nothing's selected) and removes the files from iTunes and then re-adds them. What I need to do is to play each re-added track for 1 second. How can I get a reference to the file I've re-added, so I can tell iTunes to play it?

Thanks in advance!

Best Answer

Figured it out!

Here's the relevant line: set newTrack to add location_in_finder to library playlist 1. I hadn't realized that add track returned a reference to the track added, but the iTunes AppleScript dictionary showed me the light.

The Final Script is below:

global okflag, selectedTracks, newTrack
set separator to "."
set okflag to false
-- check if iTunes is running 
tell application "Finder"
    if (get name of every process) contains "iTunes" then set okflag to true
end tell
if okflag then
    try
        tell application "iTunes"
            copy (a reference to (get view of front window)) to thePlaylist
            -- check if "Library"; later, we will not offer to number a Library playlist
            set using_the_Library to false
            if (class of thePlaylist) is library playlist then set using_the_Library to true

            if item 1 of selection exists then
                set using_selection to true
                copy (selection's items) to theTracks
            else -- its the whole playlist
                set selectedTracks to (get a reference to thePlaylist)
                copy (thePlaylist's tracks) to theTracks
                set using_selection to false
            end if

            repeat with i from 1 to (count theTracks)

                if using_selection then
                    copy item 1 of selection to thisTrack
                else
                    copy track 1 of selectedTracks to thisTrack
                end if
                -- thisTrack now set
                copy (get thisTrack's location) to location_in_finder

                delete (some track of library playlist 1 whose database ID is (get database ID of thisTrack))
                set newTrack to add location_in_finder to library playlist 1
                play newTrack
                delay 1.5
                stop

            end repeat
        end tell -- iTunes
    on error error_message number error_number
        tell me to display dialog error_message & return & "Error number " & error_number
    end try
end if -- okflag