Thanks. Hope this works.
By the way, the variable _Title isn’t created inside the handler although the data gets determined there. It gets created when you set ‘_Title’ to the results of the handler. Obviously, you have to line up the various bits and pieces.
As to the object problem… take this code, put it in an empty script and run it. A song must be selected in Music for it to work (and it must be one that’s stored on the drive, not streamed).
tell application "iTunes"
set x to current track
--> file track id 123456 of user playlist id 4321 of source id 66
end tell
You can see that a reference to a song is part of a chain. So we have to figure out how to reference the Library (source) and the Music playlist (user playlist) before we can specify the individual track. Note that when you get a track, you will also get its playlist and library. I added a handler that gets the playlist ID for the ‘Music’ playlist.
Ultimately, setting the properties will look something like this:
set name of file track id 41539 of user playlist id 23420 of ¬
source id 66 to "123456789-ADP35ARG"
So here is what seems to work for me in itunes. Should work in Music as well.
use scripting additions
set musicPlaylist to getLibaryPlaylist()
--> user playlist id 34220 of source id 65
set myf to choose folder
tell application "Finder"
set rowCt to 1
set fileList to (files of myf whose name extension is "mp3") as alias list -- list of mp3 files as aliases
repeat with fl in fileList -- cycle through song files
set selectedTrack to contents of fl
--> alias "MacHD:Users:username:Desktop:qisum:0123456789-ADP35ARG.mp3"
set fileName to name of selectedTrack -- inside Music, the name of the track should not have any extension
--> "0123456789-ADP35ARG.mp3"
set AppleScript's text item delimiters to ".mp3"
set trackName to first text item of fileName -- strip extension from track name
set AppleScript's text item delimiters to {""}
set nomList to my getColumnData() -- list of mp3 file names
set rowCt to rowCt + 1 -- counter of rows in above list
repeat with lineItem in (get rest of nomList) -- ignores headings
if contents of lineItem is equal to fileName then -- find matching row in table
set eachTrack to my acquireMeta(rowCt) -- acquire metadata
log "eachtrack: " & eachTrack
--> {"0101 - Pauline McGregor", "ADP35ARG", "Adulte intro"}
tell application "iTunes"
activate
-- display dialog "Changing metadata"
-- Change the track's name, artist, and album
set ttn to track trackName of musicPlaylist
log ttn
set name of ttn to item 2 of eachTrack
set artist of ttn to item 1 of eachTrack
set album of ttn to item 3 of eachTrack
end tell
delay 0.2
exit repeat
end if
end repeat
end repeat
end tell
-- takes row number, returns NomFichierMusique, _Title, _Category, _Album
on acquireMeta(i)
--display dialog "acquireMeta(i)" & i
tell application "Numbers"
tell table 1 of active sheet of front document
set NomFichierMusique to value of cell i of column "B" -- NomFichierMusique
set _Title to value of cell i of column "D" -- Titre
set _Category to value of cell i of column "C" -- Catégorie
set _Album to value of cell i of column "E" -- Albun
end tell
end tell
return {_Title, _Category, _Album}
end acquireMeta
-- NB requires appropriate Numbers document
-- returns list of NomFichierMusique; looks for correct column then returns list including heading;
on getColumnData()
tell application "Numbers"
tell table 1 of active sheet of front document
set nfm to "NomFichierMusique"
set keyCol to column of first cell of row 1 whose value is nfm -- determine correct column
set tbl to value of cells of keyCol -- get values of column, including heading
--display dialog "getColumnData() tbl: " & tbl
{"NomFichierMusique", "0123456789-ADP35ARG.mp3", "1234567890-ASTAR5.mp3"}
if item 1 of tbl is "NomFichierMusique" then
set val to tbl -- set val to rest of tbl
--> {"0123456789-ADP35ARG.mp3", "1234567890-ASTAR5.mp3"}
else -- in case the table has changed
display dialog "This is actually the " & item 1 of tbl & " column"
end if
end tell
end tell
end getColumnData
-- NB requires appropriate Numbers document
-- get Music hierarchy
on getLibaryPlaylist()
tell application "iTunes"
set sid to source "Library"
set pm to playlist "Music" of sid
--> user playlist id 34220 of source id 65 of application "iTunes"
set sk to first item of (playlists whose special kind is Music)
if pm is equal to sk then
set musicPlaylist to sk
end if
end tell
return musicPlaylist
end getLibaryPlaylist