Album Artist from file folder to tag problem!

Hello,
Looking to make a script which will take the album artist from the file folder (example: Album Artist - Album) prior to the - (meaning only the album artist portion) and place this in the Album Artist tags through iTunes. So far the script works but only takes the file selected and puts the end of the filename (Not the file folder) as the Album Artist. Does anybody know how to make the file folder cut off at the - leaving only the album artist? Any help with truncating and removing the other portion of the file path would be a big help. Here is what works so far.


tell application "iTunes"
	set frontPlaylist to (get view of front window)
	if exists selection then
		copy selection to selectionTracks
	else
		copy (get every track of frontPlaylist) to selectionTracks
	end if
	
	repeat with thisTrack in selectionTracks
		if class of thisTrack is file track then
			set trackFilename to last item of my TracksToList((thisTrack's location as string), ":")
			copy trackFilename to thisTrack's album artist
		end if
	end repeat
end tell

on TracksToList(newText)
	set keepDelimiter to AppleScript's text item delimiters
	try
		set AppleScript's text item delimiters to "- "
		set newList to every text item of newText
	on error errorString number errorNumber
		set AppleScript's text item delimiters to keepDelimiter
		error errorString number errorNumber
	end try
	set AppleScript's text item delimiters to keepDelimiter
	return (newList)
end TracksToList

Check out Shane’s libraries

Meta
File tags
File manager

From:

http://macosxautomation.com/applescript/apps/Script_Libs.html#Metadata_Lib

Thank you very much for the link. After searching through the libraries the only part seeming viable is in the FileManageLib


use scripting additions
use theLib : script "FileManagerLib" version "2.2.1"

set theDesktop to path to desktop
set theContents to objects of theDesktop with searching subfolders without include folders
sort objects theContents sorted property modification property result type files list without low to high

With this portion seems possible to find the folder although removing the other portion of the path seems hard to do.

If anybody knows the solution through this library or any other work around to make the Album Artist portion of a folder name through the file path move into the Album Artist tag in iTunes can you share how?

Hey looking at your code in your repeat statement you call your handler with two variables
(string, delimiter) but you handler only takes one variable. I think you want:

on TracksToList(newText, splitDelimiter)
	set keepDelimiter to AppleScript's text item delimiters
	try
		set AppleScript's text item delimiters to splitDelimiter
		set newList to every text item of newText
	on error errorString number errorNumber
		set AppleScript's text item delimiters to keepDelimiter
		error errorString number errorNumber
	end try
	set AppleScript's text item delimiters to keepDelimiter
	return (newList)
end TracksToList

have a look a Doug’s Text Handlers for reference to your above handler:
https://dougscripts.com/itunes/scripts/spareparts.php#1046

Also do you have the following in iTunes preferences/advanced selected?
“Keep iTunes Media Folder Organized” ???
I’m guessing not, as if you do have this selected:
whatever you enter into into in iTunes info tag for the Album Artist
is what iTunes will automatically move it to and name this folder for you.

Also I don’t think you need to be doing all of that “copying”.
Check out Doug’s Applescripts for iTunes. Tons of great stuff there:
https://dougscripts.com/itunes/scripts

here he provide’s a basic handler for ::
Repeat with a reference to each of the selected tracks;
or, if no tracks are selected, repeat with a reference for each track in the currently selected playlist.

tell application "iTunes"
	set sel to selection
	if sel is not {} then
		repeat with aTrack in sel
			-- do something with aTrack
			
			--
		end repeat
	else
		set thePlaylist to view of the front browser window
		repeat with i from 1 to (index of last track of thePlaylist)
			set aTrack to track i of thePlaylist
			-- do something with aTrack
			
			--
		end repeat
	end if
end tell

here’s some code to insert into the repeat’s:

try
				set fixed indexing to true
				tell aTrack
					set album artist to aAlbumFolder
				end tell
			end try

it’s not that hard to “parse” the file info.
I think it’s available via the Standard Additions.
You just have to read thru the Dictionary to Figure Out what portion of the URL File path you need:

after glancing over many of Doug’s Script’s.

this is the one that seems to come closest to containing everything that you need.
I haven’t looked at the code (sometimes his code is not readable)…
and hopefully with this script you can turn off the options for the other stuff if you don’t need it
(as you were just looking for the Ablum)

but check out this script:
https://dougscripts.com/itunes/scripts/ss.php?sp=tagsmirrorartalbfnom

That’s neat. I had no idea that that preference performed that function; I had previously disabled it, as I observed that it annoyingly kept inserting numbers into my track names.

Nasir: I’m not sure how your library is arranged, as I have no dashes in my location aliases. Examples of one or more of yours might be helpful. I’ve written a simpler alternative for you to try, if technomorph hasn’t already solved your problem.

set text item delimiters to ":"

tell application "iTunes" to repeat with aTrack in playlist 1's file tracks
	#next line for testing; disable after test complete
	display dialog my obtainArtistfrom(aTrack's location)
	
	#enable when satisfied with testing
	--set aTrack's album artist to my obtainArtistfrom(aTrack's location)
end repeat

set text item delimiters to ""

on obtainArtistfrom(locus)
	(locus as text)'s text item -3
end obtainArtistfrom

here is doug’s full script.
So you may want to edit out the parts I’ve marked below in --//

tell application "iTunes"
	set s to selection
	if s is not {} then
		--//MAY NOT NEED THIS DIALOG//display dialog "\"Tags Mirror Artist-Album-Filename\"" & return & return & ¬
			"This script will copy each selected track's filename, Album folder and Artist folder to its respective tag." & return & "The folders must be arranged in this manner:" & return & return & "Artist / Album / Songfile.xxx" buttons {"Cancel", "Continue..."} default button 2
		
		set oldfi to fixed indexing
		set fixed indexing to true
		repeat with i from 1 to length of s
			set t to item i of s
			if class of t is file track then
				tell t
					try
						if location is not missing value then
							set loc to (get location as text)
							set b to my text_to_list(loc, ":")
							--//YOU DON'T NEED FILENAME/TRACK NAME//set n to my fixextension(loc)
							--//try
								--//set name to n
							--//end try
--//NOTE I EDITED THE NEXT LINE SO IT"S album artist and not album
							try
								set album artist to item -2 of b
							end try
							--//YOU DON'T NEED THE ARTIST//try
								--//set artist to item -3 of b
							--//end try
						end if -- is file track and not missing?
					end try
				end tell
			end if -- file track?
		end repeat
		-- done
		set fixed indexing to oldfi
		--//YOU MAY NOT NEED THIS DIAL//try
			--//display dialog "Done!" buttons {"Thanks"} default button 1
		--//end try
	else -- no tracks selected
		display dialog "You must select some tracks first." buttons {"Cancel"} default button 1 with icon 0 giving up after 30
	end if
end tell

to fixextension(loc)
	set finfo to (get info for alias loc)
	return (text 1 through -((length of (name extension of finfo)) + 2) of name of finfo) as text
end fixextension

on text_to_list(txt, delim)
	set saveD to AppleScript's text item delimiters
	try
		set AppleScript's text item delimiters to {delim}
		set theList to every text item of txt
	on error errStr number errNum
		set AppleScript's text item delimiters to saveD
		error errStr number errNum
	end try
	set AppleScript's text item delimiters to saveD
	return (theList)
end text_to_list

Here’s a full trimmed down code, with a few variables renamed for easy of view.
And allowing either selected tracks, or just a selected playlist to be chosen:


(I’m actually not 100% sure if this will work as it’s just created a great new question for me
about TELL blocks, whether or not the “TELL” to iTunes will continue onto the handler.
EDIT: I cleaned up the tab formatting of the script. And after trying it out it didn’t work
I did have to add:

tell application "iTunes'
-- code
end tell

into the handler my LPfinderFOLDERtoLPartist


tell application "iTunes"
	set sel to selection
	if sel is not {} then
		repeat with aTrack in sel
			my LPfinderFOLDERtoLPartist(aTrack)
		end repeat
	else
		set thePlaylist to view of the front browser window
		repeat with i from 1 to (index of last track of thePlaylist)
			set aTrack to track i of thePlaylist
			my LPfinderFOLDERtoLPartist(aTrack)
		end repeat
	end if
end tell

on LPfinderFOLDERtoLPartist(aTrack)
	tell application "iTunes"
		if class of aTrack is file track then
			tell aTrack
				try
					if location is not missing value then
						set loc to (get location as text)
						set pathList to my text_to_list(loc, ":")
						try
							set album artist to item -2 of pathList
						end try
					end if -- is file track and not missing?
				end try
			end tell
		end if -- file track?
	end tell
end LPfinderFOLDERtoLPartist

to fixextension(loc)
	set finfo to (get info for alias loc)
	return (text 1 through -((length of (name extension of finfo)) + 2) of name of finfo) as text
end fixextension

on text_to_list(txt, delim)
	set saveD to AppleScript's text item delimiters
	try
		set AppleScript's text item delimiters to {delim}
		set theList to every text item of txt
	on error errStr number errNum
		set AppleScript's text item delimiters to saveD
		error errStr number errNum
	end try
	set AppleScript's text item delimiters to saveD
	return (theList)
end text_to_list


Thank you all for the help! Technomorph’s last script is very close to perfect as this find the album folder and puts this in the Album Artist tag. Do you know of a way to parse the folder so the tag stops prior to - ? Here is an example of the folders.

This is how things look in iTunes.

Marc yeah it’s super handy. There are two ways to disable the “Add Disc No & Track No to the name”

  1. use ONYX and set the preference via:
  • ONYX parameters TAB → iTunes TAB → uncheck IMPORTING → add Track Numbers
  1. via setting the iTunes preferences via terminal:

quit iTunes, open Terminal (in /Applications → Utilities), and type these two commands, pressing Return after each line:

defaults write com.apple.iTunes create-filenames-with-disknumber -bool FALSE
defaults write com.apple.iTunes create-filenames-with-tracknumber -bool FALSE

Hi Nasir,
when I’ve logged what the pathList creates I get this:
{“Tekno”, “Users”, “kerry”, “Music”, “iTunes”, “iTunes Media”, “Music”, “The Rolling Stones”, “Unknown Album”, “You Can’t Always Get What You Want (Soulwax Remix).mp3”}

so we actually want to set the album artist to item -3 NOT item -2
try changing the handler to this:

LPfinderFOLDERtoLPartist(aTrack)
	tell application "iTunes"
		if class of aTrack is file track then
			tell aTrack
				try
					if location is not missing value then
						set loc to (get location as text)
						set pathList to my text_to_list(loc, ":")
						try
							set album artist to item -3 of pathList
						end try
					end if -- is file track and not missing?
				end try
			end tell
		end if -- file track?
	end tell
end LPfinderFOLDERtoLPartist

BUT PLEASE NOTE: after you have already run other code on it.
It’s not going to work because. You’ve now already changed the path.

As I noted above, please let me know about the status of your iTunes preferences.
If you have the preference to keep media file organized then any time you change any of the following:
Song(title), Artist, Album, Ablum Artist, Part of a Compilation

then iTunes will both rename and relocate the file for you.
So changing the Album Artist based on the Location will affect both?
You see the cycle your going to get yourself trapped into?

Can you explain the reason why you are wanting to set the Album Artist based on the File/Folder location?

To “Parse Text” into separate items you’ll want to use the same text_to_list handler.
the “delimiter” that you pass to it is where it will split the text into a list.

so if you have “Prodigy - Return of the Mack”


set fullTEXT to "Prodigy - Return of the Mack"
set newDELIM to " - "
set testLIST to my text_to_list(fullTEXT, newDELIM)
-- {"Prodigy", "Return of the Mack"}

set part01A to item 1 of testLIST
-- "Prodigy"

set part01B to item -2 of testLIST
-- "Prodigy"

set part02A to item 2 of testLIST
-- "Return of the Mack"

set part02B to item -1 of testLIST
-- "Return of the Mack"

just be careful if you have other dashes in your text:


set fullTEXT to "Prodigy - Live at Fabric - Part 01"
set newDELIM to " - "
set testLIST to my text_to_list(fullTEXT, newDELIM)
-- {"Prodigy", "Live at Fabric", "Part 01"}
set part01A to item 1 of testLIST
-- "Prodigy"
set part01B to item -2 of testLIST
-- "Live at Fabric"
set part02A to item 2 of testLIST
-- "Live at Fabric"
set part02B to item -1 of testLIST
-- "Part 01"
set fullTEXT to "Prodigy - Bootlegs and Remixes-Disc 01"
set newDELIM to " - "
set testLIST to my text_to_list(fullTEXT, newDELIM)
-- {"Prodigy", "Bootlegs and Remixes-Disc 01"}
set part01A to item 1 of testLIST
-- "Prodigy"
set part01B to item -2 of testLIST
-- "Prodigy"
set part02A to item 2 of testLIST
-- "Bootlegs and Remixes-Disc 01"
set part02B to item -1 of testLIST
-- "Bootlegs and Remixes-Disc 01"

and of course once you know what list item you want you can shorten your code:

set fullTEXT to "Prodigy - Bootlegs and Remixes-Disc 01"
set artistNAME to item 1 of (my text_to_list(fullTEXT, " - "))

Thank you so much for the continued help technomorph! After reading your code and changing one line, the set AppleScripts text delimiters to “-” which now goes prior to the - for the Album Artist. This works just as I was hoping. Knowing about the other dashes in the song names means having to fix a few, though this is welcome due to the volume of music which will be much quicker with this script. iTunes is not set to organize the folders as this has been done by me with different naming scheme as such Album Artist - Album name.


on text_to_list(txt, delim)
	set saveD to AppleScript's text item delimiters
	try
		set AppleScript's text item delimiters to "-"
		set theList to every text item of txt
	on error errStr number errNum
		set AppleScript's text item delimiters to saveD
		error errStr number errNum
	end try
	set AppleScript's text item delimiters to saveD
	return (theList)
end text_to_list


Well after trying a few more albums the results are coming back much different. This seems to work on some of the albums but this is focusing on the files more than the folder. The last way you are showing would seem to be tedious to type in every folder and separate parse at the -. Do you know of a way to not look at the files when copying the folder in as metadata and take only the folder information prior to the -?

I’ll have a look at your question more when I’m
Not on my mobile.

One thing isn’t to make sure you are using the delimiter " - "
With the space before and after and not just “-” as that will give you problems.

Also before I answer any further questions can you please answer those other questions

  1. do you have the preference set “keep iTunes media organized”
  2. what and why are you wanting to do ultimately here?

If you do have the 1) selected
Getting the file info via iTunes will always have the path structure when you seperate it into a list as:
Last item: song name
Item -2 : LP name
Item -3 : LP artist

If it is not set that way the you’ll need to use some
File (finder) libraries / methods
To get the folder name that the file is in.

But like I’ve said If 1) is checked
Anytime you change any of the following in iTunes:
The track name, artist or Album artist, Album
It will rename and move the file and folders it’s in:
Into your iTunes Media/Music/
Artist(or LP Artist) folder/LP folder/track name

Good morning. The answer to the organization preference question was negatory in post #15. From the supplied image, it looks like the artist info is already present in iTunes, so I’m not sure why the file is being used as a data source. If the artist and album artist match, the data can simply be copied from field to field. If the data diverges somewhere, a second delimiter cycle can be used against the file’s item -3. You should always watch the results in Script Editor’s event log; it provides useful feedback to help you understand, apply, and troubleshoot the technique.

Check out dougs scripts for iTunes.
Find this tag that tag.
You can use it to swap tags.

If you endable that keep iTunes organized
It will rename and relocate things for you based on:

  • folder named by artist (or album artist if it exists)
  • folder names by album (“unknown” if empty)
  • file named by track name