代码之家  ›  专栏  ›  技术社区  ›  Josh Andreas Rehm

为什么applescript不能在此测试代码中将哈希的FirstValue转换为类型引用?

  •  1
  • Josh Andreas Rehm  · 技术社区  · 14 年前

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

    为什么第一个代码示例在第二个代码示例工作时失败?更好的问题是,有人能指点我向文档的方向解释这一点吗?这样我就能知道我的错误是什么?

    编辑: 菲利普的回答为我指明了正确的方向,我现在明白了是什么让我困惑了。 The official AppleScript docs 声明“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*)
    

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

    我怀疑有人会把这个问题读得这么深 :-)

    1 回复  |  直到 14 年前
        1
  •  0
  •   Philip Regan    14 年前

    我真的认为这是一个范围问题。如果我在finder和inDesign中声明对所选内容的引用, foo() 获取引用和包含在Works中的值。但是,由于您没有在特定的应用程序中工作,目标必须在脚本的范围内。了解applescript_及其传奇的奇妙之处,这可能是使用引用运算符的“正确”方式。

    如果我把哈希值设为全局的话,我就可以使它工作。 property 脚本,如下所示: property hash : {firstValue:1, secondValue:2}. 然后我就可以从 英尺() . 完整示例代码:

    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
    

    这个问题不是一个具体的答案,而是一个能让你度过一天的答案。