Modifying mp3 metada in reading information from OSX Numbers

I do have new metadata information stored in an OSX Numbers spreadsheet which I want to use to update the metadata of a mp3 music file.
I do not have any issue looping through the numbers spreadsheet. How er, editing the music file properties is a problem. Thanks in advance!

set folderpath to choose folder with prompt "Choose the folder containing the files to rename"
set myFolder to POSIX path of folderpath
--display dialog "myFolder: " & myFolder

tell application "Numbers"
	-- In Numbers the first table is Table 1.  It is hard to know this because the table most likely does not have its name appear, but the active sheet in a new Number file is Table 1.  There can be multiple tables on a sheet.  Therefore you need to designate the document (file) sheet, and table before you can access attributes
	tell table 1 of active sheet of front document
		
		-- For each row for the count of cells in the first column
		repeat with i from 2 to count of cells of column 1
			
			set NomFichierMusique to value of cell i of column "A" -- NomFichierMusiue
			set _Title to value of cell i of column "B" -- Titre
			set _Category to value of cell i of column "C" -- Catégorie
			set _Album to value of cell i of column "D" -- Albun
			
			set MusicFilename to quoted form of (myFolder & (NomFichierMusique))
			display dialog MusicFilename
			tell application "Music"
				set Title to _Title
				set album to text item of _Album
				set category to _Category
				set album to _Album
				open curPath
			end tell
			delay 8
		end repeat
		
	end tell
	
end tell
CSV file
NomFIchierMusique,Titre,Categorie,Album
0123456789-ADP35ARG.mp3,0101 - Pauline McGregor,ADP35ARG,Adulte intro
1234567890-ASTAR5.mp3,0102 - Mya Gosselin,ASTAR5,STAR 5
2345678901-STAR5P13.mp3,0103 -  Linda Burrough,STAR5P13,STAR 5 13 ans et plus
3456789012-PRENOV.mp3,0104 -  Elyzabeth Bro,PRENOV,Pré-Novice Prog. Libre

A few questions…

  1. Are these mp3 files already in the Music app?
  2. If so, is any of the spreadsheet data already entered? Most importantly, what is the title in the Music app?
  3. What is the media type for these files? Is it Music?

A quick suggestion… separate the spreadsheet work from the Music work. By this I mean, complete the Numbers repeat loop and then begin the Music repeat loop.

This may not be critical but what would the ‘artist’ be for these tracks in the Music app?

Thanks for the questions, you make me think further.

CONTEXT:
The mp3 files are not music file to be loaded permanently in a music library. Those are to be used within a sport event like a dancing competition. Each participants are to send their music file to the event organiser. Once they are ready to compete their music is being played. Those music files are only to exist for the duration of the event competition.

The need is to change what is displayed when looking at More Info under Get Info.

Title, Authors and Album are the metadata information to be modified by this AppleScript application.

Answering your questions:

  1. The mp3 files are not to be loaded into the Music library, not at the moment. The Music application is only to be used to play them. If, at one point, being loaded in the Music Library it would only be for the time of the event competition. They are to be destroyed after completion of the event competition.

  2. Music might not be the right application to be used for what I am trying to do.

  3. These files are Music media type.

I do not know which application I should be using to modify the metadata of each music file. All music filenames are loaded in Numbers. Manupilation of the spreadsheet and once completed the metadata is to be modified. To do so, in Numbers I am going one row at a time to apply modification of the metadata.

I’ve changed my code to the following. However, I am getting an " Error: File not found -".

The file is there. Does someone have an idea why am I getting this error?

set folderpath to choose folder with prompt "Choose the folder containing the files to rename"
set myFolder to POSIX path of folderpath
--display dialog "myFolder: " & myFolder

