2 # Copyright © 2006-2014 Jamie Zawinski <jwz@jwz.org>
4 # Permission to use, copy, modify, distribute, and sell this software and its
5 # documentation for any purpose is hereby granted without fee, provided that
6 # the above copyright notice appear in all copies and that both that
7 # copyright notice and this permission notice appear in supporting
8 # documentation. No representations are made about the suitability of this
9 # software for any purpose. It is provided "as is" without express or
12 # Converts and installs a thumbnail image inside a .saver bundle.
14 # Created: 26-Jul-2012.
17 #use diagnostics; # Fails on some MacOS 10.5 systems
20 my $progname = $0; $progname =~ s@.*/@@g;
21 my ($version) = ('$Revision: 1.4 $' =~ m/\s(\d[.\d]+)\s/s);
25 $ENV{PATH} = "/opt/local/bin:$ENV{PATH}"; # MacPorts, for ImageMagick
31 my $exit_value = $? >> 8;
32 my $signal_num = $? & 127;
33 my $dumped_core = $? & 128;
34 error ("$cmd[0]: core dumped!") if ($dumped_core);
35 error ("$cmd[0]: signal $signal_num!") if ($signal_num);
36 error ("$cmd[0]: exited with $exit_value!") if ($exit_value);
40 # Returns true if the two files differ (by running "cmp")
43 my ($file1, $file2) = @_;
45 my @cmd = ("cmp", "-s", "$file1", "$file2");
46 print STDERR "$progname: executing \"" . join(" ", @cmd) . "\"\n"
50 my $exit_value = $? >> 8;
51 my $signal_num = $? & 127;
52 my $dumped_core = $? & 128;
54 error ("$cmd[0]: core dumped!") if ($dumped_core);
55 error ("$cmd[0]: signal $signal_num!") if ($signal_num);
61 my ($src_dir, $app_dir) = @_;
63 # Apparently Apple wants Resources/{thumbnail.png to be 90x58,
64 # and Resources/thumbnail@2x.png to be 180x116. Let's just
65 # make the former, but make it be the latter's size.
69 error ("$app_dir does not exist") unless (-d $app_dir);
70 error ("$app_dir: no name")
71 unless ($app_dir =~ m@/([^/.]+).(saver|app)/?$@x);
75 $app_dir .= "/Contents/Resources";
77 error ("$app_dir does not exist") unless (-d $app_dir);
78 my $target = "$app_dir/thumbnail.png";
80 $src_dir .= "/" unless ($src_dir =~ m@/$@s);
81 my $src_dir2 = "${src_dir}retired/";
83 $app_name =~ s/rdbomb/rd-bomb/si; # sigh
85 my $img = $src_dir . lc($app_name) . ".jpg";
86 my $img2 = $src_dir2 . lc($app_name) . ".jpg";
87 $img = $img2 if (! -f $img && -f $img2);
88 error ("$img does not exist") unless (-f $img);
90 my $tmp = sprintf ("%s/thumb-%08x.png",
91 ($ENV{TMPDIR} ? $ENV{TMPDIR} : "/tmp"),
96 "-resize", $size . "^",
99 "-quality", "95", # saves 8%
100 "+dither", "-colors", "128", # Saves an additional 61%
103 print STDERR "$progname: exec: " . join(' ', @cmd) . "\n"
109 error ("failed: " . join(" ", @cmd));
112 # This only saves 0.4% on top of the above.
113 # @cmd = ("optipng", "-quiet", "-o7", $tmp);
114 # print STDERR "$progname: exec: " . join(' ', @cmd) . "\n"
116 # safe_system (@cmd);
120 error ("failed: " . join(" ", @cmd));
123 if (! cmp_files ($tmp, $target)) {
125 print STDERR "$progname: $target: unchanged\n" if ($verbose > 1);
126 } elsif (! rename ($tmp, $target)) {
128 error ("mv $tmp $target: $!");
130 print STDERR "$progname: wrote $target\n" if ($verbose);
137 print STDERR "$progname: $err\n";
142 print STDERR "usage: $progname [--verbose] image-dir program.app ...\n";
150 while ($_ = $ARGV[0]) {
152 if (m/^--?verbose$/s) { $verbose++; }
153 elsif (m/^-v+$/) { $verbose += length($_)-1; }
154 elsif (m/^--?q(uiet)?$/s) { $verbose = 0; }
155 elsif (m/^-/s) { usage(); }
156 elsif (! $src_dir) { $src_dir = $_; }
157 else { push @files, $_; }
159 usage() unless ($src_dir && $#files >= 0);
161 update ($src_dir, $_);