Меня смущают ссылки AppleScript ... Я почти никогда не разрабатываю на AppleScript, и мне очень трудно найти хорошую документацию о том, как AppleScript обрабатывает ссылки. Следующий код не работает, потому что 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
Но работает следующий код - тот же код, но я запускаю его внутри обработчика run
вместо обработчика 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
Почему первый пример кода не работает, а второй пример кода работает? Лучший вопрос, может ли кто-нибудь указать мне направление документации, объясняющей это, чтобы я мог узнать, в чем моя ошибка?
РЕДАКТИРОВАТЬ: Ответ Филиппа направил меня в правильном направлении, и я теперь вижу, что меня смутило. В официальных документах 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*)
Суть в том, что я не могу делать ссылки на локальные переменные в обработчике - но я могу делать ссылки на части записи, если эти ссылки хранятся обратно в этой записи, что является той функциональностью, которой я пользовался после .
Я сомневаюсь, что кто-нибудь когда-нибудь прочтет этот вопрос так глубоко :-)