Regular Expressions

Talk, speculate, discuss, pontificate. As long as it pertains to Hazel.

Moderators: Mr_Noodle, Moderators

Regular Expressions Fri Feb 02, 2007 6:55 pm • by protoplasm
I'd love regular expression support.

From the developer's point of view, I can appreciate it's not a friendly or easy to understand syntax. But I'd still love it. :-) It'd be great for more accurately matching filenames to move to folders etc., but even better if it was supported in renaming/moving.

For the matching of conditions I would imagine it could fit in more easily, perhaps in the drop down "starts with" there could be a "matches expression" or something... I dunno.

Just throwing the idea out there as I didn't see it anywhere else... I guess in the meantime I can look at writing a shell script for the renaming side. I've not started to play with that yet, though.
protoplasm
 
Posts: 17
Joined: Fri Feb 02, 2007 5:30 pm

Fri Feb 02, 2007 7:38 pm • by IDontDoWindows
If you're willing to use AppleScript, there's TextCommands, which lets you use RegEx in scripts.
IDontDoWindows
 
Posts: 57
Joined: Sun Jan 07, 2007 5:43 am

Fri Feb 02, 2007 9:36 pm • by protoplasm
IDontDoWindows wrote:If you're willing to use AppleScript, there's TextCommands, which lets you use RegEx in scripts.


Hey, and thanks. I'm not overly familiar with AppleScript, but I'm fairly comfortably using regular expressions in Ruby et al.

The main reason for my request came because I felt it'd be nicer if Hazel supported them directly, rather than farming off everything to scripts, particularly in the case of matching where the checks for whether conditions are met do not support return values from scripts.
protoplasm
 
Posts: 17
Joined: Fri Feb 02, 2007 5:30 pm

Wed Feb 07, 2007 4:55 pm • by Mr_Noodle
I've thought about regular expressions but they are pretty complex for non-programmers to deal with. I am working on a pattern editor for renaming and for creating subfolders but they have fixed tokens you can drop in (no regex backreferences).

Could you post specific examples of how you'd like to use this? It helps for me to have specifics so that I can ascertain the best way to address it.
Mr_Noodle
Site Admin
 
Posts: 11251
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Thu Feb 08, 2007 4:13 pm • by protoplasm
Okay, here's an example that's pretty close to my real world scenario. I have the following files:

brian.602.txt
brian.7.02.txt
updated.brian.802.txt
steve.602.txt
steve.7.02.txt
updated.steve.802.txt
ian.602.txt
ian.7.02.txt
new.ian.802.txt

[in my real case I have about 9 different names, not just the 3]

I want to achieve the following: 1) move the "brian" files to a folder called "Brian Brown"; 2) change the label colour of the files; 3) rename the files to be more consistent, e.g. all "Brian Brown - 06/02/2007.txt". For the "steve"/"ian" files I want to do the same thing, but put them under the "Steve Smith"/"Ian Inglis" folder.

Currently I have: a "Brian" rule, a "Steve" rule and an "Ian" rule. The "Brian" rule moves files containing "Brian" to a folder called "Brian Ball", sets the label to yellow and then runs my Ruby script to rename them according to my preference (getting the name from the current working directory etc).

My script is called last because it renames the files. Hazel doesn't seem able to further manipulate the file after the rename had taken place. In an ideal world I'd like to be able to pass back the new name to Hazel but that isn't really needed in this particular case.

In an idea world, I'd have one rule to match the files, using a regular expression with back references to specify which folder they should be moved into, and then the label could be coloured and it could run my script for the final renaming if a regular expression rename in the Hazel interface wouldn't suffice.

Apologies if this is all not very clear! I have other cases where I'd like this sort of thing, too, for moving pictures and renaming them in a simpler manner than above.
protoplasm
 
Posts: 17
Joined: Fri Feb 02, 2007 5:30 pm

Fri Feb 09, 2007 1:32 pm • by Mr_Noodle
As for Hazel not being able to manipulate the file after a rename, it's because Hazel doesn't know about the rename and loses track of it. I am looking to add a rename action in the next version but it won't be as flexible as using a regex but you would be able to have substitutions for file attributes like the name, the various dates etc. In addition, there's the ability to sort into subfolders based on a pattern so folder structures can be dynamically created based on attributes of the file.

