From 68ae5111bbf372a32d1bf62f1ed18d34e0880078 Mon Sep 17 00:00:00 2001 From: Mike Clarke Date: Wed, 28 Nov 2018 21:40:02 +0000 Subject: Fix DMG creation on newer versions of macOS --- ports/darwin_dmg/package_darwin_dmg.pl | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/ports/darwin_dmg/package_darwin_dmg.pl b/ports/darwin_dmg/package_darwin_dmg.pl index c669c39..00ea95b 100755 --- a/ports/darwin_dmg/package_darwin_dmg.pl +++ b/ports/darwin_dmg/package_darwin_dmg.pl @@ -63,6 +63,33 @@ 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) = @_; @@ -134,9 +161,7 @@ run "cp -pr ../../../docs '$vol_dir/Docs'"; run "touch '$vol_dir/Rakudo/Icon\r'"; run "cp ../2000px-Camelia.svg.icns $vol_dir/.VolumeIcon.icns"; -run "sips -i $vol_dir/.VolumeIcon.icns"; -run "DeRez -only icns $vol_dir/.VolumeIcon.icns > tempicns.rsrc"; -run "Rez -append tempicns.rsrc -o '$vol_dir/Rakudo/bin/perl6'"; +set_icon "../2000px-Camelia.svg.icns", "$vol_dir/Rakudo/bin/perl6"; run "mkdir $vol_dir/.background"; run "cp ../installerbg.png $vol_dir/.background"; run "SetFile -c icnC '$vol_dir/.VolumeIcon.icns'"; @@ -144,7 +169,6 @@ 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'"; -run "rm tempicns.rsrc"; print ">>> Adjusting sizes and positions in installation window\n"; -- cgit v1.1 From 46760080c149ce7e61225330765c418b26174098 Mon Sep 17 00:00:00 2001 From: Mike Clarke Date: Thu, 29 Nov 2018 17:17:30 +0000 Subject: Add Swift helper to set custom file icons --- ports/darwin_dmg/fileicon | 33 ++++++++++++++++++++++++++++++++ ports/darwin_dmg/package_darwin_dmg.pl | 35 ++-------------------------------- 2 files changed, 35 insertions(+), 33 deletions(-) create mode 100755 ports/darwin_dmg/fileicon 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"; -- cgit v1.1