NSAppleScript returns wrong result on multiple Safari windows, applescript editor works
NSAppleScript returns wrong result on multiple Safari windows, applescript editor works
- Subject: NSAppleScript returns wrong result on multiple Safari windows, applescript editor works
- From: Craig Williams <email@hidden>
- Date: Thu, 18 Dec 2008 22:32:31 -0700
From: Jot Kali <email@hidden>
Date: December 18, 2008 2:54:30 PM MST
To: email@hidden
Subject: NSAppleScript returns wrong result on multiple Safari
windows, applescript editor works
Hello,
I'm trying to get the URL of whatever is the front window / selected
tab in Safari. But for multiple windows it does not work.
Say the user opens a window in Safari and navigates to apple.com,
opens a tab and goes to nytimes.com, opens a NEW window and goes to
arstechnica.com
If I run this script in Script Editor it works every time, returns
arstechnica.com (the URL of the new front window) :
tell application "Safari"
return URL of front document as string
end tell
However if I do this in my Cocoa app :
NSDictionary *dict;
NSAppleEventDescriptor *result;
NSAppleScript *script = [[NSAppleScript alloc] initWithSource:
@"\ntell application \"Safari\"\n\tget URL of front document \nend
tell\n"];
result = [script executeAndReturnError:&dict];
[script release];
if ((result != nil) && ([result descriptorType] != kAENullEvent))
{;
NSLog (@"URL %@", [result stringValue]);
}
It returns nytimes.com. It always returns the last tab of the FIRST
window that was opened in safari. I have to close that window to get
it to return the actual front most window site arstechnica.com.
I tried both 'front document' and 'document 1', both no go.
Any hints on what is going wrong?
thanks.
I just realized you do not have an 'activate' statement in your code.
If Safari is hidden at the time the code is executed you will need an
'activate' and 'delay'
statement to get consistent return value.
@"\ntell application \"Safari\" \nactivate\ndelay .2\ntell current tab
of window 1 to return get URL\nend\n"
Craig
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden