Jump to content

Shell command from XCode interface


14 posts in this topic

Recommended Posts

Hi, I am very new to XCode and I try to write a small tool for an entry of parameters for a shell command.

The Purpose is to make an QT preferences changing easier, here in case of entering MediaKeys.

Because for those who have to use Quick Time X it is neccessary to use the Terminal to enter MediaKeys. The Shell command for that is: qtdefaults write MediaKeys "Category" "MediaKey"

I want to create an Interface, where it is possible to enter Category and Key and press a button and the command will be executed with the special parameters.

i have read some Tutorials and got now used to the dev Tools and to the handling of the Interace Builder.

 

 

I have a Class AppDelegate.m

@implementation AppDelegate

-(void)clickButton: (id)sender;

{

NSString *text = [textField stringvalue];

And a class AppDelegate.h

@interface AppleDelegate : NSObject {

IBOutlet NSTextField *textField;

IBOutlet NSTextField *textField;

}

- (IBAction)clickButton: (id)sender;

@end

 

Could anyone help me, to finish this...would be great and I could learn something. For me it looks like only a few lines could complete this job. Or the most Important thing is the line how to send the command to the terminal... Ok THX for answers I hope it was understandable, i must realize my english got bad... :thumbsup_anim:

Link to comment
Share on other sites

AppDelegate.h

@interface AppleDelegate : NSObject {
	IBOutlet NSTextField *categoryTextField;
	IBOutlet NSTextField *mediaKeyTextField;
}
- (IBAction)clickAddButton:(id)sender;
@end

 

AppDelegate.m

#import "AppDelegate.h"

@implementation AppDelegate

-(IBAction)clickAddButton:(id)sender;
{
	NSString *defaultArgument = @"qtdefaults write MediaKey";
	NSString *categoryText = [categoryTextField stringValue];
	NSString *mediaKeyText = [mediaKeyTextField stringValue];

	NSArray *argumentsArray = [NSArray arrayWithObjects:defaultArgument, categoryText, mediaKeyText, nil];

	[NSTask launchedTaskWithLaunchPath:@"" arguments:argumentsArray];
}

@end

 

You might want to give something along the lines of the above code a try. I haven't tested it myself so I can guarantee that it will work but it contains the general idea of what you'll need to do. Firstly have 2 NSTextFields for inputting the category and mediakey, and a button for adding. When the button is clicked, get the text and create an arguments array. This will be used by NSTask to execute. I'm unsure about the path property of this so that's one thing you'll need to look into.

Hope this gives a general idea,

iPoco

Link to comment
Share on other sites

bildschirmfoto20100506u.png

 

Thank you 4 answering this precisely. Helped me a lot to understand how it could work.

I will now figure out what the errors mean and how I can fix them, if you or somebody else has an idea

before me, I surely would be grateful.

But this makes me getting interested in learning more Cocoa :unsure:

 

 

The Interface is ready, I will report if I succeed :(

 

greetz

Mortus

 

first Error solved : its AraywithObjects with s... :) need to read more careful:)

 

Ok found second error, too.. wrong place for bracket :)

 

Ok, now i solved all problems with the errors, but now I get a blank window, allthough I put 2 x NSTextField and once NSButton in the window...

 

I try to figure it out, but any help is welcome :)

Link to comment
Share on other sites

I don't think so :/. Maybe try see if your interface was saved before the build, and it never hurts to run a Clean (under the Build menu). Sometimes I've noticed XCode doesn't notice a file has been edited and it is necessary to do a Clean.

 

I'm just going to go check something. Will continue later.

Link to comment
Share on other sites

I don't think so :/. Maybe try see if your interface was saved before the build, and it never hurts to run a Clean (under the Build menu). Sometimes I've noticed XCode doesn't notice a file has been edited and it is necessary to do a Clean.

 

I'm just going to go check something. Will continue later.

 

 

Hmm, I saved every class and restarted the build and run, saved the interface Builder, restarted...nothing happened... I continue later, too thx for your interest, would be cool if I (we) could get it work... greetz Mortus

Link to comment
Share on other sites

Have you tried cleaning?

 

Yes, same result...get a blank window after build and run.

Hmm, now I see, I get a Warning "Incomplete implementation of class MkenterAppDelegate.

 

Edit:

Ahh, I built the Interface new, and then it worked... OK now I press the Button nothing happens...got no MediKey in the qtdefaults....

