Copy folder name to album for imported songs

GO TO ADMIN PANEL > ADD-ONS AND INSTALL VERTIFORO SIDEBAR TO SEE FORUMS AND SIDEBAR

fusk

New member
Joined
Jul 12, 2008
Messages
5
Points
0
Hi there, i'm quite new at this, but i did search google, this forum and doug's applescript before i made this post.

So i got a big load of music, around 84gb of music, and i'd like to import that in itunes, drag'n drop, i know, but there is one flaw with that.
See if i import a mixed album where there is no id3 tag, then there is no album name in those x tracks, and then they are just scatterd around in itunes, can't sort by album since there is no album name.
So, what i'm looking for is a script that copys the foler name "disco hits 3" into the album tag of those x tracks.
So a mixed album with 10 tracks from 10 diffrent albums would have album "disco hits 3" insted of nothing.

hope you understand :)

thank you
 

S2_Mac

New member
Joined
Oct 24, 2006
Messages
4,878
Points
0
Location
About 3 feet in front of the monitor
P'bly easier all around to just do it by hand. Turn off the "Copy files to iTunes Music folder" and "Keep iTunes Music folder organized" prefs at Preferences -> Advanced -> General. Copy the files onto your drive wherever you want them to live (including the "iTunes Music" folder). Then use File -> Add to Library... to add them; they'll stay in whatever folder they're in ("disco hits 3"). Set the two preferences back the way you normally use 'em.

Then just add a Date Added column to iTunes' display and sort by date added -- all the files you just added will be at the top. Select 'em, right-click for Get Info, and add an album name.
 

fusk

New member
Joined
Jul 12, 2008
Messages
5
Points
0
I allready removed the, copy to itunes music folder, and organize.

But doing that on 84gb by hand would take ages, it's over 2500 folders, there must be an easyer solution.
 

S2_Mac

New member
Joined
Oct 24, 2006
Messages
4,878
Points
0
Location
About 3 feet in front of the monitor
OK...I read your first description as meaning all the tracks should have the same album.

I've tested this just a bit -- a hundred folders or so, maybe a thousand tracks, and none of the Album changes "real" (i.e. I just had the script run through the motions, not make any actual changes); seemed to work OK. I also tested with 25 tracks, making real changes; seemed to work OK.

That OK-ness could very well change if you throw 20,000 tracks at the script....so don't do that ;-)

To use, paste all the code into an empty Script Editor window, and save to ~/Library/iTunes/Scripts/<name your script>
If you haven't used iTunes scripts before, you may need to create some of those folders. And if iTunes doesn't show a Scripts menu (an Applescript Script icon) after installing the script, quit iTunes and relaunch.

Follow the instructions -- there is minimal error-handling in the script, and it needs specific conditions to work as expected.

Code:
(*
--
-- Album Name from Folder Name script
-- copyright 2008 by S2_mac;
-- issued under the "MIT License" (http://www.opensource.org/licenses/mit-license.php);
-- if something screws up, don't come looking to the author for redress
--

STEP ZERO: save a copy of the "iTunes Library" file located at ~/Music/iTunes/iTunes Library

= = = = = = = = = = = = = = = = = = = = = = = = = =
1) BEFORE adding files, set iTunes prefs to NOT copy files to iTunes Music & 
	to NOT keep iTunes Music organized

2) COPY only a few folders to their desired destination (including iTunes Music folder);
	 no more than 100 tracks or so

3) ADD those few folders to iTunes

4) SORT the "Music" list by Date Added

5) SELECT the just-added tracks that have no Album tag and choose 
	"New Playlist from Selection" from File menu; name the new playlist

6) DISPLAY the new playlist and sort it by PLAY ORDER (the leftmost column, 
	showing play order numbers); select all tracks

7) RUN the script

8) EXAMINE the results closely...

9) DO IT ALL AGAIN, adding up to a couple thousand tracks
= = = = = = = = = = = = = = = = = = = = = = = = = =

*)

tell application "iTunes"
	set sel to a reference to selection of front browser window
	if contents of sel is {} then
		display dialog "Select some tracks!" buttons {"Cancel"}
	end if
	
	set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
	
	with timeout of 30000 seconds
		repeat with a_track in sel
			tell a_track
				set the_path to location
				if the_path is not missing value then set album to (text item -2 of (the_path as string))
			end tell
		end repeat
	end timeout
	
	set AppleScript's text item delimiters to TID
	display dialog "Done!" buttons {"Thanks"} default button 1 giving up after 5
end tell
 

fusk

New member
Joined
Jul 12, 2008
Messages
5
Points
0
You, are an angel, works like a charm.

thank you very much.
 

MidAtlantian

