【问题标题】:Use Javascript for automation (JXA) to create a plist使用 Javascript 实现自动化 (JXA) 创建 plist
【发布时间】:2015-08-12 22:24:27
【问题描述】:

我正在研究创建一个 JXA 来构建一个 plist。我的出发点是我发现here 的AppleScript。我想出了这个sn-p:

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 是一种更直接的研究方法变量类型并根据需要进行更正)

【问题讨论】:

    标签: javascript macos automation javascript-automation


    【解决方案1】:

    使用系统事件创建属性列表文件非常繁琐。
    使用 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);
    

    【讨论】:

      【解决方案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)
      

      享受吧,迈克尔/汉堡

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-02-10
        • 1970-01-01
        • 2016-10-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-18
        • 1970-01-01
        相关资源
        最近更新 更多