Jump to content

Shell command from XCode interface


  • Please log in to reply
13 replies to this topic

#1
MortusSei

MortusSei

    InsanelyMac Protégé

  • Members
  • Pip
  • 19 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:

#2
MortusSei

MortusSei

    InsanelyMac Protégé

  • Members
  • Pip
  • 19 posts
Anybody? Did I describe the problem too bad?

#3
Poco

Poco

    InsanelyMac Plumber

  • Administrators
  • 1,494 posts
  • Gender:Male
  • Location:Canada
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

#4
MortusSei

MortusSei

    InsanelyMac Protégé

  • Members
  • Pip
  • 19 posts
Posted Image

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 :)

#5
MortusSei

MortusSei

    InsanelyMac Protégé

  • Members
  • Pip
  • 19 posts
Hmmm, I didnt get the Problem with the blank Window after build solved.

What could I have forget, anything in the properties ?

#6
Poco

Poco

    InsanelyMac Plumber

  • Administrators
  • 1,494 posts
  • Gender:Male
  • Location:Canada
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.

#7
MortusSei

MortusSei

    InsanelyMac Protégé

  • Members
  • Pip
  • 19 posts

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

#8
Poco

Poco

    InsanelyMac Plumber

  • Administrators
  • 1,494 posts
  • Gender:Male
  • Location:Canada
Have you tried cleaning?

#9
MortusSei

MortusSei

    InsanelyMac Protégé

  • Members
  • Pip
  • 19 posts

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:
Posted Image


<br>
Posted Image

#10
MortusSei

MortusSei

    InsanelyMac Protégé

  • Members
  • Pip
  • 19 posts

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?

Posted Image

#11
Poco

Poco

    InsanelyMac Plumber

  • Administrators
  • 1,494 posts
  • Gender:Male
  • Location:Canada
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

#12
MortusSei

MortusSei

    InsanelyMac Protégé

  • Members
  • Pip
  • 19 posts

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?

#13
Poco

Poco

    InsanelyMac Plumber

  • Administrators
  • 1,494 posts
  • Gender:Male
  • Location:Canada
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.app.../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

#14
MortusSei

MortusSei

    InsanelyMac Protégé

  • Members
  • Pip
  • 19 posts

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.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

© 2014 InsanelyMac  |   News  |   Forum  |   Downloads  |   OSx86 Wiki  |   Mac Netbook  |   PHP hosting by CatN  |   Designed by Ed Gain  |   Logo by irfan  |   Privacy Policy