NSAppleScript returns wrong result on multiple Safari windows, applescript editor work

  • Hello,

    I appreciate the response but neither of the two suggested solutions
    work. The one that keeps Safari in the background has the same issue,
    it returns the first Safari last tab, not the most recent Safari
    window. The second suggestion, where the applescript puts Safari as
    the frontmost app, isn't a valid solution. Having my application lose
    focus misses the point.

    I'm currently working through the Apple sample code AttachAScript to
    see if it will work.

  • Hello,

    Still working on this problem. I need to get Safari most current URL.
    NSAppleScript does not work since it returns the wrong result, or it
    needs to activate Safari which isn't a solution.

    I'm now trying osascript called via an NSTask. This works in Terminal :

    /usr/bin/osascript -e 'tell application "Safari" to get URL of current
    tab  of window 1'

    However calling this within my program results in :

    33:36: execution error: Safari got an error: Can’t get current tab of
    window 1. (-1728)

    Why ?

    Calling with this :
    NSTask *task = [[[NSTask alloc] init] autorelease];
    [task setLaunchPath: @"/usr/bin/osascript"];

    NSString *command = @"tell application \"Safari\" to get URL of
    current tab of window 1";
    NSArray *arguments = [NSArray arrayWithObjects: @"-e", command, nil];
    [task setArguments: arguments];

    NSPipe *pipe = [NSPipe pipe];
    [task setStandardOutput: pipe];
    NSFileHandle *file = [pipe fileHandleForReading];

    [task launch];

    NSData *data = [file readDataToEndOfFile];
    NSString *string = [[NSString alloc] initWithData: data encoding:
    NSLog (@"got\n%@", string);

previous month january 2009 next month
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  
Go to today