• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
NSAppleScript returns wrong result on multiple Safari windows, applescript editor works
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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 21:27:46 -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.


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 (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


  • Prev by Date: Re: download security hanging app
  • Next by Date: NSAppleScript returns wrong result on multiple Safari windows, applescript editor works
  • Previous by thread: NSAppleScript returns wrong result on multiple Safari windows, applescript editor works
  • Next by thread: NSAppleScript returns wrong result on multiple Safari windows, applescript editor works
  • Index(es):
    • Date
    • Thread