Exposing a full regex interface will require a bit of thought. Even if I add a "matches pattern" operator, it's unclear how you would tie that into an action to do substitutions. For instance, if you have two regex conditions that are "or"-ed, in the action phase you won't know which of them matched and therefore your capture group references may be off. Basically, there are cases of ambiguity and fragility in tying the match and replace patterns together that I would need to address somehow in the interface. Hopefully that makes some sense.
Mr_Noodle
Site Admin
 
Posts: 11251
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Fri Feb 09, 2007 8:47 pm • by IDontDoWindows
My script is called last because it renames the files. Hazel doesn't seem able to further manipulate the file after the rename had taken place. In an ideal world I'd like to be able to pass back the new name to Hazel but that isn't really needed in this particular case.


If Hazel could further manipulate the file, what would you have her do?
IDontDoWindows
 
Posts: 57
Joined: Sun Jan 07, 2007 5:43 am

Fri Feb 09, 2007 8:52 pm • by protoplasm
The most simple, useful, example I can give would be with regards to music files.

A script could rename files, replacing underscores with spaces, potentially creating ID3V2 tags from filename, or vice versa, then trigger the import into iTunes action from Hazel.
protoplasm
 
Posts: 17
Joined: Fri Feb 02, 2007 5:30 pm

Sat Feb 10, 2007 6:32 pm • by IDontDoWindows
Can't you import into iTunes via the script?
IDontDoWindows
 
Posts: 57
Joined: Sun Jan 07, 2007 5:43 am

Sat Feb 10, 2007 8:01 pm • by protoplasm
I could do absolutely everything via a script and bypass Hazel completely. But that would kinda ruin the point of it all, wouldn't it?
protoplasm
 
Posts: 17
Joined: Fri Feb 02, 2007 5:30 pm

Mon Feb 12, 2007 12:34 pm • by Mr_Noodle
Actually, thinking about it some, Hazel should be able to track the file as long as the script only does a simple rename or a move within the same filesystem. I'll work on it for the next version.

Of course, that doesn't address the original issue concerning regex but at least scripts can be more useful.
Mr_Noodle
Site Admin
 
Posts: 11251
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Mon Feb 12, 2007 1:22 pm • by protoplasm
If it's doable, that would be great! Thank you.
protoplasm
 
Posts: 17
Joined: Fri Feb 02, 2007 5:30 pm

Re: Regular Expressions Wed Apr 23, 2008 4:39 pm • by Quine
I'd like to check in again (figured I might as well post in this topic instead of making a new one) and see what's up with regex support. I think it'd be really nice if it could be built in, even if its kept totally cryptic to regular users for now, because it'd be so useful for power users (and honestly, are regex that hard to learn? They don't seem any tougher than applescript, and theres way more help online for them than script)

I was making rules today to automove all my class docs into appropriate folders, and noticed that every quarter i have to make 4 new rules (plus extracurrics which is another 4-6). I always put the class name near the number in my filenames, so I figured why not just make a regex that'd fine the three letter classname + number combo and would use those two tokens to put it in the appropriate directory in my School dir, then it'd just take one rule for the next 3 years.

Anyways, there's another real world example. It'd also be nice for renaming files (then i could ditch A Better Finder Rename) of course.
Quine
 
Posts: 15
Joined: Mon Mar 19, 2007 6:26 pm

Re: Regular Expressions Thu Apr 24, 2008 8:29 pm • by Mr_Noodle
I'm actually working on this feature now. That said, it won't be regexes, at least in the sense that you know them. Exposing them to users (even power users. Remember that a lot of power users don't program) would be confusing and quite error prone.

The feature will have pattern matching with subsequent substitution in the rename and sort actions. Based on the patterns I've seen, this should be able to handle almost all the cases people want to use it for. Basically, you should be able to take a name apart and then plug the parts back in as you see fit. I'm hoping to have a beta available soon.

One thing I noticed looking through this thread again though is that protoplasm's example is not doable. In that case, the last name somehow magically appears out of nowhere so I don't know how that would work. In that case, separate rules would be needed.
Mr_Noodle
Site Admin
 
Posts: 11251
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Regular Expressions Sat Jun 11, 2016 10:50 am • by YannB
We really can't have Regex? Pretty please?

I used to send the files to Name Mangler for that, but I just updated to Hazel 4, and it doesn't run the droplet anymore… Just opens it for me to stare at it :'(

I can't imagine how it could be confusing and hinder a user in any way if you just added a "regex" option inside your rename "with pattern" thinggy.

Please reconsider! Thanks… :)
YannB
 
Posts: 6
Joined: Fri Jun 07, 2013 11:21 pm

Next

Return to Open Discussion