Help with Aliases, Lists, and a Droplet

Hi! I’ve been learning Applescript for a few days now, and I decided to make myself a program to test out my new set of skills. I had two goals for the program. First, I’m rather anal about tagging my music correctly. I like to have the year of the album placed at the beginning of the album ID3 tag slot so that they arrange themselves chronologically both in iTunes and on my iPod. I wanted to create a droplet which would take the year of the album and place it at the front of the album tag. In this, I have been successful. However, it is my second goal which alludes me.

Because a lot of the music I listen to is downloaded, the folders often come with .m3u playlists for that album. This annoys me, because when I drag the folder into iTunes, all of the files go into the library twice (once for the actual file, and once more because of the .m3u). My second goal was to be able to drag folders of music onto the droplet, while having it discriminate against those .m3u files. Something about the nature of droplets, lists, and aliases is preventing me from doing this, however. I’ve tried many different ways to get this to work, but nothing seems correct. What I have now seems to be a little primitive, but I do not understand why it still wouldn’t work. Any help that you guys can offer would be extremely appreciated.

Here is the part of the code that deals with the .m3u files:

on open Album_Fixer
	tell application "Finder"
		set SongList to {}
		set adrop to {}
		set bdrop to {}
		set cdrop to {}
		set ddrop to {}
		repeat with a in Album_Fixer
			if folder of (info for Album_Fixer) then
				set adrop to item a of Album_Fixer
				repeat with b in adrop
					if list then
						set bdrop to item b of adrop
						repeat with c in bdrop
							if list then
								set cdrop to item c of bdrop
								repeat with d in cdrop
									if list then
										set ddrop to item d of cdrop
										repeat with e from 1 to (count of ddrop)
											if name of ddrop's item e does not end with ".m3u" then
												set end of SongList to ddrop's item e
											end if
										end repeat
									else
										if name of cdrop's item d does not end with ".m3u" then
											set end of SongList to cdrop's item d
										end if
									end if
								end repeat
							else
								if name of bdrop's item c does not end with ".m3u" then
									set end of SongList to bdrop's item c
								end if
							end if
						end repeat
					else
						if name of adrop's item b does not end with ".m3u" then
							set end of SongList to adrop's item b
						end if
					end if
				end repeat
			else
				if name of Album_Fixer's item a does not end with ".m3u" then
					set end of SongList to Album_Fixer's item a
				end if
			end if
		end repeat
	end tell
-- More code here
end open

By the way, the reason for all the repeat loops is so that I could deal with, in theory, files that were as many as five folders down.

Again, thank you for any help you can offer.

Welcome. :slight_smile:

In that type of loop, a already has (a reference to) the appropriate value:

set theseItems to {"abc", "def"}

repeat with a in theseItems
	return a
	--> item 1 of {"abc", "def"}
end repeat

In this type of loop, a would be a number:

set theseItems to {"abc", "def"}

repeat with a from 1 to (count theseItems)
	-- set thisItem to item a of theseItems

	return a
	--> 1
end repeat

See also: Repeat Statements

I think that this was something that I had already tried, but thanks for the input. I’ll give it a shot a see if anything comes of it.

No, that didn’t do it, but thanks anyway. The program is currently changed to meet your advice.

Another thing I think I should mention is that the program works fine when I grab a bunch of files and drop them into it; it’s only when folders are involved that it messes up. Currently, it says something about not being able to get the <> of the list or alias, but I’m afraid that I don’t know what that means. I also can’t see the end of the error message, because it goes off of the tiny error window.

Hi, Tomberculosis.

Besides Bruce’s comment, ‘if list’ doesn’t mean anything in either Finder scripting or in vanilla AppleScript.

To burrow down into folders and subfolders, a recursive handler (one that calls itself) is needed here. When it hits a folder, it calls another iteration of itself to deal with the items in the folder. That iteration calls other iterations if required.

I’ve changed some of your variable names below to try and make them more descriptive:

on open dropped_items
	set song_list to get_song_list(dropped_items)
	
	-- More code here.
end open

-- Recursive handler.
-- In: a list of aliases to files and/or to folders containing files.
-- Out: a list of aliases to the files whose name extensions aren't "m3u".
on get_song_list(these_aliases)
	set song_list to {}
	repeat with i from 1 to (count these_aliases)
		set this_alias to item i of these_aliases
		set alias_info to (info for this_alias)
		if (folder of alias_info) and not (package folder of alias_info) then
			-- If this alias refers to a folder, get a list of aliases to the items in the folder.
			tell application "Finder"
				try
					set folder_items to (items of folder this_alias) as alias list
				on error
					set folder_items to {(item 1 of folder this_alias) as alias}
				end try
			end tell
			-- Call this handler again to deal with those aliases
			-- and concatenate the returned list to the current 'song_list'.
			set song_list to song_list & get_song_list(folder_items)
		else if (name extension of alias_info is not "m3u") then
			-- If this alias refers to a file whose name extension is not "m3u" then append it to 'song_list'.
			set end of song_list to this_alias
		end if
	end repeat
	
	return song_list
end get_song_list

Wow, I’m glad I asked for help. I would never have thought of anything even close to that. It works wonderfully, thank you.