aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Clarke <mike@lambdafunctions.com>2018-11-29 17:17:30 +0000
committerMike Clarke <mike@lambdafunctions.com>2018-11-29 17:17:30 +0000
commit46760080c149ce7e61225330765c418b26174098 (patch)
treedd283401706e7292542b3c5617e3018e939e2b21
parent68ae5111bbf372a32d1bf62f1ed18d34e0880078 (diff)
Add Swift helper to set custom file icons
-rwxr-xr-xports/darwin_dmg/fileicon33
-rwxr-xr-xports/darwin_dmg/package_darwin_dmg.pl35
2 files changed, 35 insertions, 33 deletions
diff --git a/ports/darwin_dmg/fileicon b/ports/darwin_dmg/fileicon
new file mode 100755
index 0000000..1e1d2a2
--- /dev/null
+++ b/ports/darwin_dmg/fileicon
@@ -0,0 +1,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")
+}
diff --git a/ports/darwin_dmg/package_darwin_dmg.pl b/ports/darwin_dmg/package_darwin_dmg.pl
index 00ea95b..7c3ade6 100755
--- a/ports/darwin_dmg/package_darwin_dmg.pl
+++ b/ports/darwin_dmg/package_darwin_dmg.pl
@@ -63,33 +63,6 @@ die $USAGE if @ARGV != 2
or $mm !~ m{ \A \d{2} \z }msx
or $mm < 1 or $mm > 12;
-# Sadly, Apple decided to remove the `-i` / `--addicon` option from the `sips`
-# utility. Therefore, use of Cocoa is required, which we do via Python,
-# which has the added advantage of creating a _set_ of icons from the source
-# image, scaling as necessary to create a 512 x 512 top resolution icon
-# (whereas sips -i created a single, 128 x 128 icon).
-#
-# Thanks go to https://github.com/mklement0/fileicon/blob/master/bin/fileicon
-# and https://apple.stackexchange.com/a/161984/28668
-#
-# Note: setIcon_forFile_options_() seemingly always indicates True, even with
-# invalid image files, so we attempt no error handling in the Python code.
-sub set_icon {
- croak if not @_;
- my ($img_file, $target_file) = @_;
-
- print "Setting icon for $target_file\n" if $opt_verbose;
-
- my $rc = system(qq{/usr/bin/python - "$img_file" "$target_file" <<'EOF'
-import Cocoa
-import sys
-
-Cocoa.NSWorkspace.sharedWorkspace().setIcon_forFile_options_(Cocoa.NSImage.alloc().initWithContentsOfFile_(sys.argv[1].decode('utf-8')), sys.argv[2].decode('utf-8'), 0)
-EOF
- });
- die if $rc != 0;
-}
-
sub run {
croak if not @_;
my (@command) = @_;
@@ -159,16 +132,12 @@ run "CpMac -r '$src_dir' '$vol_dir'";
run "cp ../HOW_TO_INSTALL.txt '$vol_dir/README.txt'";
run "cp -pr ../../../docs '$vol_dir/Docs'";
-run "touch '$vol_dir/Rakudo/Icon\r'";
run "cp ../2000px-Camelia.svg.icns $vol_dir/.VolumeIcon.icns";
-set_icon "../2000px-Camelia.svg.icns", "$vol_dir/Rakudo/bin/perl6";
+run "../fileicon set '$vol_dir/Rakudo/bin/perl6' ../2000px-Camelia.svg.icns";
run "mkdir $vol_dir/.background";
-run "cp ../installerbg.png $vol_dir/.background";
+run "cp ../installerbg.png $vol_dir/.background";
run "SetFile -c icnC '$vol_dir/.VolumeIcon.icns'";
run "SetFile -a C '$vol_dir'";
-run "SetFile -a C '$vol_dir/Rakudo'";
-run "SetFile -a C '$vol_dir/Rakudo/bin/perl6'";
-run "SetFile -a V '$vol_dir/Rakudo/Icon\r'";
print ">>> Adjusting sizes and positions in installation window\n";