Skip to content

Instantly share code, notes, and snippets.

@gregschoen
Created January 9, 2016 06:40
Show Gist options
  • Save gregschoen/136906719adab44ea6a0 to your computer and use it in GitHub Desktop.
Save gregschoen/136906719adab44ea6a0 to your computer and use it in GitHub Desktop.
Quick and dirty script to keep my Screenshot folder clean and properly named.
#!/usr/bin/php
<?php
date_default_timezone_set("America/Chicago");
$who = trim(`whoami`);
$base = "/Users/{$who}/Dropbox/Screenshots/";
$files = glob("{$base}*.png");
foreach($files as $file)
{
$original = $file;
if(preg_match("/Screenshot (.*)\.png/",$file,$match))
{
$dt = str_replace(" at "," ",$match[1]);
if(strpos($dt," (2)"))
{
$dt = str_replace(" (2)","",$dt);
$timestamp = strtotime($dt) + 1;
}
else
$timestamp = strtotime($dt);
$datetime = date("Y-m-d H.i.s",$timestamp);
if($datetime)
{
// crushing PNG with PNGquant
exec("pngquant " . escapeshellarg($original) . " --output " . escapeshellarg($base.$datetime.".png"), $output, $code);
if($code == 0)
unlink($original);
else
echo "Something went wrong! Error Code: $code\n";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment