I have a mac music library consisting of local MP3s and iCloud tracks, which are online only or saved encrypted as an M4a file (if I chose to keep the locally). I want to delete any given iCloud track with a script. However, according to ChatGPT, iCloud tracks are somehow protected because they are bound to the iCloud account. Is there any way I can still delete them?
Below you can find the script I am trying to run:
import Foundation
func deleteLocalTrack(named trackName: String, by artistName: String) {
let scriptSource = """
tell application "Music"
repeat with aTrack in (tracks of library playlist 1 whose name is "\(trackName)" and artist is "\(artistName)")
if location of aTrack is not missing value then
delete aTrack
end if
end repeat
end tell
"""
let script = NSAppleScript(source: scriptSource)
var error: NSDictionary?
script?.executeAndReturnError(&error)
if let error = error {
print("Error: \(error)")
} else {
print("Local track(s) by \(artistName) named '\(trackName)' deleted (if found).")
}
}
// Usage
deleteLocalTrack(named: "MySong", by: "Artist Name")