2010-08-25 34 views
1

我对AppleScript引用感到困惑......我几乎从不在AppleScript中开发,并且在AppleScript如何处理引用时遇到了很难找到的好文档。同样的代码,但是我运行它run处理程序中,而不是foo处理程序 -为什么AppleScript不能在此测试代码中将hash的firstValue转换为类型引用?

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 

但是,下面的代码工作::为什么

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的Can’t make firstValue of hash into type reference.第一个代码示例在第二个代码示例工作时失败?更好的问题,有人可以指示我解释这个文件的方向,所以我可以知道我的错误是什么?

编辑:菲利普的答案指出我正确的方向,现在我看到了什么使我困惑。 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*) 

问题的关键是,我不能在处理程序中引用局部变量 - 但只要这些引用存储回该记录中,我就可以引用记录的某些部分,这是我之后的功能。

我怀疑任何人都不会读到这里,下到这个问题:-)

+0

你想要做什么,需要参考?我只问,因为我一直在Applescript工作多年,而且我从来没有需要参考,特别是在如此简单的情况下。您不需要创建对记录的引用来访问请求的值。我对引用的基本理解是,它与指针类似。 – 2010-08-25 20:26:52

+0

@Philip - 我不认为我需要引用,但我的代码似乎没有工作没有他们...我(试图)做的是在内存中维护一个复杂的数据结构,并将它传递给各种处理程序来处理它。我还读到使用引用速度要快得多... – Josh 2010-08-25 20:28:45

+0

@Philip:请参阅[Apple's Docs](http://developer.apple.com/mac/library/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_classes.html# // apple_ref/doc/uid/TP40000983-CH1g-BBCDBHIE),特别是关于“对于大型列表,在将大量项目插入列表时使用对运算符的引用更有效”的部分。我会看看我是否可以让我的代码无w/o裁判工作。 – Josh 2010-08-25 20:37:04

回答

0

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

我是能够使这项工作,如果我做了哈希稿件的全球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 

不是特定的问题答案,而是一个会让你度过一天的问题。

+0

谢谢@Philip!当我继续调试我的脚本时,我现在遇到了一个更复杂的参考问题,但您应该接受这个问题。如果我无法弄清我更复杂的问题,我会发布一个新问题。 – Josh 2010-08-25 21:11:28

+0

我现在看到什么让我困惑。我将编辑我的答案并发布一些额外的细节,但显然让我困惑的是“AppleScript通过引用传递所有参数*”,但这不会**意味着与“AppleScript传递参考所有参数“。换句话说,AppleScript中有两种引用! – Josh 2010-08-25 21:26:52

+0

如果你想让你的大脑受伤,请阅读我编辑的问题。我想我现在要休息一天了,哈! – Josh 2010-08-25 21:36:28