Skip to content

Instantly share code, notes, and snippets.

@Sakurina
Created August 3, 2010 04:36
Show Gist options
  • Save Sakurina/505838 to your computer and use it in GitHub Desktop.
Save Sakurina/505838 to your computer and use it in GitHub Desktop.
converts Icons/-style iPhone 4 themes to Bundles/-style using naming info from CSV
#!/usr/bin/env cycript
/* FileUtils */
function mv(old, _new) {
[[NSFileManager defaultManager] moveItemAtPath:old toPath:_new error:nil];
}
function mkdir(path) {
[[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
}
function fileExists(path) {
return [[NSFileManager defaultManager] fileExistsAtPath:path];
}
/* CSV Stuff */
function skcsv_foreach_row(filename, callback) {
if (!fileExists(filename)) {
system.print("Sorry, I can't seem to find your "+fileName+" file.");
return;
}
var fileContents = [NSString stringWithContentsOfFile:filename encoding:0 error:nil];
var lines = [fileContents componentsSeparatedByString:"\n"];
fileContents = nil;
var i = 0;
for each (line in lines) {
var range = [line rangeOfString:","];
if (i != 0) {
var row = [line componentsSeparatedByString:","];
callback(row);
}
i++;
}
}
/* Actual Theme-Related Stuff */
var IMPORTED_DATA = {};
function importData() {
skcsv_foreach_row("RetinaDisplayIcons.csv", function(row) {
IMPORTED_DATA[row[0]] = { bundleID: row[1], fileName: row[2] };
});
}
/* Main */
if (!fileExists("Icons-Retina")) {
system.print("Your current directory must have an Icons-Retina subdirectory for this to work.");
return;
}
importData();
var icons = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:"Icons-Retina" error:nil];
for each (icon in icons) {
if ([icon hasPrefix:"."])
continue;
var _icon = [icon stringByDeletingPathExtension];
if (IMPORTED_DATA[_icon] != undefined) {
var bID = IMPORTED_DATA[_icon].bundleID;
// Note: not sure why I can't concatenate strings together anymore; format doesn't work
// and + doesn't work either. I had to resort to this nasty stringByAppendingString: abuse. :(
var newFolder = ["Bundles/" stringByAppendingString:bID];
system.print([[_icon stringByAppendingString:" => "] stringByAppendingString:newFolder]);
mkdir(newFolder);
var oldPath = ["Icons-Retina/" stringByAppendingString:icon];
var newPath = [[newFolder stringByAppendingString:"/"] stringByAppendingString:IMPORTED_DATA[_icon].fileName];
mv(oldPath, newPath);
}
}
#!/usr/bin/env cycript
RIEWOSEL0 = new Selector("defaultManager");RIEWOSEL1 = new Selector("moveItemAtPath:toPath:error:");RIEWOSEL2 = new Selector("createDirectoryAtPath:withIntermediateDirectories:attributes:error:");RIEWOSEL3 = new Selector("fileExistsAtPath:");RIEWOSEL4 = new Selector("stringWithContentsOfFile:encoding:error:");RIEWOSEL5 = new Selector("componentsSeparatedByString:");RIEWOSEL6 = new Selector("rangeOfString:");RIEWOSEL7 = new Selector("contentsOfDirectoryAtPath:error:");RIEWOSEL8 = new Selector("hasPrefix:");RIEWOSEL9 = new Selector("stringByDeletingPathExtension");RIEWOSEL10 = new Selector("stringByAppendingString:");function mv(e,t){objc_msgSend(objc_msgSend(NSFileManager,RIEWOSEL0),RIEWOSEL1,e,t,nil)}function mkdir(e){objc_msgSend(objc_msgSend(NSFileManager,RIEWOSEL0),RIEWOSEL2,e,YES,nil,nil)}function fileExists(e){return objc_msgSend(objc_msgSend(NSFileManager,RIEWOSEL0),RIEWOSEL3,e)}function skcsv_foreach_row(e,t){var n,i,r,s,o;if(!fileExists(e)){system.print("Sorry, I can't seem to find your "+e+" file.");return}n=objc_msgSend(NSString,RIEWOSEL4,e,0,nil),i=objc_msgSend(n,RIEWOSEL5,"\n"),n=nil,r=0;with({$cys:i,$cyt:undefined})for($cyt in $cys){line=$cys[$cyt];{s=objc_msgSend(line,RIEWOSEL6,",");if(r!=0)o=objc_msgSend(line,RIEWOSEL5,","),t(o);r++}}}IMPORTED_DATA={};function importData(){skcsv_foreach_row("RetinaDisplayIcons.csv",function(e){IMPORTED_DATA[e[0]]={bundleID:e[1],fileName:e[2]}})}if(!fileExists("Icons-Retina")){system.print("Your current directory must have an Icons-Retina subdirectory for this to work.");return}importData(),icons=objc_msgSend(objc_msgSend(NSFileManager,RIEWOSEL0),RIEWOSEL7,"Icons-Retina",nil);with({$cys:icons,$cyt:undefined})for($cyt in $cys){icon=$cys[$cyt];{if(objc_msgSend(icon,RIEWOSEL8,"."))continue;_icon=objc_msgSend(icon,RIEWOSEL9);if(IMPORTED_DATA[_icon]!=undefined)bID=IMPORTED_DATA[_icon].bundleID,newFolder=objc_msgSend("Bundles/",RIEWOSEL10,bID),system.print(objc_msgSend(objc_msgSend(_icon,RIEWOSEL10," => "),RIEWOSEL10,newFolder)),mkdir(newFolder),oldPath=objc_msgSend("Icons-Retina/",RIEWOSEL10,icon),newPath=objc_msgSend(objc_msgSend(newFolder,RIEWOSEL10,"/"),RIEWOSEL10,IMPORTED_DATA[_icon].fileName),mv(oldPath,newPath)}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment