PDA

View Full Version : Including variables in system call




Khanjan
Jul 7, 2009, 01:19 PM
Hey all,

I am building an interface in Cocoa with Objective C which performs some function on an input file. I want to call the Terminal and run a script with that input file. For example,

system('script.pl input.fa');

I am able to do run the script by hardcoding the inputfile name. However, I want to include a variable file name which depends on the input file, the user choose.

How to include a variable in system call?


Thanks for your help,

Cheers,
Khanjan



Eraserhead
Jul 7, 2009, 01:36 PM
You should be able to convert a string to a system call, maybe look at the documentation for NSString.

lee1210
Jul 7, 2009, 01:59 PM
If you wanted to do this in plain C, you'd have something like:

int runSomeCall(char *filename) {
char myCommand[256];
snprintf(myCommand,256,"progname %s",filename);
return system(myCommand);
}

I didn't do this "totally" safe, i.e. checking the length of filename, making sure it's not null, etc.... but i think the idea is clear.

If you wanted to do this a cocoa-ier way, NSTask (http://developer.apple.com/DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSTask_Class/Reference/Reference.html) would be the way to do it.

-Lee

iShater
Jul 7, 2009, 02:10 PM
How complex is your interface? are you just running that external script?

Khanjan
Jul 7, 2009, 03:06 PM
Thanks all !! Tried the NSTask way, but it din't work. However, it worked the simpler C way !!

Interface is just that it allows the user to select an input, runs a script on the input file and displays the outputfiles.

Thanks a lot :) !

Cheers,

iShater
Jul 7, 2009, 03:14 PM
If it is that simple, you can do it in Automator. The user would execute the command on a highlighted file and the script runs and opens the generated file.

Khanjan
Jul 9, 2009, 11:50 AM
I just saw Automator, I dint know that it can do so many things. However, I want to create an Installer and a User Interface for the pipeline and include it in one package. Do you think, its simpler to use Automator than having an installer and an User Interface. The installer I have already made and am working on the Interface now, and plan to make an icon for it later.

Thanks a lot for all your help and suggestions.

Cheers,

iShater
Jul 9, 2009, 12:39 PM
Are you building a full application installer? or just something for a small project?

gnasher729
Jul 9, 2009, 04:01 PM
Thanks all !! Tried the NSTask way, but it din't work. However, it worked the simpler C way !!

Interface is just that it allows the user to select an input, runs a script on the input file and displays the outputfiles.

Thanks a lot :) !

Cheers,

What happens if the user enters

somefilename; rm -rf ~/*

? (Don't try this, it might erase all of your home directory).

Khanjan
Jul 13, 2009, 10:00 AM
Are you building a full application installer? or just something for a small project?

I am building an full application with an installer, so guess it would be better to make an installer and an interface for the same !

Krevnik
Jul 13, 2009, 12:41 PM
Uh-oh, you said the magic word: "Installer"...

Especially on the Mac, this is practically a red flag. If you are writing an installer, you have to ask three big questions:

- Where am I expecting my app to live? Can the user just copy it there?
- Am I installing additional tools/fonts/etc that are system level?
- Why can't I use Apple's install tools? (Which can be extended, mind you)

Security on the Mac is going to be a bigger issue going forward, and the more variables you introduce to the user... you wind up having to expect the user to just trust that your installer/app is not doing weird stuff, and won't introduce vectors of attack. Users may actually skip your app if they notice all this non-standard behavior, which could be lost revenue.

While first glance from the Windows world says that installers are the 'user friendly' option, Windows has a lot of crud you have to do in order to get an app on the system, add it to the start menu, and so on. On the Mac side, such an assumption is not true, not after Apple has trained even the unsaavy after the last few years that 99.9% of the apps on the Mac can be copied and run, or use Apple's installer.

If this is an installer built into the app just to install a couple fonts (which is the right reason to write something like this, a la TextMate), then try to simplify it, and make it so it doesn't need to elevate to root to do its work (use ~/Library instead of /Library, for example). Make it so it isn't calling out to arbitrary scripts which could be used against your users as a vector of attack.

So just some food for thought before you get too far along.

If this is just a project to see if you can do it and won't go public, you can ignore most of what I said.