New member
Joined
Jan 2, 2012
Messages
2
Points
0
To S2_Mac .......This is an old thread, but who knows. You might still be there!

Is there a way to alter your script to deal with deeper file structures? I (usually) have album folders in artists folders. It would not be too difficult to make this uniform: that all albums are within artist folders, but it would be great to be able to carry that information into iTunes too.

Would that next level "up" be "text item-3"?
If I put all the music in a folder called "Enclosing Folder", could I make the insertion of the Artist's name conditional on "-3" NOT being "Enclosing Folder"?

Many thanks,
 

S2_Mac

New member
Joined
Oct 24, 2006
Messages
4,878
Points
0
Location
About 3 feet in front of the monitor
I'm not understanding what you want to do.... Are you looking to fill in missing Artist tags from folder names? If so, the relevant code would look like this:
Code:
with timeout of 30000 seconds
	repeat with a_track in sel
		tell a_track
			set the_path to location
			if the_path is not missing value then
				set third_path_item to (text item -3 of (the_path as string))
				if third_path_item is not "Enclosing Folder" then
					set artist to third_path_item
				end if
			end if
		end tell
	end repeat
end timeout
(Pedantic note concerning your question, If I put all the music in a folder called "Enclosing Folder", could I make the insertion of the Artist's name conditional on "-3" NOT being "Enclosing Folder"?

Yes...but only because you're using a Mac. Since Mac OS keeps track of files by either file path OR file ID, you can move music files around to different folders (on the same disk partition) and iTunes will still know where they are. If this was a Windows scripting situation, your idea wouldn't work -- move a file away from where iTunes expects it to be (i.e. change its filepath), and the file is "missing" to iTunes.)​

If you had something else in mind, post again with specifics of what you want to accomplish.
 

MidAtlantian

New member
Joined
Jan 2, 2012
Messages
2
Points
0
Great Stuff!

Yes, if there is a folder above the album name in my files, it is the artist, at least almost all of the time.

While I did not understand most of your script, I did manage to fake my way though before I got your response by using your original code as a model:

"if (the_path is not missing value) then if (text item -3 of (the_path as string) ≠ "Enclosing Folder") then set artist to (text item -3 of (the_path as string))"

which appears to have worked .... brilliantly, even if your solution is clearly more elegant. I do not know if you have any idea how many hours and hours (and days) of work this saves!

I've never really played with Windows, but I will try to remember that about how it keeps track of file structures. I used to work with mainframe system's code - in assembler - but that is a long way from Mac OR PC architecture.

Where do you find the commands for, say, iTunes - or any other scriptable language - scripts?

Again, Many thanks!
 

S2_Mac

New member
Joined
Oct 24, 2006
Messages
4,878
Points
0
Location
About 3 feet in front of the monitor
Where do you find the commands for, say, iTunes - or any other scriptable language - scripts?

Fisrt, a word about the unique nature of Applescript. Unlike say, Perl, which behaves the same across every platform, Applescript is a less predictable beast. AS is more of a "glue language" than a straight programming language: its benefits lie in helping applications talk to each other (or to the System), and in providing access to an application's underpinnings for the purpose of creating macros (like the script here) or accessing features that the GUI makes less-than-convenient.

Since AS aims mostly at other applications, its syntax can seem to wander. Oh, there's a core syntax and a strict schema for how the language parses, but each application has great freedom in how it employs those foundations. Long story short ("Too late!"), what you learn about Applescript from iTunes will feel very different than what you learn about Applescript from, say, the Mac's "Mail" application, or Quicktime, or TextWrangler, etc.

That said, Applescript is a gas! I regularly use about 14 scripts (complete with Cmd-key combos for easy invocation) that make iTunes much more enjoyable. It's an object-based scripting system, with natural-language tendencies. There's two "best" ways to learn about Applescript-and-iTunes: reading through the info pages at Doug's AppleScripts for iTunes site for some context; and then diving into some scripts (best source: Doug's) to get a feel for it.

(Another thing I like about AS is its ability to run shell scripts; it's often easier for me to run a Perl or bash script through Applescript than through the CLI. You'll find this capability is often used with AS, even in iTunes scripts.)

Reference materiasl include: become familiar with the iTunes "dictionary" of AS terms (i.e. the objects, elements, and properties that iTunes brings to the language) by opening the iTunes Dictionary -- open the Script Editor application on your Mac; choose the "Library" item from the "Window" menu; double-click on the "iTunes" icon to open the iTunes dictionary (feel free to open other dictionaries as well, such as for System Events or Finder). Apple's "official" AS documentation is here; a thorough reference, if a bit pedantic. Questions about 'scripting (hows, whys, whens, etc.) can be fed to MacScripter. Questions about iTunes Applescripts are generally best answered here ;-)
 
Top