This is a cocoa app that adds a right click menu option to change whether a file is executable. It works by executing the system command chmod
on each of the files. But I want to know if there is a better way to change the permission of a file, such as a cocoa function that can change the permission of a file.
What are some other ways that I can improve my code?
#import "AppDelegate.h"
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
[NSApp setServicesProvider:self];
NSUpdateDynamicServices();
}
void runSystemCommand(NSString *cmd)
{
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/bin/chmod"];
if ([[NSFileManager defaultManager] isExecutableFileAtPath:cmd]) {
//NSLog(@"is executable \n");
[task setArguments:@[ @"-x", cmd]];
[task launch];
} else {
//NSLog(@"is not executable \n");
[task setArguments:@[ @"+x", cmd]];
[task launch];
}
}
- (void)handleServices:(NSPasteboard *)pboard
userData:(NSString *)userData
error:(NSString **)error {
if([[pboard types] containsObject:NSFilenamesPboardType]){
NSArray* fileArray=[pboard propertyListForType:NSFilenamesPboardType];
int i;
int count;
count = [fileArray count];
for (i = 0; i < count; i++){
//NSLog (@"%@", [fileArray objectAtIndex: i]);
runSystemCommand([fileArray objectAtIndex: i]);
}
}
}
@end
Here is the repository for the complete project.