Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS - reading macOS .plist files

I have a .plist file on my mac that I would like to read. I can easily open it using Xcode but would like to do it using NodeJS.

I found plist package and I'm trying to use it like this:

import fs from 'fs';
import plist from 'plist';

fs.readFile('/my/path/here.plist', 'utf-8', (err, data) => {
  console.log('#data', data);
  const obj = plist.parse(data);
  console.log('#obj', obj);
});

What I'm getting as data is looking like this (a part from that file)

dataYdisplayasZfile-label]file-mod-date[arrangement_preferreditemsize��-   O(book(0$Usersnyc    Downloads �i(�   +� HXhA���L�  file:///Macintosh

plist package is throwing errors as I guess it expects string in XML format.

How can I read a .plist file like this?

like image 662
mdmb Avatar asked Oct 20 '25 02:10

mdmb


2 Answers

I’d recommend using an appropriate library such as bplist-parser (read-only) or simple-plist (read-write, wraps bplist-parser and bplist-creator, also handles plain text plist) instead of using the child_process module. Don’t forget that exec needs special care (e.g. arguments escaping) and it’s easy to shoot yourself in the foot using it!

import { parseFile } from 'bplist-parser';

parseFile('./path/to/binary/file.plist', callback);
like image 87
Iso Avatar answered Oct 21 '25 16:10

Iso


Ok, found the solution. Guess posting to SO gives me more luck in googling...

macOS .plist files are mostly in binary. There is a command plutil that allows you to convert to binary to xml, so to do this in node you have to:

import { exec } from 'child_process';

const command = 'plutil -convert xml1 ./path/to/binary/file.plist';
exec(command, callback); // Here you gonna convert this file to plain `xml`.

Then you just have to repeat my steps from above - read the file using fs.readFile and then convert it using plist package so you get a nice JS object.

Hope this helps someone!

like image 43
mdmb Avatar answered Oct 21 '25 15:10

mdmb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!