Re: Integrating Unix scripting in Cocoa app
Re: Integrating Unix scripting in Cocoa app
- Subject: Re: Integrating Unix scripting in Cocoa app
- From: Ron Fleckner <email@hidden>
- Date: Wed, 8 Aug 2007 22:06:08 +1000
On 08/08/2007, at 9:08 PM, Andrew Farmer wrote:
On 08 Aug 07, at 02:58, Ron Fleckner wrote:
On 08/08/2007, at 6:28 AM, Craig Hunter wrote:
Take a look at the shell's "source" command, which will load and
execute
commands from a text file with no other requirements:
source file.txt
This might give you an additional option. Perhaps you can check
to see if
the file is executable and/or if it includes the shebang. If
not, then
conclude it's merely a text file containing shell commands and
pass it along
to "source" to run.
But here's something interesting: according to a simple "hello
world" script
I just tested, source will even handle text files that have execute
permission set and contain a shebang (ie, real scripts). If that
turns out
to be true in general, then you can let the source command
execute anything
designated as a script by the user. I'd recommend testing it out.
Well that sounds pretty interesting. I don't have the energy to
investigate it right now (too much to do!), but I certainly will
look at "source" as a generic script runner. When you say
"shell", which one do you mean?
Don't bother. If you can determine that a file lacking a shebang is
indeed a shell script* you can just pass it as an argument to /bin/
bash (or whatever shell you deem appropriate). Methods such as
passing the shell a wrapper script which invokes 'source' or
sending it a 'source' command through a pipe are unnecessarily
circuitous.
And, as I noted in an earlier email, it's entirely useless (and
possibly harmful) if you're dealing with anything other than shell
scripts.
A generalized approach to running scripts should look like the
following:
1. Is it executable? If so, run it directly.
2. Does it have a shebang? If so, either set its executable bit
and run it. (You can also try parsing the first line yourself if
you don't want to modify permissions, but this is easy to get
wrong, particularly when multiple arguments are involved.)
3. Is the extension known? If so, use an appropriate interpreter.
4. Complain. Any guess you make at this point is likely to be
wrong, so it's best to just ask the user to give you a hand.
*: I'm not sure how you'd do this, besides sniffing the extension
or using some really nasty heuristics.
As a workable approach, I've settled (I think) on #3 above. That is,
keep a list of allowable extensions and run with the appropriate
interpreter. As for the other approaches, they're simply too complex
and beyond my current skills to implement effectively. This whole
thing started out as a 'black box' class which creates and manages an
AppleScript Scripts menu for a Cocoa app. That bit was easy. Then
it was suggested to me that the class would be more generally useful
if it could also run other script types including Python, Perl, Ruby
and shell scripts. The first three were also easy: just invoke the
appropriate interpreter as the first arg to [NSTask
launchedTaskWithLaunchPath:arguments:] and the path to the
'script' (executable or not) as the second arg. So, for the moment
at least, I'll leave it right there.
Thanks for your input.
Ron
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden