Sort into Subfolder with duplicates?

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Sort into Subfolder with duplicates? Wed Mar 10, 2010 1:08 am • by nixta
Hi there.

Loving Hazel - it keeps amazing me with what it can do. But how would I do this?

I want a drop folder for new MP3s that I've bought from Amazon/Rhapsody/Lala etc. that will rename them into my folder structure on the same drive. I have all my files on a drive named iTunesMaster stored like this

/My Music/<Artist>/<Album>/<Album> [<tracknumber>] - <title>.mp3

I've created a drop folder named

/New Music for iTunes

and would like them moved to the right location then added to iTunes (unless the files are already there). Currently I trigger if the file was added more than a minute ago.

(I can't work out how to attach a file so here's my attempt at showing the rule):

1. {Rename file} with pattern {album [track number(00)] - title extension}
2. {Move File} to folder {/My Music}
3. {Sort file into subfolder} with pattern {>authors(First Item)>album}
4. {Import into iTunes} to playlist {Library}

Now, this works great except that if the file already exists in the right place, then a new one with "-1" appended on the end (before the extension - e.g. "An Album [01] The Song-1.mp3") is created and that is added to iTunes, of course as a duplicate.

Would love to be able to work around this. I.e. if a file already exists, be able to use the same options as the Move File action, and if the file was discarded then not add it to iTunes. I suspect it's easy but I'm being a n00b.

Can anyone suggest a way to do this, ideally without using "/My Music" as a temporary holding area before sorting to a subfolder?

Many many thanks!

Nick.
nixta
 
Posts: 9
Joined: Tue Mar 09, 2010 9:11 pm

Re: Sort into Subfolder with duplicates? Wed Mar 10, 2010 11:50 pm • by Mr_Noodle
Unfortunately, the sort into subfolder action doesn't have the same options that move/copy does. It's on the feature list but I don't know if it will make the next maintenance release or get bumped to 3.0 which could be a ways off. A lot of the issue now is where to put the UI since the subfolder pattern UI already takes up a bit of space. So, it'll happen at some point but whether it happens sooner rather than later depends on how comfortable I am with any UI decisions.
Mr_Noodle
Site Admin
 
Posts: 11273
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Sort into Subfolder with duplicates? Thu Mar 11, 2010 3:53 pm • by nixta
Mr_Noodle wrote:Unfortunately, the sort into subfolder action doesn't have the same options that move/copy does.

Thanks for the update. Can you think of a workaround that could achieve what I'm after? Perhaps build the folder structure in a temporary location then use Move to move it over? Or would that overwrite rather than merge?

Either way, thanks for a great app. You've changed the way I work for the better.
nixta
 
Posts: 9
Joined: Tue Mar 09, 2010 9:11 pm

Re: Sort into Subfolder with duplicates? Fri Mar 12, 2010 1:54 pm • by Mr_Noodle
I can't think of a good workaround that isn't totally tortured (possibly involving a copy of all the files or aliases). I'm leaning towards wedging in the rename/replace options into the rename and sort actions in the next maintenance release. It may not be pretty but it will be a stopgap until I can rework things in 3.0. I suggest turning on auto-updating so that you can get a notification when the new version is out.
Mr_Noodle
Site Admin
 
Posts: 11273
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Sort into Subfolder with duplicates? Wed Mar 17, 2010 1:26 am • by holigen
You might want to try using two rules -- the first to see if the file exists in your folder structure, and if so, then to delete (or move to the Desktop) the file and the second to move any files that survived the first rule into your folder structure and import them into iTunes.

You could probably get this into one rule using a bit more code, but it would definitely be easier to keep as little custom code in Hazel as possible.

The first rule for the new music folder could be called something like "purge duplicate music" and would match any music files that are in the folder and who have their date last matched set as blank (that haven't been seen by Hazel yet). The corresponding action could be written in Python (or Ruby, or shell scripting, or even AppleScript, probably), with the shell set to "/usr/bin/python" and the following as the embedded script:

Code: Select all
import os;
import subprocess;
import sys;
import shutil;

# this part is nasty, but it works
artist = subprocess.Popen(["mdls", "-name", "kMDItemAuthors", sys.argv[1]], stdout=subprocess.PIPE).communicate()[0].split("\"")[1];
album = subprocess.Popen(["mdls", "-name", "kMDItemAlbum", sys.argv[1]], stdout=subprocess.PIPE).communicate()[0].split("\"")[1];
title = subprocess.Popen(["mdls", "-name", "kMDItemTitle", sys.argv[1]], stdout=subprocess.PIPE).communicate()[0].split("\"")[1];
trackNumber = subprocess.Popen(["mdls", "-name", "kMDItemAudioTrackNumber", sys.argv[1]], stdout=subprocess.PIPE).communicate()[0].split(" ")[2].rstrip("\n").zfill(2);

# this part is a bit cleaner
extension = os.path.splitext(sys.argv[1])[1];

name = album + " [" + trackNumber + "] - " + title + extension;
pathToFile = os.path.join("/Volumes/iTunesMaster/MyMusic/", artist, album, name);

if os.path.exists(pathToFile):
    shutil.move(sys.argv[1], os.path.expanduser("~/Desktop/"));
    # the following line can be used in lieu of the previous in order to completely delete the matching file if it already exists
    #os.remove(sys.argv[1]);


The next rule would do the work of actually moving the file and importing it into iTunes. You can probably figure out how to do that one on your own, since it seems like you've got that part of the rule working already. Make sure to not match any files that haven't been seen by Hazel yet, just in case a file gets by the first rule.
holigen
 
Posts: 18
Joined: Tue Mar 16, 2010 2:43 am

Re: Sort into Subfolder with duplicates? Wed Mar 17, 2010 1:55 pm • by Mr_Noodle
Actually, you could wedge an AppleScript between steps 3 & 4. Being a bit too busy to bang out a script, here's a general rundown:

- Check if the file has a pattern of a dash then some number at the end.
- See if the directory has a file with the name minus the tacked on number.
- If it does, then throw the current file away and return a record with the "hazelStop" key set to true.

The last step is a feature where an AppleScript can signal back to Hazel to stop processing additional actions. Search the in-app help for AppleScript for more details.

And thanks again, holigen, for your useful script-fu.
Mr_Noodle
Site Admin
 
Posts: 11273
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Sort into Subfolder with duplicates? Wed Mar 17, 2010 2:21 pm • by nixta
Mr_Noodle wrote:Actually, you could wedge an AppleScript between steps 3 & 4. Being a bit too busy to bang out a script, here's a general rundown:

- Check if the file has a pattern of a dash then some number at the end.
- See if the directory has a file with the name minus the tacked on number.
- If it does, then throw the current file away and return a record with the "hazelStop" key set to true.

The last step is a feature where an AppleScript can signal back to Hazel to stop processing additional actions. Search the in-app help for AppleScript for more details.

And thanks again, holigen, for your useful script-fu.

Yes, thank you holigen! And thank you Mr. Noodle.

Both these solutions make sense to me. I'm going to use this as an excuse to learn some damned AppleScript already (though who knows when I'll get around to it!) and when I do I'll post it here for you both to laugh at.
nixta
 
Posts: 9
Joined: Tue Mar 09, 2010 9:11 pm


Return to Support