I try to figure out :)

 

 

 

 

 

Here the actual script lines:

bildschirmfoto20100507u.png

 

 

<br>

bildschirmfoto20100507uu.png

Link to comment
Share on other sites

I'm unsure about the path property of this so that's one thing you'll need to look into.

 

Ah, Ok I think now it`s Time to look on this part.

hmm what path is the shell.... isn't it user/bin/sh something like that?

 

And something else, I get these three warnings, they sound critical...are they?

 

bildschirmfoto20100507u.png

Link to comment
Share on other sites

Yep!

 

OK the 3 warnings:

 

1) You notice the commented NSWindow instance variable and property. When you first created the new project these were there by default and in IB the window outlet was connected to the NSWindow in the xib. When you commented out the lines IB does not remove the references outlets to it. So it presents a warning since the NSWindow has a referencing outlet to the non-existant window outlet in the MKenterAppDelegate interface. To fix this either uncomment, or remove the referencing outlet on the NSWindow in IB.

 

2/3) These are actually the same problem. Your header file (.h) contains all the information about what your class contains (ivars, properties, methods). While you can define the interface in the implementation file (.m) it is more conventional to put it in the header file. So you have in your interface declared - (IBAction)clickAddButton:(id)sender as a method, which means that it should be present in your implementation file. However in your implementation file you have - (IBAction)ckickAddButton:(id)sender ;). It doesn't matter if the "sender" part of the method is the same in the interface as in the implementation but the function name must remain the same. This could also be the reason why your button does nothing. Since the method it is suppose to call when clicked doesn't exist.

 

Glad the interface is appearing now ;).

 

Thanks,

iPoco

Link to comment
Share on other sites

Yep!

 

OK the 3 warnings:

 

1) You notice the commented NSWindow instance variable and property. When you first created the new project these were there by default and in IB the window outlet was connected to the NSWindow in the xib. When you commented out the lines IB does not remove the references outlets to it. So it presents a warning since the NSWindow has a referencing outlet to the non-existant window outlet in the MKenterAppDelegate interface. To fix this either uncomment, or remove the referencing outlet on the NSWindow in IB.

 

2/3) These are actually the same problem. Your header file (.h) contains all the information about what your class contains (ivars, properties, methods). While you can define the interface in the implementation file (.m) it is more conventional to put it in the header file. So you have in your interface declared - (IBAction)clickAddButton:(id)sender as a method, which means that it should be present in your implementation file. However in your implementation file you have - (IBAction)ckickAddButton:(id)sender :) . It doesn't matter if the "sender" part of the method is the same in the interface as in the implementation but the function name must remain the same. This could also be the reason why your button does nothing. Since the method it is suppose to call when clicked doesn't exist.

 

Glad the interface is appearing now :D .

 

Thanks,

iPoco

hi,

Sorry for answering that late.

Thx. Another error by bad reading :P . But thanx 4 the detailed explanation. All Errors are gone now.

But after pressing the "button" nothing happens. could

the path be wrong? If it works, will I see a Terminal window executing the Command, or does it just execute in the background?

Do I have to do something else in the Interface Builder?

Link to comment
Share on other sites

Have you connected the IBOutlets and IBActions in interface builder? I believe it executes in the background so you shouldn't see anything. You can set a breakpoint in the method or use NSLogs to find out if it gets executed or not.

 

I've taken a look at the NSTask documentation (http://developer.apple.com/mac/library/documentation/cocoa/reference/foundation/Classes/NSTask_Class/Reference/Reference.html) and what you could do is modify some of the code as follows:

NSTask *aTask = [[NSTask launchedTaskWithLaunchPath:@"/bin/sh" arguments:argumentsArray] waitUntilExit];

int status = [aTask terminationStatus];

if (status == ATASK_SUCCESS_VALUE)
NSLog(@"Task succeeded.");
else
NSLog(@"Task failed.");

 

Poco

Link to comment
Share on other sites

Have you connected the IBOutlets and IBActions in interface builder?

 

Do you mean by ctrl-clicking on the NSTextField and NSButton and "bind" it to

the App Delegate in the IB then click delegate or clickAddButton? Then Yes :)

 

I will try a Breakpoint and the modify, thanx for lookin it up. I will read more of the link.

Link to comment
Share on other sites

 Share

×
×
  • Create New...