Skip to content

Instantly share code, notes, and snippets.

@fallroot
Created August 7, 2017 06:08

Revisions

  1. fallroot created this gist Aug 7, 2017.
    198 changes: 198 additions & 0 deletions manipulating-plist-in-macos-cli.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,198 @@
    # Manipulating Property List in macOS Command Line

    ## Tools

    - [defaults](https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man1/defaults.1.html)
    - [PlistBuddy](https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man8/PlistBuddy.8.html)
    - [plutil](https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man1/plutil.1.html)

    ⚠️ defaults 명령어는 홈 경로(~)는 인식하지만 상대 경로(., ..)는 인식하지 않는다.

    ℹ️ defaults 명령어는 타입을 지정하지 않으면 문자열이 기본이다.

    ## Create

    ```
    $ defaults write <file> <key> <value>
    $ /usr/libexec/PlistBuddy -c "Save" <file>
    ```
    ⚠️ defaults 명령어는 필드없이 새 파일을 만들 수 없다.

    ⚠️ plutil 명령어는 새 파일을 만들 수 없다.

    ℹ️ PlistBuddy 명령어의 내부 명령어는 대소문자를 구분하지 않는다.

    ## Read

    ```
    $ defaults read <file> [key]
    $ defaults export <file> -
    $ /usr/libexec/PlistBuddy -c "Print [key[:(index|key1)]]" <file>
    $ plutil -p <file>
    $ plutil -convert <binary1|json|xml1> -o - <file>
    ```
    ⚠️ plutil 명령어에서 -convert 옵션은 -o 옵션과 함께 사용하지 않으면 기존 파일을 덮어쓴다.

    ℹ️ "-" 값은 표준 출력을 의미한다.

    ## Write

    ```
    $ defaults write <file> <key> [-type] <value>
    $ /usr/libexec/PlistBuddy -c "Add <key> <type> <value>" <file>
    $ plutil -insert <key> <-type> <value> <file>
    ```

    ℹ️ 데이터 타입 명칭은 아래 표처럼 프로그램마다 다른 경우가 있다.

    ------------------------------
    defaults | PlistBuddy | plutil
    -------- | ---------- | ------
    bool[ean]| bool | bool
    data | data | data
    date | date | date
    float | real | float
    int[eger]| integer | integer
    string | string | string

    ## Write Array

    ```
    $ defaults write <file> <key> '(value1, value2)'
    $ defaults write <file> <key> -array [-type] value1 [-type] value2
    $ /usr/libexec/PlistBuddy -c "Add <key> array" -c "Add <key:> <type> <value>" <file>
    $ plutil -insert <key> -json '[1, 2, 3]' <file>
    $ plutil -insert <key> -xml '<array><integer>1</integer><integer>2</integer></array>' <file>
    ```

    ## Append Array Element

    ```
    $ defaults write <file> <key> -array-add [-type] <value>
    $ /usr/libexec/PlistBuddy -c "Add <key:> <type> <value>" <file>
    ```
    ⚠️ plutil 명령어는 배열의 마지막에 요소를 추가하는 것이 어렵다. 배열의 길이를 계산 후 넣을 수는 있지만 매우 번거롭다.

    ## Insert Array Element

    ```
    $ /usr/libexec/PlistBuddy -c "Add <key:index> <type> <value>" <file>
    $ plutil -insert <key.index> <-type> <value> <file>
    ```
    ℹ️ index 값은 0부터 시작한다.

    ⚠️ defaults 명령어는 배열의 특정 위치에 요소를 삽입할 수 없다.

    ## Write Dictionary

    ```
    $ defaults write <file> <key> '{<key1>=<value1>; <key2>=<value2>;}'
    $ defaults write <file> <key> -dict <key1> [-type] <value1> <key2> [-type] <value2>
    $ /usr/libexec/PlistBuddy -c "Add <key> dict" -c "Add <key:key1> <type> <value1>" -c "Add <key:key2> <type> <value2" <file>
    $ plutil -insert <key> -json '{"key1":value1, "key2":value2}' <file>
    ```
    ⚠️ 첫 번째 문장처럼 defaults 명령어로 사전을 문자열 인자를 통해 만들 때 마지막 세미콜론을 생략하면 오류가 발생한다.

    ## Append Dictionary Element

    ```
    $ defaults write <file> <key> -dict-add <key1> [-type] <value1>
    $ /usr/libexec/PlistBuddy -c "Add <key:key1> <type> <value1>" <file>
    $ plutil -insert <key.key1> <-type> <value> <file>
    ```

    ## Update

    ```
    $ defaults write <file> <key> [-type] <value>
    $ /usr/libexec/PlistBuddy -c "Set <key>[:(index|key1)] <type> <value>" <file>
    $ plutil -replace <key>[.(index|key1)] <-type> <value> <file>
    ```
    ⚠️ defaults 명령어는 배열이나 사전의 특정 요소만 수정할 수 없다.

    ⚠️ PlistBuddy 명령어는 기존 데이터 타입을 바꿔서 수정할 수 없다.

    ## Copy

    ```
    $ /usr/libexec/PlistBuddy -c "Copy <src_key> <dst_key>" <file>
    ```

    ## Rename

    ```
    $ defaults rename <file> <old_key> <new_key>
    ```

    ## Delete

    ```
    $ defaults delete <file> [key]
    $ /usr/libexec/PlistBuddy -c "Delete <key>[:(index|key1)]" <file>
    $ plutil -remove <key>[.(index|key1)] <file>
    ```
    ⚠️ defaults 명령어는 배열이나 사전의 특정 요소만 삭제할 수 없다.

    ## Import

    ```
    $ defaults import <file> <output>
    $ /usr/libexec/PlistBuddy -c "Merge <other_file> [<key>]" <file>
    $ /usr/libexec/PlistBuddy -c "Import <key> <file>"
    ```
    ℹ️ output 값으로 표준 입력(-)을 사용할 수 있다.

    ⚠️ 표준 입력을 사용할 때에는 정확한 XML 문서를 입력해야 한다.

    ## Export

    ```
    $ defaults export <file> <output>
    $ plutil -convert <binary1|json|xml1> -o <output> <file>
    $ plutil -extract <key> <binary1|json|xml> <file>
    ```
    ℹ️ output 값으로 표준 출력(-)을 사용할 수 있다.

    ⚠️ defaults 명령어는 특정 키만 내보낼 수 없다.

    ## Summary

    ### defaults

    macOS 또는 앱이 사용하는 설정 파일을 다룰 때 파일 경로가 아닌 도메인을 사용할 수 있기 때문에 설정 파일을 다루기에 적합하다.

    ```
    $ defaults read ~/Library/Preferences/.GlobalPreferences.plist
    $ defaults read NSGlobalDomain
    $ defaults read -g
    $ defaults read ~/Library/Preferences/com.google.Chrome.plist
    $ defaults read com.google.Chrome
    ```

    ### PlistBuddy

    대화형 모드를 통해 사용하기 적합하다.

    ### plutil

    빈 파일을 생성하거나 배열 요소를 추가할 수 없다는 점을 제외하고는 가장 유용하다.