Move into subfolders based on first word?

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

Moderator: Mr_Noodle

Move into subfolders based on first word? Tue Dec 03, 2013 3:51 pm • by dzg
Let's say I have a folder with these files...

    Code: Select all
    AAA foo1 30.mov
    AAA bar 15.mov
    AAA lorem.mov
    BBBB foofoo2.mov
    BBBB barbar 50.mov
    CCCCC ipsum 30.mov
    CCCCC gamma 10.mov


I want Hazel to look at files that were added before today, and move them into a subfolder based on the first word of the filename.

So all the files beginning with AAA would go into an AAA subfolder, the BBBB's into the BBBB subfolder, etc.

Is this possible?
dzg
 
Posts: 25
Joined: Tue Jun 26, 2012 10:19 pm

Yes. Search the help for "match patterns" as that contains an article which explains how to do it.
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Move into subfolders based on first word? Wed Dec 04, 2013 3:54 pm • by dzg
Mr_Noodle wrote:Yes. Search the help for "match patterns" as that contains an article which explains how to do it.


Sorry, I don't see how that applies.

I want to extract the first word, and then move to (or create if it doesn't already exist) a folder named with that word.

I tried this embedded AppleScript to extract the first word, with a token "x":

Code: Select all
tell application "Finder" to set f to (name of theFile)
set hazelExportTokens to {x:word 1 of f}


and then "sort into subfolder with pattern •x" ... but that's not working.

I don't think the AppleScript values are getting passed

The log shows:

Code: Select all
... hazelworker[41619] [Error] Sorting into subfolder failed: Could not move /Volumes/..... Source and destination are the same.
dzg
 
Posts: 25
Joined: Tue Jun 26, 2012 10:19 pm

Re: Move into subfolders based on first word? Wed Dec 04, 2013 4:11 pm • by dzg
Ok, I finally found a website that explained I should use

Code: Select all
return {hazelExportTokens:{x:word 1 of f}}


It would have saved me a couple of hours if that were in the manual!
:idea:
dzg
 
Posts: 25
Joined: Tue Jun 26, 2012 10:19 pm

You don't need a script for this. If you use match patterns, just create a pattern like:

(•first word) (anything)

where (•first word) is a custom token. You can use (anything) as its pattern to specify any combination of characters. The important part is that you include a space between the custom token and the (anything token) as that will make the custom token only match up to that first space.

Then in your Sort action, just use (•first word).

Again, consult the help article as it has images there which should help.
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Move into subfolders based on first word? Wed Dec 04, 2013 4:24 pm • by dzg
And this helps too:
Code: Select all
tell application "Finder" to set f to (name of theFile)
set AppleScript's text item delimiters to {" ", "_", ".", "-"}
return {hazelExportTokens:{x:text item 1 of f}}
dzg
 
Posts: 25
Joined: Tue Jun 26, 2012 10:19 pm

Re: Move into subfolders based on first word? Wed Dec 04, 2013 4:30 pm • by dzg
Mr_Noodle wrote:You don't need a script for this. If you use match patterns, just create a pattern like:

(•first word) (anything)

...


Ah, now I see.
dzg
 
Posts: 25
Joined: Tue Jun 26, 2012 10:19 pm


Return to Support