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


  • Prev by Date: NSAppleScript returns wrong result on multiple Safari windows, applescript editor works
  • Next by Date: Re: NSData downloaded over socket is bigger than it should be...
  • Previous by thread: NSAppleScript returns wrong result on multiple Safari windows, applescript editor works
  • Next by thread: Problem when userSpaceScaleFactor is not 1.0
  • Index(es):
    • Date
    • Thread