Skip to content

Instantly share code, notes, and snippets.

@jjrscott
Created September 16, 2016 08:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jjrscott/48979dbca4ec633fbb511bb363bcdf06 to your computer and use it in GitHub Desktop.
Save jjrscott/48979dbca4ec633fbb511bb363bcdf06 to your computer and use it in GitHub Desktop.
Fill up the ever expanding AppIcon.appiconset directory using a single PDF.
#!/usr/bin/perl
#
# This script will take your nice AppIcon in PDF format and resize it to all the different
# sizes inside the AppIcon.appiconset directory.
#
# Usage example:
#
# ./pdf2appiconset.pl <AppIcon.appiconset directory> <PDF path>
#
use strict;
use Data::Dumper;
use JSON::PP;
use File::Slurp;
my $content = read_file($ARGV[0].'/Contents.json');
my $json = JSON::PP->new;
my $data = $json->decode( $content );
my %filenames;
foreach my $image (@{$data->{'images'}})
{
my ($width, $height) = $image->{'size'} =~ m/^(\d+(?:.\d+)?)x(\d+(?:.\d+)?)$/;
my ($scale) = $image->{'scale'} =~ m/^(\d+)x$/;
$image->{'filename'} = sprintf '%sx%s@%sx.png', $width, $height, $scale;
$filenames{$image->{'filename'}} = {'width' => $width * $scale, 'height' => $height * $scale};
}
foreach my $filename (sort keys %filenames)
{
my $properties = $filenames{$filename};
system "sips", "--resampleHeightWidth", $properties->{'width'}+5, $properties->{'height'}+5, "--cropToHeightWidth", $properties->{'width'}, $properties->{'height'}, "-s", "format", "png", "--out", $ARGV[0].'/'.$filename, $ARGV[1];
}
$content = $json->encode($data);
write_file($ARGV[0].'/Contents.json', $content);
print Dumper(\%filenames);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment