Cannot extract the container from System Events item properties.

I’m updating an old script I found with lots of deprecated info for variables. Trying to convert all of them to corresponding “System Events” properties, I stumbled when trying to extract an item’s container from its System Events properties.
This is still working in 10.11.3:

tell application "Finder"
	set mySelection to selection as alias list
	set itemContainer to container of item 1 of mySelection as text
end tell
return itemContainer --> "HD-1:Users:username:Desktop:" --> OK

But this current “System Events” container property fails to return the container cleanly:

tell application "Finder" to set mySelection to selection as alias list
tell application "System Events"
	set itemContainer to container of item 1 of mySelection
	return itemContainer
	--> folder "HD-1:Users:usernames:Desktop:" of application "System Events"
	-- The container path is there but not alone. What is the meaning of that?
	-- Can it be extracted?
end tell

Although the container property is there on the first line of the property list,
the extraction fails the same way, surrounded by artefacts.

tell application "Finder" to set mySelection to selection as alias list
tell application "System Events"
	set itemProperties to get properties of item 1 of mySelection
	return itemProperties
	--> {short version:"1.0", container:folder "HD-1:Users:usernames:Desktop:" of application "System Events", path:"HD-1:Users:usernames:Desktop:Scratch.scptd:", file type:missing value, volume:"HD-1", physical size:missing value, URL:"file:///Users/username/Desktop/Scratch.scptd/", id:"Scratch.scptd,-100,1267154"...}
	set itemContainer to container of itemProperties
	return itemContainer
	--> folder "HD-1:Users:chris:Desktop:aaa:" of application "System Events"
end tell

Failing that, I did it this way:

tell application "Finder" to set mySelection to selection as alias list
tell application "System Events"
	set itemPath to item 1 of mySelection as text
	set itemName to name of item 1 of mySelection as text
	-- Done with text manipulation:
	set itemContainer to items 1 through (-2 - (length of itemName)) of itemPath as text
	return itemContainer --> "HD-1:Users:username:Desktop:" --> OK
end tell

Please, can somebody explain why extracting the container from the “System Events” properties list fails. Is this a bug?
What am I missing?

You just missed an important detail.
When asked for the container of an item, the Finder returns the path of the folder containing the item.
When asked for the container of an item, System Events returns a System Events object whose path is what was returned by the Finder
Here extracting the container behaves flawlessly.

tell application "Finder" to set mySelection to selection as alias list
tell application "System Events"
	set itemContainer to container of (item 1 of mySelection)
	--> folder "HD-1:Users:chris:Desktop:" of application "System Events"
	properties of itemContainer
	-->{name extension:"", volume:"HD 1", package folder:false, class:folder, modification date:date ".", physical size:missing value, URL:"file:///Users/chris/Desktop/", displayed name:"Desktop", path:"HD 1:Users:chris:Desktop:", POSIX path:"/Users/chris/Desktop/", container:folder "HD 1:Users:chris:" of application "System Events", id:"Desktop,-100,1754323", size:missing value, busy status:false, visible:true, name:"Desktop", creation date:date "."}
	path of itemContainer
	--> "HD 1:Users:chris:Desktop:"
end tell

and of course, the bare code would be :

tell application "Finder" to set mySelection to selection as alias list
tell application "System Events"
path of container of (item 1 of mySelection)
	--> "HD 1:Users:chris:Desktop:"
end tell

Thanks to this difference we may choose to get the HFS path or the POSIX one.

tell application "Finder" to set mySelection to selection as alias list
tell application "System Events"
	POSIX path of container of (item 1 of mySelection)
	--> "/Users/chris/Desktop/"
end tell

Yvan KOENIG running El Capitan 10.11.3 in French (VALLAURIS, France) samedi 5 mars 2016 15:33:29

Hi Yvan,
Many thanks for your quick and comprehensive reply. I had the feeling I was missing something which I should have known. Thanks again for the clear explanation of the differences.

Cheers,
Chris

Thanks for the feedback.

Yvan KOENIG running El Capitan 10.11.3 in French (VALLAURIS, France) samedi 5 mars 2016 18:54:31