Re: Running scripts from cron AND passing arguments. A solution.
Re: Running scripts from cron AND passing arguments. A solution.
- Subject: Re: Running scripts from cron AND passing arguments. A solution.
- From: "Mark J. Reed" <email@hidden>
- Date: Fri, 16 Sep 2005 18:19:49 -0400
This is an elegant solution. Thank you. The usual solution is more
brute force - cron a shellscript that expands the variables first, so
that the values are just literals by the time osascript sees them,
e.g.
osascript -e "return \"$1\""
>Make sure it's chmoded + x and stick it in your path.
Also has to be +r, but that's probably true by default.
I have some suggestions for improving the shellscript, mostly stylistic:
1. I'd put the filename in a shell parameter so you don't have to type
it multiple times, can change its name if you decide to by changing it
in only one place, and the remove and checks and creation will always
match.
ARGFILE="${thePath}/folder-action-arguments.txt"
> # remove existing arguments file if it exists. The -f just supresses the error if
> # the file does not exist
> rm -f "${thePath}/folder-action-arguments.txt"
> # this tiny delay is necessary because if the file did exist before
> then Finder
> # will not notice the new file arriving as it is replaced too quickly
> otherwise
> sleep 4
2. I'd protect the above with a conditional. No sense sleeping for 4
seconds if the file didn't exist.
if [ -f "$ARGFILE" ]; then
rm "$ARGFILE"
sleep 4
fi
> # this bumps the first argument off the list (defined path)
> shift 1
3. The 1 is the default count for shift, so you don't need to
specify it; not that it hurts, and it is arguably clearer. However,
many shellscripters aren't even aware that it takes an argument count,
and so may be confused.
> # the curly braces are used to supress output until the code block
is complete
> # then everything generated in the block is output to file at the end (after the closing brace)
4. But you don't need any braces in this case, since a loop is
logically one statement. Just put the output redirection on the line
with the "done".
5. You should put quotation marks around the ${i}, because shell
syntax is honored in variable expansions. If i were, for instance,
'>foo', then your 'echo $i' statement would erase the contents of file
'foo;. Whereas the quoted form 'echo "$i" ' will simply display
'>foo' on the screen.
6. While I'm a big fan of using the curly braces in shell parameter
expansions for clarity even when unnecessary,I think they add too much
visual weight when the variable name is just one letter, so would
dispense with them in this case.
Applying my suggestions to your loop, you get this:
for i in "$@"; do
echo "$i"
done >"$ARGFILE"
--
Mark J. Reed <email@hidden>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden