aboutsummaryrefslogtreecommitdiff
path: root/ports/darwin_dmg/fileicon
blob: 1e1d2a268ebce7eb7109a0945294e269e83a7bc9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#! /usr/bin/swift

import Darwin
import Cocoa

func die(_ msg: String) {
    fputs("\(msg)\n", stderr)
    exit(1)
}

let name = CommandLine.arguments[0]

/*
 * We don't do anything with the 'set' at the moment; it's just there to
 * reserve a space for a subcommand to allow for future expansion without
 * changing the interface.
 */
if CommandLine.argc == 4 && CommandLine.arguments[1] == "set" {
    let target_file = CommandLine.arguments[2]
    let icon_file_name = CommandLine.arguments[3]

    if let icon = Cocoa.NSImage.init(contentsOfFile: icon_file_name) {
        if !NSWorkspace.shared.setIcon(icon, forFile: target_file) {
            die("Failed to set icon for '\(target_file)'")
        }
    }
    else {
        die("Failed to read icon file '\(icon_file_name)'")
    }
}
else {
    die("Usage: \(name) set FILE ICON_FILE")
}