Error Message: The specified object is a property, not an element

safari

(Michael Mays) #1

The script below was working last December but when I ran the script today it said:
“The specified object is a property, not an element.”
???

set myWindow to makeMyWindow()
tell application "Safari" 	
	set the URL of document 1 of myWindow to ("https://www.apple.com")	
end tell
on makeMyWindow()
	set aName to ((random number from 10000000 to 99999999 as integer) as text)
	tell application "Safari"
		make new document with properties {name:aName}
		set everyWindow to every window
		repeat with eachWindow in everyWindow
			tell eachWindow
				set everyDocument to every document
				repeat with eachDocument in everyDocument
					tell eachDocument
						if name is aName then
							return eachWindow
						end if
					end tell
				end repeat
			end tell
		end repeat
	end tell
end makeMyWindow

(Michael Mays) #2

I changed
set the URL of document 1 of myWindow to ("https://www.apple.com")
to
set the URL of tab 1 of myWindow to ("https://www.apple.com")
and it is ‘working’.


(Nigel Garvey) #3

Hi.

Yeah. In Safari, a document’s either an element of Safari itself or a property of a window, equivalent to the current tab. So any of these …

tell application "Safari"
	-- Specifically the window's first tab.
	set the URL of tab 1 of myWindow to ("https://www.apple.com")
	-- The window's currently showing tab/document.
	set the URL of current tab of myWindow to ("https://www.apple.com")
	-- Ditto.
	set the URL of document of myWindow to ("https://www.apple.com")
	-- Safari's front document — ie. the currently showing document of its front window.
	set the URL of document 1 to ("https://www.apple.com")
end tell

… but not what you tried originally.


(Michael Mays) #4

My problem was that I had ran this script section all of last year and years before in several scripts without the error message. Then yesterday I ran the script and it generated an error message.

Does a window in Safari have only a single document and one doesn’t have to say which document index? Is this new?


(Nigel Garvey) #5

As far as Safari’s AppleScript terminology’s concerned, yes. A Safari window has a ‘document’ property whose value is the document currently showing in that window. Safari itself contains things called ‘documents’ which are all the documents currently showing in any of the windows. Safari windows contain things called ‘tabs’ which are all the documents in those windows, showing or not. :-}

Well…. Yes and no, possibly. In the dictionary of Safari 5.0.6 on my old G5, there’s nothing linking documents with windows at all. But with two tabs in the front window, the following all return a single string which is the URL of the current tab:

tell application "Safari"
	URL of document of window 1
	URL of document 1 of window 1
	URL of documents of window 1
end tell

However, this produces the “object is property not element” error:

tell application "Safari"
	URL of document 2 of window 1
end tell

So at least as long ago as 2011, Safari windows may have had ‘document’ properties, Safari’s dictionary didn’t mention this, and certain element specifiers worked when they shouldn’t have! I don’t know when this was tightened up, but I can believe your script used to work.