Skip to content

Instantly share code, notes, and snippets.

@a10k
Last active January 3, 2017 09:43
Show Gist options
  • Save a10k/ae2abd4bde3f03c9c7f90a2c1687bfc8 to your computer and use it in GitHub Desktop.
Save a10k/ae2abd4bde3f03c9c7f90a2c1687bfc8 to your computer and use it in GitHub Desktop.
DUP SVG Optimization script
my $file = @ARGV[0];
my $text = do {
local $/ = undef;
open my $fh, "<", $file
or die "could not open $file: $!";
<$fh>;
};
my $isLight = "\@import url('https://fonts.googleapis.com/css?family=Open+Sans:300');";
my $isLightItalic = "\@import url('https://fonts.googleapis.com/css?family=Open+Sans:300i');";
my $isNormal = "\@import url('https://fonts.googleapis.com/css?family=Open+Sans:400');";
my $isNormalItalic = "\@import url('https://fonts.googleapis.com/css?family=Open+Sans:400i');";
my $isSemibold = "\@import url('https://fonts.googleapis.com/css?family=Open+Sans:600');";
my $isSemiboldItalic = "\@import url('https://fonts.googleapis.com/css?family=Open+Sans:600i');";
my $isBold = "\@import url('https://fonts.googleapis.com/css?family=Open+Sans:700');";
my $isBoldItalic = "\@import url('https://fonts.googleapis.com/css?family=Open+Sans:700i');";
my $isExtrabold = "\@import url('https://fonts.googleapis.com/css?family=Open+Sans:800');";
my $isExtraboldItalic = "\@import url('https://fonts.googleapis.com/css?family=Open+Sans:800i');";
if(index($text, "font-family=\"'OpenSans-Light'\"") == -1){ $isLight = '';};
if(index($text, "font-family=\"'OpenSansLight-Italic'\"") == -1){ $isLightItalic = '';};
if(index($text, "font-family=\"'OpenSans'\"") == -1){ $isNormal = '';};
if(index($text, "font-family=\"'OpenSans-Italic'\"") == -1){ $isNormalItalic = '';};
if(index($text, "font-family=\"'OpenSans-Semibold'\"") == -1){ $isSemibold = '';};
if(index($text, "font-family=\"'OpenSansSemibold-Italic'\"") == -1){ $isSemiboldItalic = '';};
if(index($text, "font-family=\"'OpenSans-Bold'\"") == -1){ $isBold = '';};
if(index($text, "font-family=\"'OpenSansBold-Italic'\"") == -1){ $isBoldItalic = '';};
if(index($text, "font-family=\"'OpenSans-Extrabold'\"") == -1){ $isExtrabold = '';};
if(index($text, "font-family=\"'OpenSansExtrabold-Italic'\"") == -1){ $isExtraboldItalic = '';}else{print "FAIL";};
$stylesheet = "<style>$isLight $isLightItalic $isNormal $isNormalItalic $isSemibold $isSemiboldItalic $isBold $isBoldItalic $isExtrabold $isExtraboldItalic</style>";
$text =~ s/<\/svg>/$stylesheet <\/svg>/;
$text =~ s/font\-family="'OpenSans-Light'"/font\-family="'Open Sans'" font\-weight="300"/g;
$text =~ s/font\-family="'OpenSans'"/font\-family="'Open Sans'" font\-weight="400"/g;
$text =~ s/font\-family="'OpenSans\-Semibold'"/font\-family="'Open Sans'" font\-weight="600"/g;
$text =~ s/font\-family="'OpenSans\-Bold'"/font\-family="'Open Sans'" font\-weight="700"/g;
$text =~ s/font\-family="'OpenSans\-Extrabold'"/font\-family="'Open Sans'" font\-weight="800"/g;
$text =~ s/font\-family="'OpenSansLight\-Italic'"/font\-family="'Open Sans'" font\-weight="300" font\-style="italic"/g;
$text =~ s/font\-family="'OpenSans\-Italic'"/font\-family="'Open Sans'" font\-weight="400" font\-style="italic"/g;
$text =~ s/font\-family="'OpenSansSemibold\-Italic'"/font\-family="'Open Sans'" font\-weight="600" font\-style="italic"/g;
$text =~ s/font\-family="'OpenSansBold\-Italic'"/font\-family="'Open Sans'" font\-weight="700" font\-style="italic"/g;
$text =~ s/font\-family="'OpenSansExtrabold\-Italic'"/font\-family="'Open Sans'" font\-weight="800" font\-style="italic"/g;
open FH, '>', $file or die "Error:$!\n";
print FH $text;
on adding folder items to this_folder after receiving these_items
repeat with i from 1 to number of items in these_items
set this_item to item i of these_items
set the file_path to the POSIX path of this_item
try
do shell script "/bin/bash -l -c 'perl ~/Library/Scripts/DUP/DUP-svg-transform.pl \"" & file_path & "\"'"
on error errStr number errorNumber
display dialog "Error: " & errStr buttons {"OK"} with icon stop
return number
end try
end repeat
end adding folder items to
#!/bin/sh
SCRIPTS_PATH="$HOME/Library/Scripts/Folder Action Scripts"
DUP_SCRIPTS_PATH="$HOME/Library/Scripts/DUP"
mkdir -p "$SCRIPTS_PATH"
mkdir -p "$DUP_SCRIPTS_PATH"
curl -sSO https://gist.githubusercontent.com/a10k/ae2abd4bde3f03c9c7f90a2c1687bfc8/raw/DUP-svg-transform.pl
curl -sSO https://gist.githubusercontent.com/a10k/ae2abd4bde3f03c9c7f90a2c1687bfc8/raw/DUP-svg.applescript
osacompile -o "$SCRIPTS_PATH/DUP-SVG.scpt" DUP-svg.applescript
cp ./DUP-svg-transform.pl "$DUP_SCRIPTS_PATH/DUP-svg-transform.pl"
rm DUP-svg-transform.pl
rm DUP-svg.applescript
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment