1

我对 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

其输出如下:

(*在 foo()*)
(*objectList isRef =false*)
(*在 addObjectToObjectList*)
(*object isRef =false*)
(*对象名称=测试*)
(*objectRef isRef =true*)
(*objectRef 名称 =Test2*)
(*foo(): 对象名 =Test*)
(*名称:Test2, id:0*)
(*名称:Test2, id:0*)
(*名称:Test3, id:0*)
(*名称:Test3, id:0*)

关键是,我不能在处理程序中引用局部变量——但我可以引用记录的某些部分,只要这些引用被存储回该记录,这是我所追求的功能。

我怀疑有人会深入了解这个问题:-)

4

1 回答 1

0

我真的认为这是范围的问题。如果我在 Finder 和 InDesign 中声明对选择的引用,则从foo() 获取引用和包含在作品中的值。但是由于您不在特定的应用程序中工作,因此目标必须在脚本的范围内。了解 Applescript 及其传奇的古怪之处可能是使用引用运算符的“正确”方式。

如果我将哈希设置property为脚本的全局变量,我就能够完成这项工作,如下所示:property hash : {firstValue:1, secondValue:2}.然后我能够成功地从foo(). 完整示例代码:

property hash : {firstValue:1, secondValue:2}

on run
    foo()
end run

on foo()
    set the hashRef to a reference to the hash
    return the firstValue of hashRef
end foo

不是对问题的具体答案,而是可以帮助您度过一天的答案。

于 2010-08-25T21:06:31.067 回答