0

I have an issue with my music library. Some songs I am unable to play because they cannot be found locally. Here's an example of the error messages I get when playing a specific song:

The song ... could not be used because the original file could not be found. Would you like to locate it?

enter image description here

I can simply press Cancel and the song will be matched via the Apple Music Service. This allows me to then play the song.

This issue has been discussed here, albeit not in an automated way. Hence, I would like to find an automated solution.

For this, I took the approach of looping through my library by playing each song. By default, if a song cannot be found, the script automatically skips to the next song. However, I would like the script to deal with the "file not found" errors and press Cancel.

My current attempt unfortunately does not work:

-- Play first song in library (turn off shuffle and repeat)
set i to 4000 --number of songs in library
repeat while i > 0
    tell application "Music" to play (next track)
    tell application "System Events"
        key code 53
    end tell
    set i to i - 1
end repeat

How can I force the script to deal with these pop-up errors?

Note: I am also open to any other, more efficient solution to my problem if you have any suggestions. I decided not to go for the Locate option because it takes more time and I will delete any unreferenced songs from my disk at a later stage anyways.

calpyte
  • 855
  • 1
  • 9
  • 17
  • I'm on Sierra so I still use itunes. When you add missing purchased music to a playlist, it asks if I wish to download a copy as it is required for adding to playlists. If Music has this same feature, you could just create a playlist and add all of your albums to it. As far as I can tell, it asks once for the whole add process so maybe you could save some effort this way. – Mockman Jun 07 '21 at 05:51
  • @Mockman thanks for the suggestion. I am running Catalina version 10.15.7 and it does not prompt that notification. I still have to play a song to find out it has no local file and to then force a re-download. – calpyte Jun 07 '21 at 15:46
  • Do you have all of the 'automatic' download options checked? In itunes, there are preferences such as 'automatic downloads', 'always check for available downloads'. In the help section 'Download previous purchases from the itunes store', it specifies how this works and has link for 'redownload music' as well. Maybe Music's help has similar information. Also, what happens if you right-click a song with '!', does it offer download? – Mockman Jun 07 '21 at 21:52
  • @Mockman I have both options (`automatic downloads` and `always check for available downloads`) ticked. Right clicking on a track with `!` shows the same options as for a song without the exclamation mark. – calpyte Jun 08 '21 at 17:48
  • Hmm, this is a tough issue for me to replicate. BTW, two suggestions for your script… Frst, put two lines above the 'tell system events' line: 'activate'; delay 1; (with ; being end of line) — and while testing, you should lower your 4000 to 10 or something like that. This will make Music active and give it time for the button press to work. – Mockman Jun 10 '21 at 04:02
  • Second, re-order your script so that the repeat loop is inside the Music tell block. BTW, you need to activate Music for the key code to work. This works for me if I open the 'info' window for each track as it begins to play. If it works, play around with the delay length: `tell application "Music"`; `set i to 4`; `activate`; `play track 1 of playlist 2`; `repeat while i > 0`; `next track`; `delay 1`; `tell application "System Events" to key code 53`; `set i to i - 1`; `end repeat`; `end tell`; – Mockman Jun 10 '21 at 04:36
  • @Mockman thanks for the suggestions. It's indeed tough to replicate the issue, it's not like I can share a toy example... But I have taken into consideration your suggested code changes. Unfortunately I cannot get the script to properly deal with the Cancel button. – calpyte Jun 10 '21 at 18:36
  • Nature of the beast. Looks like you've got a solution though so that's good. I can't run what you're running and Apple doesn't even make it possible to access the dictionary so…. If you plan on continuing to play around with it, try and get Music to cycle through songs and increase the delay to 4 seconds or so. Then as each song begins, type Cmd-R (to select the playing song) and then Cmd-I (to open the info window) and then see if you can get the script to cancel. Then maybe you can transfer that functionality to the other dialogue. Good luck. – Mockman Jun 10 '21 at 19:14

1 Answers1

0

UPDATED VERSION. Following script doesn't play tracks and programatically clicks button "Cancel" when track is corrupted. Like described by OP fixing tracks manually workflow:

tell application "Music"
    activate -- required
    tell source "Library"
        repeat with nextTrack in tracks
            try
                with timeout of 2 seconds
                    set theResult to play (contents of nextTrack)
                end timeout
                theResult
            on error
                delay 1
                tell application "System Events" to tell process "Music" to tell window 1 to if UI element "Cancel" exists then click UI element "Cancel"
            end try
            stop
        end repeat
    end tell
end tell
Robert Kniazidis
  • 1,760
  • 1
  • 7
  • 8
  • My objective is to fix the errors as quickly as possible, so I don't actually want to listen to the songs. Starting to play a song is just the easiest way to trigger the error message, as far as I discovered. Besides that there are syntax errors in you script, would it be possible to loop only through a certain number of songs without shuffle and then terminate the script? I can't stop the script and it only terminates once it has gone through my entire library. – calpyte Jun 09 '21 at 18:29
  • Hello again, @calpyte. There are no syntax errors in my script. Otherwise it wouldn't even compile, let alone run through and play your entire library. Another thing is that it is difficult to understand a person who cannot express his thought. It is still not clear what you want to do after the pop-up message **self-collapses**. By the way, didn't you originally ask for self-folding of the message? – Robert Kniazidis Jun 10 '21 at 14:16
  • I updated the answer, because now it is more clear what you want. – Robert Kniazidis Jun 10 '21 at 15:14
  • Thanks Robert for the update. It loops nicely through the library and I can see `Cancel` turn blue and the error message disappear. I let the script run until it encountered the first few error messages and then stopped it to investigate if the `!` disappeared. Unfortunately that was not the case. I still had to play the song manually and press `Cancel` manually to prompt the download option. Not sure why the automatic approach does not have the same effect as doing it manually. I accepted your answer though because it does the steps I asked for, even if the outcome is not as expected. – calpyte Jun 10 '21 at 18:40