Shell - obj-c Full Example

JB sundown at pacifier.com
Tue Jun 27 23:09:51 EDT 2017


The following is a full example of how to compile
and objective-c file with gcc and run with Livecode.

1.  Open a text editor and paste the following
objective-c code:

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString *arg1 = [NSString stringWithUTF8String:argv[1]];
        NSString *arg2 = [NSString stringWithUTF8String:argv[2]];
        
        NSLog(@"\nArgument 1: %@\n", arg1);
        NSLog(@"\nArgument 2: %@\n", arg2);
    
    
        NSError * error = NULL;
        //NSString *myString = @"This is simple test."; //Assuume the string you want to write is this
        NSString *myString = arg1;
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        //NSString *path = [documentsDirectory stringByAppendingPathComponent:@"myfile.txt"];
        NSString *path = [documentsDirectory stringByAppendingPathComponent:arg2];
        
        //[myString writeToFile:path atomically:NO encoding:NSUTF8StringEncoding error:&error];
        
        BOOL success = [myString writeToFile:path atomically:NO encoding:NSUTF8StringEncoding error:&error];
        if(success == NO)
        {
            NSLog( @"\nerror saving to %@ - %@\n", path, [error localizedDescription] );
        } else {
            NSLog( @"\nThe file %@ was saved.\n", path);
        }
    }
    
    return 0;
}


2.  Save the file on desktop as argv_string.m

3.  Open the Terminal app and type the following
to compile your file;

cd ~/desktop
gcc argv_string.m -o argv_string -framework Foundation

The compiled file should now be on the desktop.

4.  Paste the following in a button in Livecode.
Make sure you created a field and enter it in
the code below;

on mouseUp
   set the defaultFolder to "/Users/JB/Desktop/"
   put quote & "This is a simple test." & quote into tFILE1
   put quote & "My File.txt" & quote into tFILE2
   put shell( "./argv_string" && tFILE1 && tFILE2) into pData
   put pData into fld <YOURFIELD>
   beep 2
end mouseUp

good luck!
JB





More information about the use-livecode mailing list