Re: Integrating Unix scripting in Cocoa app
Re: Integrating Unix scripting in Cocoa app
- Subject: Re: Integrating Unix scripting in Cocoa app
- From: John Labovitz <email@hidden>
- Date: Tue, 7 Aug 2007 08:25:31 -0700
On Aug 7, 2007, at 7:02 AM, Ron Fleckner wrote:
The context is a Scripts menu in an application. If the user
wishes to add a script to that menu by adding the script to a
folder in the app's Application Support folder, then I think there
is no subversion going on. As a bonus, the user can add a plain
text file without the need to make it an executable and the app
will run the script.
I think there's some confusion here about what a "script" is,
especially one that is in a plain text file. You'll end up with the
following possibilities. These are ordered by certainty -- the first
ones are the ones that are easiest to implement and most reliable to
make work.
- A file with the executable bit on. These, as several folks have
pointed out, will be executed by the kernel, and you don't need to
know anything else about them. Just run them directly via NSTask,
specifying the file's path for NSTask's executable path.
- A file with "#!" as the first two characters. You can do exactly
as the system does and read the rest of this line as the
"interpreter" and its arguments. You'll want to split this line by
spaces, then use the first element (the interpreter path) as NSTask's
executable path, the remainder as the first elements in the task's
arguments, and the final argument being the path to the script file.
(This is because a script author can specify not only the
interpreter, but arguments the interpreter should use to run that
script.)
- A file with a known extension -- .sh, .pl, .rb, etc. You could
have a small dictionary that mapped these extensions to reasonable
interpreters -- /usr/bin/{ruby,perl} or /bin/sh. As above, use the
interpreter's path as NSTask's executable path, and the script path
as a one-element array for NSTask's arguments.
- A "plain text" file, with no executable bit or extension. This is
where it gets difficult. Which shell should interpret this file?
bash? csh? ksh? All those have different syntaxes, so there's no
right answer.
If I were you, I'd require the user to either set the executable bit,
include the shebang (#!) line, or use a known extension. It's going
to get too complicated to try to guess at the format beyond that.
Hope this helps.
--John
_______________________________________________
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