tell application "Numbers"
	-- In Numbers the first table is Table 1.  It is hard to know this because the table most likely does not have its name appear, but the active sheet in a new Number file is Table 1.  There can be multiple tables on a sheet.  Therefore you need to designate the document (file) sheet, and table before you can access attributes
	tell table 1 of active sheet of front document
		
		-- For each row for the count of cells in the first column
		repeat with i from 2 to count of cells of column 1
			
			set NomFichierMusique to value of cell i of column "B" -- NomFichierMusiue
			set newTitle to value of cell i of column "D" -- Titre
			set newArtist to value of cell i of column "C" -- Catégorie
			set newAlbum to value of cell i of column "E" -- Album
			
			set MusicFilename to quoted form of (myFolder & (NomFichierMusique))
			display dialog MusicFilename
			
			do shell script "/usr/local/bin/exiftool -title='" & newTitle & "' -album='" & newAlbum & "' -artist='" & newArtist & "' " & quoted form of MusicFilename
		end repeat
		
	end tell
	
end tell

Unfortunately, exiftool cannot write such tags (ID3) to mp3 files — it is read only.

You can confirm for your version by viewing the man page for exiftool under the Description in the File Types table. Or you could search for ‘MP3’ by typing the following and ‘enter’:

/MP3

So the ‘not found’ error is probably because you have two quoted form of: both in the .set MusicFilename to quoted form… line, and also in the do shell script… line.

Remove one and try again.

But then you will likely encounter an Writing of MP3 files is not yet supported error.

Two small points related to do shell script:

First, add a use scripting additions line to the top of the script.

Second, if you put the line inside a tell block, preface it with tell me to, like so:

`tell me to do shell script`…

It is hidden but you will generate a privilege error (-10004) otherwise.

You are correct, I did get an “Writing of MP3 files is not yet supported”.

I am back in using Music. I’ve tried this code and now have the following message : error “Can’t set name of "/" to "0101 - Pauline McGregor".” number -10006 from name of “/”



set folderpath to choose folder with prompt "Choose the folder containing the files to rename"
set myFolder to POSIX path of folderpath
--display dialog "myFolder: " & myFolder

tell application "Numbers"
	-- In Numbers the first table is Table 1.  It is hard to know this because the table most likely does not have its name appear, but the active sheet in a new Number file is Table 1.  There can be multiple tables on a sheet.  Therefore you need to designate the document (file) sheet, and table before you can access attributes
	tell table 1 of active sheet of front document
		
		-- For each row for the count of cells in the first column
		repeat with i from 2 to count of cells of column 1
			
			set NomFichierMusique to value of cell i of column "B" -- NomFichierMusiue
			set newTitle to value of cell i of column "D" -- Titre
			set newArtist to value of cell i of column "C" -- Catégorie
			set newAlbum to value of cell i of column "E" -- Album
			
			set MusicFilename to (myFolder & (NomFichierMusique))
			display dialog MusicFilename
			
			tell application "Music"
				-- Get the currently selected track
				set selectedTrack to first item of (get MusicFilename)
				
				-- Change the track's name, artist, and album
				set name of selectedTrack to newTitle
				set artist of selectedTrack to newArtist
				set album of selectedTrack to newAlbum
			end tell
			
			--do shell script "/usr/local/bin/exiftool -title='" & newTitle & "' -album='" & newAlbum & "' -artist='" & newArtist & "' " & quoted form of MusicFilename
		end repeat
		
	end tell
	
end tell

You’re trying to get something that wasn’t set, so it’s improvising.

The first item of command is actually getting the first character of the path string (ie MusicFilename). It is not getting the first file in the folder.

Run these lines in a new script and you should see what I mean.

set folderpath to choose folder with prompt "Choose the folder containing the files to rename"
set myFolder to POSIX path of folderpath
--> /Users/username/Desktop/musicalstuff/

items of myFolder
--> {"/", "U", "s", "e", "r", "s", "/",…

Here is one way that you might get the files:

set myf to choose folder
tell application "Finder"
	set fileList to (files of myf whose name extension is "mp3") as alias list
end tell

repeat with fl in fileList
	set selectedTrack to item 1 of fileList
end repeat

Here is a script that does most of what you want. It doesn’t do the tagging because I’m not really sure how you want to do that.

It requires a folder with the mp3 files and a Numbers spreadsheet with matching filenames. What it does is make a list of mp3 files and another of the matching metadata. It then cycles through the list of files and then grabs the corresponding data.

use scripting additions
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
	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
		--> "0123456789-ADP35ARG.mp3"		
		
		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
				--> {"0101 - Pauline McGregor", "ADP35ARG", "Adulte intro"}
				exit repeat
			end if
		end repeat
		
	end repeat
end tell


-- takes row number, returns NomFichierMusique, _Title, _Category, _Album
on acquireMeta(i)
	tell application "Numbers"
		tell table 1 of active sheet of front document
			
			set NomFichierMusique to value of cell i of column "A" -- NomFichierMusique
			set _Title to value of cell i of column "B" -- Titre
			set _Category to value of cell i of column "C" -- Catégorie
			set _Album to value of cell i of column "D" -- 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
			{"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 

Regarding the handlers, you can inspect them by putting either of them in a new script and removing the ‘on’ statements and then, for the metadata handler, setting a variable for the row number the spreadsheet to grab.

We are progressing.

I am now getting this error : Music got an error: Unknown object type. when executing

set name of selectedTrack to newTitle
set myf to choose folder with prompt "Choose the folder with MP3 metadata to be modified"
set myFolder to POSIX path of myf

tell application "Finder"
	set fileList to (files of myf whose name extension is "mp3") as alias list
	set itemCount to count of fileList
end tell

tell application "Numbers"

	tell table 1 of active sheet of front document
		
		repeat with i from 2 to count of cells of column 1
			
			set NomFichierMusique to value of cell i of column "B" -- NomFichierMusiue
			set newTitle to value of cell i of column "D" -- Titre
			set newArtist to value of cell i of column "C" -- Catégorie
			set newAlbum to value of cell i of column "E" -- Album
			set NumbersFilename to (myFolder & (NomFichierMusique))
			
			repeat with fl from 1 to count of fileList
				set selectedTrack to item fl of fileList
				set foldertrack to POSIX path of selectedTrack
				if NumbersFilename = foldertrack then
					tell application "Music"
						display dialog "Changing metadata"
						-- Change the track's name, artist, and album
						set name of selectedTrack to newTitle
						set artist of selectedTrack to newArtist
						set album of selectedTrack to newAlbum
					end tell
				end if
			end repeat
		end repeat
	end tell
end tell

That error is not a listed one.

Online, people think it relates to sandboxing.

But in this case, I think that it is because you are trying to feed the Music app an ‘alias’ object, and apparently this is foreign.

At that point, you have to work with Music objects and kit.

To Music, it is not a file, it is a track. So you have to change the name of the track, and to do that, you must specify the track.

For anyone who is interested in ffmpeg, I would recommend using one of the macos packaging tools to install, such as Macports (or the Fink or homebrew).

You might also find an ID3 editor through one of these services, which would be much lighter and more focused. ffmpeg is overkill for such a task.

And no quicktime player please, (unless you want to do a quick screen recording).

Wow ! Many Thanks!

This is a very nice code.

In the end I am going to use the Music App. I am getting error "The variable _Title is not defined.

I’ve added a display dialog in the «acquireMeta» and the title value does contain information. I do not understand why the return {_Title, _Category, _Album} does not return any information.

I’ve also added a display dialog right after the call of «acquireMeta» and nothing got returned. Everything seems to be ok and why nothing is being returned?

Many thanks!

use scripting additions
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
	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
		--> "0123456789-ADP35ARG.mp3"	
		--display dialog "Main() " & fileName
		
		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
				--> {"0101 - Pauline McGregor", "ADP35ARG", "Adulte intro"}
                               display dialog "Changing metadata selectedTrack: " & selectedTrack & "/nTitle: " & _Title & " Category: " & _Category & " Album: " & _Album
				tell application "Music"
					display dialog "Changing metadata"
					-- Change the track's name, artist, and album
					set name of selectedTrack to _Title
					set artist of selectedTrack to _Category
					set album of selectedTrack to _Album
				end tell
				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			
			display dialog "NomFichierMusique: " & NomFichierMusique & " _Title: " & _Title & " _Category: " & _Category & " _Album: " & _Album
		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 

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

For sure Exif Tool can write mp3 meta data

Best bet is Kid3 tag editor
Can work with CSV file and write meta data fairly easily.