1

我正在研究创建一个 JXA 来构建一个 plist。我的出发点是我在此处找到的 AppleScript 。我想出了这个片段:

var se = Application('System Events');

var item1 = se.PropertyListItem({kind: "string", name: "employee_name", value: employeeName}).make();

var plistFile = se.PropertyListFile({name: '/Users/armando/Desktop/x.plist', PropertyListItem: [item1]}).make();

ScriptEditor 编译没有错误,文件已创建但文件上没有生成条目。我想我在如何填充处理实际条目的 PropertyListFile 属性方面遗漏了一些东西。

关于如何正确使用 JXA 和 System Event 的 plist 的任何线索?

(如果您想知道为什么不使用 AppleScript 方法是因为我通过自动化从 Excel 中提取数据,但需要验证数据类型的一致性和空值......在我看来,javascript 似乎是一种更直接的方法来研究变量类型和根据需要进行更正)

4

2 回答 2

2

使用系统事件创建属性列表文件非常繁琐。
使用 JXA,您可以直接使用 Objective-C 代码。

var employeeName = "John Doe";

var item1 = { "employee_name" : employeeName };
var plist = $.NSDictionary.dictionaryWithDictionary(item1);
plist.writeToFileAtomically( '/Users/armando/Desktop/x.plist', true);
于 2015-08-09T08:11:00.097 回答
2

进行了测试,因为我认为必须可以使用 JXA 解决问题并找到此解决方案。它看起来很简单,但确实需要一些时间才能找到:-/

// Setting some variables
plist_path = "~/Desktop/example.plist"
employeeName = "Michael"

// Storing the Application object
se = Application('System Events')

// Create PropertyListItems
propertyListItem1 = se.PropertyListItem({
    kind: "string", 
    name: "employee_name", 
    value: employeeName
});

// Create the PropertyListFile
plistFile = se.PropertyListFile({
    name: plist_path
}).make()

// Push the PropertyListItem to the PropertyListFile
plistFile.propertyListItems.push(propertyListItem1)

享受,迈克尔/汉堡

于 2015-08-12T14:24:27.147 回答