为什么不能做的AppleScript哈希firstValue到本次测试code型的参考? [英] Why Can't AppleScript make firstValue of hash into type reference in this test code?

查看:123
本文介绍了为什么不能做的AppleScript哈希firstValue到本次测试code型的参考?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对AppleScript的引用困惑......我几乎从来没有在AppleScript的发展,我有一个非常困难的时间找到对的AppleScript如何处理好引用文档。下面code失败,因为AppleScript的不能使firstValue散列入式引用

I am confused by AppleScript references... I almost never develop in AppleScript, and am having a very difficult time finding good documentation on how AppleScript handles references. The following code fails 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

但下面code ++工程 - 同code,但我运行它的内运行处理程序,而不是处理程序:

But the following code works -- same code, but I am running it inside the run handler instead of the foo handler:

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

为什么第一个code例如失败,而第二个code示例工作?更好的问题,可以有人点我的文档的方向解释这个,所以我可以了解我的错误是什么?

Why does the first code example fail while the second code example works? Better question, can someone point me in the direction of documentation explaining this so I can learn what my mistake is?

编辑:菲利普的回答我指出了正确的方向,我现在看到我弄糊涂了。 <一href=\"http://developer.apple.com/mac/library/documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_about_handlers.html#//apple_ref/doc/uid/TP40000983-CH206-SW4\"相对=nofollow>官方AppleScript的文档状态的AppleScript传递通过引用的所有参数,这意味着,一个传递变量的处理程序,并调用者之间共享,好象该处理程序一直使用该组创建的变量命令。 然而,这并不意味着AppleScript的是传递一个的 AppleScript的参考对象的每个参数!

Philip's answer pointed me in the right direction and I now see what confused me. The official AppleScript docs state that "AppleScript passes all parameters by reference, which means that a passed variable is shared between the handler and the caller, as if the handler had created a variable using the set command". However, this does not mean that AppleScript is passing an AppleScript Reference Object as each parameter!

下面是下面的,更细致,code例如,要显示我公司开发的最终工作的解决方案:

Here's the following, more detailed, code example, to show the final working solution I developed:

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*)

问题的关键是,我无法在处理程序中作出局部变量的引用 - 但我可以对长记录的部分引用那些引用存储回该记录,这是我以后的功能

The point being, I can't make references to local variables within a handler -- but I can make references to parts of a record as long as those references are stored back into that Record, which is the functionality I was after.

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

I doubt anyone will ever read this far down into this question :-)

推荐答案

我真的认为这是范围的问题在这里。如果我从内 foo的声明在两个Finder中和InDesign一个选择的参考()获得包含作品中的参考和值。但是,因为你没有一个特定的应用程序中工作,目标必须是在脚本的范围。知的AppleScript和它的传奇wonkiness,这可能是使用参考运营商的正确的方式。

I really think it's an issue of scope here. If I declare a reference to a selection in both the Finder and InDesign from within foo() getting a reference and values contained within works. But since you're not working within a specific app, the target has to be in the scope of the script. Knowing Applescript—and its legendary wonkiness—that could be the "correct" way of using the reference operator.

我是能够使这项工作,如果我做剧本的哈希一个全球性的属性,如下所示:属性哈希:{firstValue :1,secondValue:2} 当时我能够从 foo的成功调用值()。完整的例子code:

I was able to make this work if I made the hash a global property of the script, like the following: property hash : {firstValue:1, secondValue:2}. I was then able to call the value successfully from foo(). Full example code:

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

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

Not a specific answer to the question, but one that will get you through your day.

这篇关于为什么不能做的AppleScript哈希firstValue到本次测试code型的参考?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