NSAppleScript returns wrong result on multiple Safari windows, applescript editor work
Jot Kali NSAppleScript returns wrong result on multiple Safari windows, applescript editor work Jan 06 2009, 18:47Hello,
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.
Jot Kali NSAppleScript returns wrong result on multiple Safari windows, applescript editor work Jan 09 2009, 09:08Hello,
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)
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];
NSData *data = [file readDataToEndOfFile];
NSString *string = [[NSString alloc] initWithData: data encoding:
NSLog (@"got\n%@", string);