Apple Script + Cocoa Application

Discussion in 'Mac Programming' started by ruhi, Jul 16, 2009.

  1. macrumors member

    Joined:
    Jun 17, 2009
    #1
    Hello, I am trying to add a small apple script to my cocoa application.

    what do i need to do to make it execute and give desired result

    Following are the two ways i used:
    1.

    NSString *scriptPath = [[NSBundle mainBundle] pathForResource: @"your_script" ofType: @"scpt"];
    NSAppleScript *theScript = [[NSAppleScript alloc] initWithContentsOfURL: [NSURL fileURLWithPath: scriptPath] error: nil];

    But here i found that scriptPath come out to be nil.

    what is the problem here i am getting the path for other resources but not for my script.

    2.

    The script is

    tell application "System Events"
    set frontApp to name of first application process whose frontmost is true
    end tell
    tell application frontApp
    if the (count of windows) is not 0 then
    set window_name to name of front window
    end if
    end tell

    so i wrote:

    NSAppleEventDescriptor *str;
    NSAppleScript *playScript;
    playScript = [[NSAppleScript alloc] initWithSource:mad:"tell application \"System Events\" \n set frontApp to name of first application process \n whose frontmost is true \n end tell \n tell application frontApp \n if the (count of windows) is not 0 then \n set window_name to name of front window \n end if\n end tell"];
    str=[playScript executeAndReturnError:&dict];
    NSLog(@"err - %@",dict);
    NSLog(@"Output --- %@",str );


    Here is the error its giving and Output always comes null that should give title of active window:

    err - {
    NSAppleScriptErrorBriefMessage = "Expected \U201cend\U201d or \U201cend tell\U201d but found \U201cwhose\U201d.";
    NSAppleScriptErrorMessage = "Expected \U201cend\U201d or \U201cend tell\U201d but found \U201cwhose\U201d.";
    NSAppleScriptErrorNumber = -2741;
    NSAppleScriptErrorRange = NSRange: {87, 5};
    }
    2009-07-16 15:50:43.509 Web[2964:10b] Output --- (null)

    What is the problem here? where i am wrong. Please correct me.

    Do i need to add some framework or do some changes in plist for the apple script to run in my cocoa application
     
  2. thread starter macrumors member

    Joined:
    Jun 17, 2009
    #2
    Solved

    Go the problem solved thanx.
     

Share This Page