2013-01-10 70 views
1

我有两个苹果脚本编译的AppleScript像 “编译”:如何使用命令从 “AppleScript的编辑器”

parent.scpt: property a:1

child.scpt: property parent:load script POSIX file ".../parent.scpt" return a

我在parent.scpt中将a的值更改为“2”,运行“sudo osacompile .../child.scpt”,然后“sudo osascript .../child.scpt” 它仍然获得值“1”,但是如果我从“AppleScript编辑器”“编译”child.scpt,我可以得到正确的值“2”

我错过了什么吗?如何实现这个使用命令?

回答

4

在您重新编译脚本之前,属性是持久的。如果您在设置属性时加载脚本文件,则在编译时完成,而不是在运行时完成。这意味着脚本的副本被存储并且属性与文件无关。它与脚本编辑器一起工作的原因是该文件被重新编译,这意味着parent.scpt会再次被加载到最新版本。

我不推荐加载父对象,最好加载子对象。现在你从父母链的底部开始,更好地建立从根对象开始的对象树。

看着你正在尝试在对象树中动态添加对象的代码。要做到这一点的方法之一是这样的:

parent.scpt:

property name : "I'm the parent" 
property b : 100 

set theChildLib to load script POSIX file "/Users/shortname/Desktop/child.scpt" 
set theChild to theChildLib's newChildObject(me) 
return {theChild's parent's name, theChild's name, theChild's a, theChild's b} 

child.scpt:

on newChildObject(_parent) 
    script childObject 
     property parent : _parent 
     property name : "I'm the child" 
     property a : 2 
    end script 
    return childObject 
end newChildObject 

正如你看到的,我可以打电话从孩子家长。当我调用对象中不存在的属性时,它将遵循父母链。

+0

感谢您的解决方案,它的工作原理。 –

+0

想出了一个简单的解决方案,如果有人按照我的例子:将代码从child.scpt复制到纯文本文件说child.txt,运行“sudo osacompile -o child.scpt child.txt”每次运行之前“sudo osascript孩子.scpt“,或者运行”sudo osascript child.txt“ –