What's wrong with this access to the URL of Safari?

Could someone please explain to me why this works:

tell front document of application "Safari"
	get URL
end tell

But this does not:

get URL of front document of application "Safari" --> Can't get URL of document 1.

I’d think they both mean the same.

Hey There,

You’d think the object inheritance of the code was pretty clear, but this sort of notation fails at some level with many apps.

JXA provides a completely unambiguous line of inheritance:

Application("Safari").windows[0].currentTab.url()

This too works:

# Now is the time for all good men to come to the aid of their country.
word 1 of paragraph 1 of front document of application "Script Editor"

This works with Word 2011:

tell application "Microsoft Word"
	active window
end tell

But this won’t even compile.

active window of application "Microsoft Word"

I can’t give you the technical details, but I’m fairly sure this is due to how the application’s sdef is written.

Even when I’m tempted to write slick one-liners I usually don’t, because they’re harder to read as time passes.

The major exception to that is one-off stuff I won’t use again.


Best Regards,
Chris


{ MacBookPro6,1 · 2.66 GHz Intel Core i7 · 8GB RAM · OSX 10.11.4 }
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

When code is in an tell application block the code’s context will be compiled with target’s scripting dictionary. Because the code is out of context it doesn’t know the the property and it will not work.

The reason it will compile is because URL is also defined in the standard scripting addition and is therefore set in the global scope. Otherwise it wouldn’t even compile like in ccstone’s example. The proper way to load a dictionary in code is using the using terms from block like this:

using terms from application "Safari"
	get URL of document 1 of application "Safari"
end using terms from

Now you’ll “force” AppleScript to use the application’s dictionary during compilation making sure the URL property from Safari is used and not somewhere else. This will be translated into the proper FourCharCode and at run time the right property is asked.

Thanks, DJ. That’s a good explanation. As I’m writing a script dictionary (Sdef) for my own app (iClip), I have run into similar issues but was able to solve them all, I believe, so that one can use both forms anywhere (without the “using” clause).

BTW, does someone known when “using” was introduced?

Since iClip supports 10.6.8 and up, I like to make sure that the scripts I provide run on all the systems that iClip supports.

using terms from application was there since the beginning of AppleScript. However using terms from itself has been updated recently to not only support application dictionaries but also script libraries and to turn on scripting additions explicitly.

We may also use the one liner :

tell application "Safari" to get URL of front document

Oops, the instruction was wrongly inserted between the balises.
Thanks Christopher.

Yvan KOENIG running El Capitan 10.11.4 in French (VALLAURIS, France) lundi 25 avril 2016 14:57:27