使用Javascript for automation(JXA)创建plist
我正在考虑创建一个JXA来构建plist。我的出发点是我找到的一个AppleScript。我想到了这个片段:使用Javascript for automation(JXA)创建plist,javascript,macos,automation,javascript-automation,Javascript,Macos,Automation,Javascript Automation,我正在考虑创建一个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/a
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与系统事件的plist一起使用,有什么线索吗
(如果您想知道为什么不使用AppleScript方法,是因为我通过自动化从Excel中提取数据,但需要验证数据类型的一致性和空值……javascript在我看来是一种更直接的方法,可以查看变量类型并根据需要进行更正)使用系统事件创建属性列表文件非常繁琐。
使用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);
使用系统事件创建属性列表文件非常繁琐。
使用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);
四处测试,因为我认为使用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)
享受吧,Michael/Hamburg进行了测试,因为我认为使用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)
享受吧,迈克尔/汉堡