I got confused about AppleScript links ... I almost never developed into AppleScript, and it is very difficult for me to find good documentation on how AppleScript handles links. The following code crashes because AppleScript Can’t make firstValue of hash into type reference.:
on run
foo()
end run
on foo()
set the hash to {firstValue:1, secondValue:2}
set the hashRef to a reference to the hash
return the firstValue of hashRef
end foo
But the following code works - the same code, but I run it inside the handler runinstead of the handler foo:
on run
set the hash to {firstValue:1, secondValue:2}
set the hashRef to a reference to the hash
return the firstValue of hashRef
end run
Why doesn't the first code sample work while the second code sample is working? Better question, can someone point me towards the documentation explaining this so that I can find out what is my mistake?
EDIT: , , . AppleScript , "AppleScript , , , set". , , AppleScript AppleScript !
, , :
on run
foo()
end run
on isRef(someValue)
try
someValue as reference
return true
on error
return false
end try
end isRef
on foo()
log "In foo()"
set the objectList to makeObjectList()
log "objectList isRef =" & isRef(objectList)
set theObject to makeObject given id:0, name:"Test"
addObjectToObjectList(theObject, objectList)
log "foo(): object name =" & name of theObject
log item 1 of allItems of objectList
log item 1 of goodItems of objectList
set the name of item 1 of allItems of objectList to "Test3"
log item 1 of allItems of objectList
log item 1 of goodItems of objectList
end foo
on makeObjectList()
set the hash to {allItems:{}, goodItems:{}, badItems:{}}
return the hash
end makeObjectList
on makeObject given name:theName, id:theId
set theObject to {name:theName, id:theId}
return theObject
end makeObject
on addObjectToObjectList(object, objectList)
log "In addObjectToObjectList"
log "object isRef =" & isRef(object)
copy object to the end of allItems of the objectList
set objectRef to a reference to the last item in allItems of the objectList
set name of objectRef to "Test2"
log "object name =" & name of object
log "objectRef isRef =" & isRef(objectRef)
log "objectRef name =" & name of (contents of objectRef)
copy objectRef to the end of goodItems of the objectList
end addObjectToObjectList
:
(*In foo()*)
(*objectList isRef =false*)
(*In addObjectToObjectList*)
(*object isRef =false*)
(*object name =Test*)
(*objectRef isRef =true*)
(*objectRef name =Test2*)
(*foo(): object name =Test*)
(*name:Test2, id:0*)
(*name:Test2, id:0*)
(*name:Test3, id:0*)
(*name:Test3, id:0*)
, , , , , .
, - - :-)