I have a simple AppleScript for starting a random video on a random position in Music.app as Automator application and as a startup login item. In the half of cases I get an error:
The action "Run AppleScript" encountered an error: "Music got an error: Can't get duration of current track."
If I run it in the Script Editor it executes correctly.
I tried to add "delay 5" before the line "set Y to duration of current track" but it didn't help. Also tried to add "if" condition to quit Music.app if it is running before starting the script, but it also didn't change anything.
tell application "Music"
run
set song repeat to all
set shuffle enabled to true
play playlist "Music Videos"
delay 1
set Y to duration of current track
set X to random number from 0 to Y
set player position to X
activate
end tell
The error often appears on the next day if system was shutdown with the script:
tell application "Finder" to shut down
I guess this is somehow related to the running state of Music.app on the moment of shutting down the system, since after system startup all apps return to the same state they were before shutting down, e.g. if I close all my apps and restart the system using Apple logo > Restart the script will be executed correctly and I won't see any errors on the screen.
macOS Ventura 13.4
Thanks a lot