node-xattr
A library to manipulate xattr on macOS with Typescript support. APIs provided by this library are similar to node's fs module.
- Requirements
- Installation
- When to use the sync version
- Get xattr
- Set xattr
- List xattr
- Remove xattr
- Set Custom Icon For File
- Parse FileMeta
What's xattr
Extended attributes are arbitrary metadata stored with a file, but separate from the filesystem attributes (such as modification time or file size). The metadata is often a null-terminated UTF-8 string, but can also be arbitrary binary data.
Xattr is a mechanism provided by the system. With xattr, you can store your own data as attributes to file. Also, you can pass data to Finder or other apps.
For example, you can set custom icon for a file:
You can add a com.apple.quarantine
xattr to make the system check the origin of the file you download:
Or you can remove this xattr on an existing file, and this window will not display.
Requirements
Runtime
- Node.js v10+ (Electron with Node.js 10+ works)
Development
- macOS 10.14+ with XCode
Installation
$ yarn add node-xattr
When to use the sync version
Technically, the sync version would be a little faster. Because the async version waits for a queue to schedule. Also, The sync verion is realtime, it would be an advantage in some scenarios. The disadvantage of the sync version is that it will probably block the process. So DO NOT use sync version in some UI process(such as the renderer process of Electron). The best scenario to use sync version is in background worker/process/thread.
Set xattr
setXattrSync(path: string, name: string, value: string | Buffer): void;
setXattr(path: string, name: string, value: string | Buffer): Promise<void>;
Sync
const setXattrSync = ;;
Async
const setXattr = ; ;
Get xattr
getXattrSync(path: string, name: string): Buffer;
getXattrSync(path: string, name: string, encoding: string): string;
getXattr(path: string, name: string): Promise<Buffer>;
getXattr(path: string, name: string, encoding: string): Promise<string>;
Sync
const getXattrSync = ;const buffer = ;const string = ;
Async
const getXattr = ; ; ; ;
List xattr
listXattrSync(path: string): string[];
listXattr(path: string): Promise<string[]>;
Sync
const listXattrSync = ; const list = ;
Async
const listXattr = ; ;
Remove xattr
removeXattrSync(path: string, name: string): void;
removeXattr(path: string, name: string): Promise<void>;
Sync
const removeXattrSync = ;;
Async
const removeXattr = ;;
Set Custom Icon
setCustomIcon
are in macUtils
namespace:
setCustomIcon(filePath: string, iconPath: string): Promise<void>;
setCustomIconSync(filePath: string, iconPath: string): void;
Sync
const macUtils = ; macUtils;
Async
const macUtils = ;macUtils;
Parse FileMeta
This library provide utils to parse some content written by system:
serializeArrayOfString(content: string[]): Buffer;
deserializeArrayOfString(buffer: Buffer): string[];
You can use above functions in macUtils
to parse com.apple.metadata:kMDItemWhereFroms
.