From: Jot Kali <j...@mac.com>
Date: December 18, 2008 2:54:30 PM MST
To: Cocoa-dev@lists.apple.com
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.


Try this.

@"\ntell application \"Safari\" to tell current tab of window 1 to return get URL\n"

Window 1 is always the frontmost window. I have had better results targeting it instead of the document.

Cheers,

Craig
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to