Page 1 of 1

regular expression within replace text

PostPosted: Fri Oct 19, 2012 10:32 am
by raghav
hi,
is there a way to put regular expressions in the 'replace text' option?
i want to do something like this : whenever a file is downloaded, remove all numerics/special characters from the filename and create a folder with this new name and move the file to that folder.
like , hazel_001.pdf would result in a folder of name 'hazel' and the pdf will move there. all subsequent pdfs like hazel_002/003 also would move to 'hazel' folder.

i'm a newbie - tried hazel and got the license in the last one hour. so bear with me :)
thanks
Raghav

Re: regular expression within replace text

PostPosted: Fri Oct 19, 2012 12:41 pm
by Mr_Noodle
Search the help for "match patterns". You can basically have Hazel match the first "word" or whatever, assign that to a custom token then use that token in the renaming.

Re: regular expression within replace text

PostPosted: Fri Oct 19, 2012 1:10 pm
by a_freyer
raghav wrote: is there a way to put regular expressions in the 'replace text' option?


No, you can just replace/remove characters one at a time.


raghav wrote:i want to do something like this : whenever a file is downloaded, remove all numerics/special characters from the filename and create a folder with this new name and move the file to that folder.


Here, if your pattern is always (letters)(underscore)(numbers) then follow Mr_Noodle's suggestion. I have typed several tutorials on just this topic.

Report back if the pattern is not predictable at all (i.e. a file might be asdf987asdas8asdf__.pdf). That requires a different solution.

Note: if you rename several similar files Hazel_001 and Hazel_002 to "Hazel" and move them to the "Hazel" directory, then Hazel will by default append a number to the end of each subsequent copy: Hazel, Hazel1, etc.

Re: regular expression within replace text

PostPosted: Fri Oct 19, 2012 11:32 pm
by raghav
ok. looks like i need to try out hazel more to understand how hazel pattern matching works.
but this is essentially what i want to do (taking an example):
1] lets say i download a file of name 09245_organic food-issue0712.pdf
2] i want to search for "organic food" and then if the pattern matches - move the file to "organic food" folder under a deep folder structure like Books/Health/Magazines/Organic Food/
3] apply tags to the new file say like 'health','organic' etc based on the which search pattern matches.

is there an inbuilt way to do this within hazel or do i need write a script?
if there are any must-read posts on this forum, pls point those out.

Re: regular expression within replace text

PostPosted: Sat Oct 20, 2012 1:46 pm
by a_freyer
raghav wrote:ok. looks like i need to try out hazel more to understand how hazel pattern matching works.
but this is essentially what i want to do (taking an example):
1] lets say i download a file of name 09245_organic food-issue0712.pdf


(1) Will your files always appear in this NUMBER_SEARCH TERM-ISSUE####?

raghav wrote:2] i want to search for "organic food" and then if the pattern matches - move the file to "organic food" folder under a deep folder structure like Books/Health/Magazines/Organic Food/


If you answered (1) with YES, then follow the tutorials to extract the SEARCH TERM using custom tokens. Then rename and sort into subfolder SEARCH TERM.

If you answered (1) with NO, then we're going to have to be a bit more clever. Is there any pattern that you can find from your PDFs? Perhaps in the metadata?

raghav wrote:3] apply tags to the new file say like 'health','organic' etc based on the which search pattern matches.


Do you mean spotlight comments? What do you mean by tag?

Re: regular expression within replace text

PostPosted: Sat Oct 20, 2012 2:23 pm
by raghav
answer is No - i dont have a known pattern for filenames since these will be mostly data downloaded from internet.

essentially, i have a list of keywords - each keyword associated with a folder path. whenever i find that the incoming file matches with the keyword i'd let hazel take care of filing it to the right folder.

infact you could consider building this feature into hazel i.e, instead of giving a single pattern, pointing to predefined set of [kind]<-->[keyword/pattern]<->[folderpath] mapping. ofcourse, this might look messy to begin with but i think this will be mighty useful.

right now - i'm cooking up an applescript which calls a perl script to get this job done. since i'm new to applescript, im sticking to perl. any better ideas/suggestions are welcome.

and btw, about applying tags - i'm hoping that i will be able to put tags (meta tags) onto incoming files so that i can easily search in other tools like leap/idocument.
thanks
raghav

Re: regular expression within replace text

PostPosted: Sat Oct 20, 2012 2:27 pm
by a_freyer
You're doing exactly what I'd do. Compare a file name to a known keyword list.

Re: regular expression within replace text

PostPosted: Sat Oct 20, 2012 2:31 pm
by raghav
ok - how do you do that - may be i'm thinking of it in a programmatic way and not really understanding the available options.

my problem boils down to - moving asd12-d_yzPATTERNadd1a.PDF to /longpath/PATTERN. except for PATTERN , everything else is variable.

Re: regular expression within replace text

PostPosted: Sat Oct 20, 2012 5:30 pm
by raghav
i'm quite confused with getting the return value of my perl script into my applescript and back into hazel. i'm new to applescript, so help is much appreciated.

my apple script goes like this:
ctkFolder is the custom token but i never get the right results back into applescript/hazel.

on hazelProcessFile(theFile)
set f to theFile as string -- get the path of the matched file
set f to POSIX path of theFile
set myscriptpath to "/Users/xyz/Documents/hazelrulescripts/ebookrule.pl"
set ctkFolder to "" as string
set filebasename to do shell script "basename " & f
set command to "perl -w " & quoted form of myscriptpath & " " & quoted form of filebasename
set command to command as «class utf8»
set shellresult to do shell script command
return {hazelExportTokens:{ctkFolder:shellresult}}
end hazelProcessFile

and my perl script goes like this

#!/usr/bin/perl

sub main()
{
my $file = $ARGV[0] || "-";
$file = lc($file);

$fol = `grep -o .*$file.* /Users/xyz/Documents/hazelrulescripts/bookrules.txt | cut -d',' -f2 |cut -d':' -f1`;
print "$fol";
#$tag = `grep -o .*$file.* /Users/xyz/Documents/hazelrulescripts/bookrules.txt | cut -d',' -f2 |cut -d':' -f2`;
exit 0;
}

Re: regular expression within replace text

PostPosted: Sat Oct 20, 2012 5:33 pm
by a_freyer
Are you getting script errors? What about hazel errors? What happens when you run this script within the terminal?

Re: regular expression within replace text

PostPosted: Sat Oct 20, 2012 10:38 pm
by raghav
there are no script errors - just that apple script doesnt seem to be getting the return value of the perl script.
perl script looks into a text file which has a map for "keyword,keyword_folder:keyword_tag" & extracts out the keyword_folder and returns it. pretty simple & works fine on the terminal.
however , applescript doesnt seem to be getting the return value of the perl script. this is really my first applescript, so i might be missing something?
this is taking longer than i thought it would :?

hazel log - doesnt show any problems except that it's sorting the file into a wrong subfolder "Downloads//" while i want it to be under "Downloads/Organic/"

2012-10-21 02:45:31.617 hazelworker[14362] Processing folder Downloads
2012-10-21 02:45:33.622 hazelworker[14362] 0712-organic-raw-foods.pdf: Rule New Rule matched.
2012-10-21 02:45:36.713 hazelworker[14362] [File Event] File moved: 0712-organic-raw-foods.pdf moved from folder /Users/xyz/Downloads to folder /Users/xyz/Documents.
2012-10-21 02:45:37.052 hazelworker[14362] [File Event] File moved into subfolder: /Users/xyz/Documents/Downloads/0712-organic-raw-foods.pdf sorted from folder /Users/xyz/Documents to subfolder Downloads//.
2012-10-21 02:45:37.222 hazelworker[14362] Done processing folder Downloads
2012-10-21 02:45:37.457 hazelworker[14365] Processing folder Downloads
2012-10-21 02:45:39.466 hazelworker[14365] Done processing folder Downloads

Re: regular expression within replace text

PostPosted: Sat Oct 20, 2012 11:34 pm
by a_freyer
There script doesn't print $tag. Add that line before exit and it should work.

Re: regular expression within replace text

PostPosted: Sun Oct 21, 2012 12:37 am
by raghav
naah - got that working.
first i need to grep if the filename has any known patterns and then once the pattern is found out, i had to look into the mapping to figure out the folder name and comments for that pattern.
i had missed out putting the first part into apple script - now its working! :D

is there a simpler/dumb way to do this?
next step is to figure out if i can look into the tags of pdf files for organizing them.

thanks for the help in between - appreciate that - that kept me going.