Refer to class property from a string

I’m trying to access a object property, such as:

get name of item 1 of someList

What I would like to do, almost like in actionscript:

var someVal = someObj[“thePropertyName”]; //which is someObj.thePropertyName

I’m not sure if applescript is capable of this, using a string to directly refer to an object’s property.

script SomeObj
	property name : "Joe"
end script

set theName to name of SomeObj

But that’s for a user defined property correct? What if I’m trying to reference a property from the Finder dictionary, such as creation date?

Same thing.

set theFile to choose file
tell application "Finder"
	set cDate to creation date of theFile
end tell

Lemme clarify my usage a bit. I have a function that returns a specific property from a list and does some stuff to it (not important). The function call goes:

doSomething(theList, thePropertyToUse)…so…doSomething(someList, “creation date”)

but since a “creation date” cannot be converted to the the actual property name creation date I’m up the creek. I need to turn “creation date” to the actual property name creation date.

I do not believe this is possible in AppleScript.

Bummer. I’ll have to shimmy something else, thanks for the help.

You might look at appscript if you know a little Ruby or Python.

Thanks, I’ll look into appscript. From what I just glanced at it looks interesting.

Perhaps the Properties property of the file object might help.

Something like this might work.

set someObject to choose file

return GetProperty(someObject, "creation date")

on GetProperty(anObject, propertySought)
	set propertyNames to ¬
		{"class", "name", "index", "displayed name", "name extension", "extension hidden", "container", "disk", "position", "desktop position", "bounds", "kind", "label index", "locked", "description", "comment", "size", "physical size", "creation date", "modification date", "icon", "URL", "owner", "group", "owner privileges", "group privileges", "everyones privileges", "file type", "creator type", "stationery", "product version", "version"}
	repeat with myIndex from 1 to count of propertyNames
		if item myIndex of propertyNames = propertySought then exit repeat
	end repeat
	tell application "Finder"
		return item myIndex of (get properties of anObject as list)
	end tell
end GetProperty

I wonder if there is an implicit way to get the list propertyNames from the properties Property of an object.
(I copy/pasted and text-edited to create the explicit list, so I know that list of property names is somewhere inside the machine.)


you can do it with second level evaluation

set theFile to choose file
doSomething(theFile as text, "creation date")

on doSomething(theList, thePropertyToUse)
		return run script "tell application \"Finder\" to get " & thePropertyToUse & " of item  \"" & theList & "\""
	on error
		return missing value
	end try
end doSomething

Hey that might work! That’s a nifty trick. I’ll try that out in the morn. Thanks.

I assume that this means that “run script” is more or less the equivalent of some other languages’ “eval” ?