• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Integrating Unix scripting in Cocoa app
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Integrating Unix scripting in Cocoa app


  • Subject: Re: Integrating Unix scripting in Cocoa app
  • From: Ron Fleckner <email@hidden>
  • Date: Thu, 9 Aug 2007 19:06:18 +1000

Message: 14
Date: Wed, 8 Aug 2007 21:29:20 -0700
From: Marcel Weiher <email@hidden>
Subject: Re: Integrating Unix scripting in Cocoa app
To: Apple cocoa-dev List Cocoa <email@hidden>
Message-ID: <email@hidden>
Content-Type: text/plain;	charset=US-ASCII;	format=flowed;	delsp=yes


On Aug 8, 2007, at 5:06 AM, Ron Fleckner wrote:


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.


Hmm...I had interpreted that as a series of steps, not as alternatives.



 That is, keep a list of allowable extensions and run with the
appropriate interpreter.


As others have pointed out, that is OK as a fallback, but probably not generally.


 As for the other approaches, they're simply too complex and beyond
my current skills to implement effectively.


1. Check wether file is executable:

[[NSFileManager defaultManager] isExecutableFileAtPath:path];

2a. Check wether file starts with shebang

[[NSString stringWithContentsOfFile:path] hasPrefix:@"#!"];

2b. Make file executable

(This actually seems a bit cumbersome with NSFileManager APIs...so
switching to libc.  Hmm...of course, this now also has code that can
be used to check if the file is executable.)

	#include <sys/stat.h>
	#include <sys/types.h>

	{
		struct stat statbuf;
		stat( [path fileSystemRepresentation]  , &statbuf );
		chmod( [path fileSystenRepresentation] ,statbuf.st_mode |
S_IXUSR  );			// make executable for owner only
	}

Add error checking to taste.	

Marcel

Marcel, thanks very much for the code snippets. Finding out if the file is executable was a stumbling block for me. I don't know why I didn't see that NSFileManager method. I haven't had any time for coding over the last few days so all the replies my original question got are waiting for me to mull over them and then decide on a path forwards.


Thanks again,

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


  • Follow-Ups:
    • Re: Integrating Unix scripting in Cocoa app
      • From: Nir Soffer <email@hidden>
  • Prev by Date: Re: Integrating Unix scripting in Cocoa app
  • Next by Date: Re: Loading data in a background thread and displaying it with Bindings
  • Previous by thread: Re: Integrating Unix scripting in Cocoa app
  • Next by thread: Re: Integrating Unix scripting in Cocoa app
  • Index(es):
    • Date
    • Thread