Script running in parent folder

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

Moderator: Mr_Noodle

Script running in parent folder Mon Sep 03, 2012 5:32 pm • by Tam-Lin
I'm trying to get a rule running to regenerate a ctags file every time something in my ~/src folder changes. (Incidentally, you might want to change the condition that matches extensions to ignore a beginning period; it took me a while to figure out why I could touch .py files without the rule executing.)

I'm watching the ~/src folder, with a rule set to trigger and process subfolders if there's a file in them where the last matched date is before the last modified date, or if the last matched date is blank. (Another thing you might consider is having a blank last matched date always be considered prior to any other date.) That rule works.

I then have another rule that runs the following script if it finds a file with a source code extension that's changed since last being matched, or if the last match date is blank. This is the script:

Code: Select all
/Applications/BBEdit.app/Contents/Helpers/ctags --excmd=number -f tags --tag-relative=no --fields=+a+m+n+S -R $1


The problem I'm having is that the tags file isn't being generated in the code subdirectory as I want it to, but in the parent directory. Which is to say the script is creating the file ~/src/tags, not ~/src/<source directory>/tags. So I'm assuming that the script is running in the parent directory, not the child directory where the changed file actually is. Is this the expected behavior?
Tam-Lin
 
Posts: 13
Joined: Mon May 09, 2011 10:18 pm

Re: Script running in parent folder Mon Sep 03, 2012 9:11 pm • by Tam-Lin
Poking around, I was able to get the results I wanted with the following script:

Code: Select all
cd $(dirname $1)
/Applications/BBEdit.app/Contents/Helpers/ctags --excmd=number -f tags --tag-relative=no --fields=+a+m+n+S -R $1


But, it would be nice if it was documented somewhere where the script you're invoking is being run from.
Tam-Lin
 
Posts: 13
Joined: Mon May 09, 2011 10:18 pm

Re: Script running in parent folder Tue Sep 04, 2012 5:02 pm • by Mr_Noodle
Actually, scripts should not assume anything regarding paths. You should be specifying full paths whenever possible. Also, I'd recommend putting double quotes (") around $1 to guard against paths with spaces in it.

Also, yes, I will be fixing the behavior of matching against extension to allow the period. It's is surprisingly more work than you would think because of some things tied into the rule format but hopefully it will be addressed in 3.1.
Mr_Noodle
Site Admin
 
Posts: 11551
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Script running in parent folder Thu Sep 06, 2012 11:51 pm • by Tam-Lin
Then, if I may make a suggestion, I'd document in the help entry on scripting that the scripter shouldn't assume any particular location for the script to be running in, nor any particular set of environmental variables, and give the dirname and basename commands so that the scripter can find them easily. I've seen references to this in the forum, but nothing in the help, unless I'm missing something.
Tam-Lin
 
Posts: 13
Joined: Mon May 09, 2011 10:18 pm

Re: Script running in parent folder Fri Sep 07, 2012 4:39 pm • by Mr_Noodle
I'll consider it but (a) keep in mind that scripts can use an interpreter and the rules may be different for each one and (b) for shell scripts, it's a practice you should be adopting in general, not just in Hazel. If anything it can be a bad security risk or you could end up modifying files you didn't intend to.
Mr_Noodle
Site Admin
 
Posts: 11551
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City


Return to Support