Misc Scripting

From bernie's
Jump to navigation Jump to search

Illustrator Angle Between

doc = app.activeDocument;
sel = doc.selection;
selLen = sel.length;

// walk through each selected item
for (var s=0;s<selLen ;s++ )
	{
		if (sel[s].typename == "PathItem"){
			pnts = sel[s].pathPoints; 
			siz = pnts.length;
			list ="";
				for(a=0;a<siz;a++){
						previous = (a==0)?pnts[siz-2]:pnts[a-1];
						current = pnts[a];
						next = (a==(siz-1))?pnts[0]:pnts[a+1];
						previous = previous.anchor;
						current = current.anchor;
						next = next.anchor;
						vec1 = [previous[0]-current[0],previous[1]-current[1]];
						vec2 = [next[0]-current[0],next[1]-current[1]];
						angle = angleBetween(vec1,vec2);
						if(angle>10){
							alert(angle);
							var myCircle = new Path.Circle(new Point(current[0], current[1]), 20);
							}
					}

		}
	}
	
	
	
function angleBetween(v1,v2){
	//divide by zero if v1 or v2 is = 0
	scalar = v1[0]*v2[0]+v1[1]*v2[1];
	v1len = Math.sqrt(v1[0]*v1[0]+v1[1]*v1[1]);
	v2len = Math.sqrt(v2[0]*v2[0]+v2[1]*v2[1]);
	angleInRadians =Math.acos(scalar/(v1len*v2len));
	angleInDegree = angleInRadians*180/Math.PI;
	return angleInRadians;
	}

valette

  • ecrit ce texte dans un fichier .bat (windows) tel quel, il va creer une page html quand tu le lancera, tu peut l'appeler comme tu veux genre lol.bat, mais il doit etre dans le mm repertoire que les images
  • modifie le filter (la il prend les fichiers type test.56111.png, testfull.111.png) et le width (ca c'est pour regler la largeur des images, tu peut utiliser shift-left arrow / shift-right arrow pour les modifier dans la page
  • la page se lance toute seule, mais des que tu rajoutera des images (si les noms changent) il faudra relancer le bat (qui ecrasera le fichier html)
@echo off

set filter="*.jpg"
set out_file="out.html"
set percentwidth=90

echo ^<html^>^<head^>^<style^>#cnt{width: %percentwidth%%%}^</style^>^<script^> > %out_file%
echo|set /p=var imgs=[ >> %out_file%
for %%f in (%filter%) do echo|set /p=^'%%f^', >> %out_file%
echo|set /p='end.jpg']; >> %out_file%
echo. >> %out_file%
echo widthMod =  %percentwidth%; >> %out_file%
for /F "skip=15 delims=" %%i in (%~0) do echo %%i >> %out_file%
%out_file%
GOTO :EOF





min = 9999;    
max = 0;    
for(i=0;i<=imgs.length-2;i++){    
	ln = imgs[i].length;    
	min = Math.min(min, ln);    
	max = Math.max(max, ln);    
}    
var pages=[];    
pages.push([]); 
token = 0;    
imagesToken = 0;    
for(i=0;i<=imgs.length-2;i++){
//console.log(imagesToken+' '+imgs[i])    
	if(imgs[i].length==min){    
		if(imagesToken==4){    
			pages.push([]);    
			token++;    
			imagesToken = 0;
		}
		pages[token].push(imgs[i]);    
		imagesToken++;    
	}else{    
		imagesToken = 0;
		if(pages[token].length != 0){
			pages.push([]);    
			token++; 
		}
		pages[token].push(imgs[i]);    
		pages.push([]);    
		token++;    
	}    
}
//console.log(JSON.stringify(pages));
document.onkeydown = function(evt) {   
    evt = evt || window.event;   
    if (evt.keyCode  == 37) {   
    	if(evt.shiftKey){ 
    		widthMod -= 5; 
			document.getElementById("cnt").style.width = widthMod+"%"; 
    	}else{ 
			incdec(-1);  
		}   
    }else if(evt.keyCode  == 39){ 
    	if(evt.shiftKey){ 
    		widthMod += 5; 
			document.getElementById("cnt").style.width = widthMod+"%"; 
    	}else{ 
			incdec(1);  
		}  
	}   
};   
function changepage(){   
	refreshImage();   
}   
function incdec(v){   
	var curValue = parseFloat(document.getElementById("textbox").value);   
	if(curValue == 1 && v == -1){   
		v = 0;   
	}   
	document.getElementById("textbox").value = curValue + v;   
	refreshImage();   
}  
function refreshImage(){ 
	page = parseFloat(document.getElementById("textbox").value-1); 
	singleIm = document.getElementById("imgsingle"); 
	multiTbl = document.getElementById("tableuh4"); 
	if(pages[page].length == 1){ 
		multiTbl.style.display = "none"; 
		im = singleIm; 
		im.style.display = "block"; 
		im.src = pages[page][0] ; 
		im.title = pages[page][0] ; 
	}else{ 
		multiTbl.style.display = "block"; 
		singleIm.style.display = "none"; 
		for(i=1;i<=4;i++){ 
			im = document.getElementById("img"+i); 
			if(pages[page][i-1] != undefined){ 
				im.style.visibility = 'visible'; 
				im.src = pages[page][i-1] ; 
				im.title = pages[page][i-1]; 
			}else{ 
				im.style.visibility = 'hidden'; 
			} 
		} 
	} 
} 
window.onload = function(){refreshImage()}; 
</script> 
<style> 
.tbl{ 
	width: 100%; 
	display: block; 
	table-layout: fixed; 
} 
td{ 
	padding:10px; 
	text-align: center; 
} 
img{ 
	width:100%; 
} 
#cnt{ 
	margin: auto; 
} 
</style>    
</head>   
<body>   
<div style="text-align: center;">   
	<input type="text" id="textbox" name="go" value="1" onkeyUp="changepage()">   
	<button type="button" onclick="incdec(-1)"><<</button>   
	<button type="button" onclick="incdec(1)">>></button>   
	<div id="cnt"> 
		<table id="tableuh4" class="tbl" align="center" border="0"> 
			<tr> 
				<td><img id="img1" src="none.gif"></td> 
				<td><img id="img2" src="none.gif"></td> 
			</tr> 
			<tr> 
				<td><img id="img3" src="none.gif"></td> 
				<td><img id="img4" src="none.gif"></td> 
			</tr> 
		</table> 
	<div style="text-align: center; padding:10px">   
		<img id="imgsingle" src="none.gif" style="padding:0px"> 
	</div>			 
	</div>   
</div> 
</body>   
</html>

valette toshop avec pages fullscreen

  • enregistrer en .jsx dans le dossier ou ya les images
  • a besoin d'un fichier qui s'appel template.psd (aussi dans le meme dossier) avec les calques 4 x 4 ou autre config positionnés et dans le bon ordre dans le chutier de calques
  • cherche aussi un calque qui a le mot "full" dedans

en gros ca doit etre comme ca:

jOqRTXK.png


var debug = false;

function e(s) {
    $.writeln(s);
}
function r(r){
    output = "";
    for(i=0;i<=r.length-1;i++){
        for(j=0;j<=r[i].length-1;j++){
            output += r[i][j]+"\n";
        }
    output += "\n";
    }
    return output;
}

function pad(width, string, padding) {
    return (width <= string.length) ? string : pad(width, padding + string, padding)
}


function repositionLayer(incomingLayer,replacedLayerBounds) {

    //resize and position a layer according to [xmax,ymax,xmin,ymin] which you get with layername.bounds() function

    newBounds = incomingLayer.bounds;
    curBounds = replacedLayerBounds;

    // if the template's layers aren't the same size as the incoming images, resize new images, and move them to where they should be

    if (((curBounds[2] - curBounds[0]) != (newBounds[2] - newBounds[0])) && ((curBounds[3] - curBounds[1]) != (newBounds[3] - newBounds[1]))) {

        var newWidth = (curBounds[2] - curBounds[0]) / (newBounds[2] - newBounds[0]);
        var newHeight = (curBounds[3] - curBounds[1]) / (newBounds[3] - newBounds[1]);
        incomingLayer.resize(newWidth * 100, newHeight * 100);

    }

    var oldCenter = [curBounds[0], curBounds[1]];
    var newCenter = [incomingLayer.bounds[0], incomingLayer.bounds[1]];
    incomingLayer.translate(oldCenter[0] - newCenter[0], oldCenter[1] - newCenter[1]); 
}



var units = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var script = new File($.fileName);
var template = new File(script.path + "/template.psd");

if (!template.exists) {
    alert("template not found with script: '" + script.path + "/template.psd'\n\nhurr durr");

} else {
    var filter = prompt("Files filter","*.jpg");
    if(filter != null){

        var docRefOrigin = app.open(template);
        

        //figure out how many elements per page, grab their bounds, and check if there's a layer for full page, get its bounds

            var layers = docRefOrigin.artLayers;
            var numItems = 0;
            var page = 0;


            var exisitingbounds = [];
            var bigbounds = [];

            var firstLayerPosition = -1;

            for (i = layers.length-1; i>=0; i--) {

                if (layers[i].kind != LayerKind.TEXT && !layers[i].isBackgroundLayer && !layers[i].allLocked) {
                    
                    firstLayerPosition = i;

                    if(layers[i].name.toLowerCase().indexOf("full") != -1){
                        bigbounds = layers[i].bounds;
                    }else{
                        
                        numItems++;
                        exisitingbounds.push(layers[i].bounds);
                    }
                layers[i].remove();
                }
            }
            
            if(bigbounds.length == 0){
                alert("No layer found with 'full' in its name found");
            }

            exisitingbounds.reverse(); 

        //figure out pages pattern accordin to their name length for each page

            var imagesFolder = new Folder(script.path);
            var files = imagesFolder.getFiles(filter);
            files.sort();

            var min = 9999;    
            var max = 0;
            var imgs = files;
            for(i=0;i<=imgs.length-1;i++){    
                ln = imgs[i].name.length;
                min = Math.min(min, ln);    
                max = Math.max(max, ln);    
            }
            var pages=[];    
            pages.push([]); 
            var token = 0;    
            var imagesToken = 0;    
            for(i=0;i<=imgs.length-1;i++){
                if(imgs[i].name.length==min){    
                    if(imagesToken==numItems){    
                        pages.push([]);    
                        token++;    
                        imagesToken = 0;
                    }
                    pages[token].push(imgs[i]);    
                    imagesToken++;    
                }else{    
                    imagesToken = 0;
                    if(pages[token].length != 0){
                        pages.push([]);    
                        token++; 
                    }
                    pages[token].push(imgs[i]);    
                    pages.push([]);    
                    token++;    
                }    
            }


        // process array of files, for each element of pages (either a size n*4 where n!=1 bounds array or size 1*4 bounds array)
        tmp = "";
        for(i=0;i<pages.length;i++){

            
            docRef = docRefOrigin.duplicate();
            app.activeDocument = docRef;
            layers = docRef.artLayers;

            //for each layer, check if it's text and if is, add page number (only on the first text layer though)

                for (j = 0; j < layers.length; j++) {
                    var curLay = layers[j];
                    if (curLay.kind == LayerKind.TEXT && !curLay.allLocked) {
                        curLay.textItem.contents += " " + (i+1);
                    }
                }

            // go through subitems in pages: open, copy, close, paste

                for (j = 0; j < pages[i].length; j++) {
                    var curImage = pages[i][j];
                        if (curImage != undefined) {
                            var img = new File();
                            var imgRef = app.open(curImage);
                            app.activeDocument = imgRef;
                            app.activeDocument.flatten()
                            app.activeDocument.selection.selectAll()
                            app.activeDocument.selection.copy()
                            app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                            app.activeDocument = docRef;
                            var newLayer = app.activeDocument.paste();
                            newLayer.move(docRef.layers[firstLayerPosition+j], ElementPlacement.PLACEAFTER);
                            curBounds = (pages[i].length == 1)?bigbounds:exisitingbounds[j];
                            repositionLayer(newLayer,curBounds);
                        }
                    
                }
                
            // save 2 versions and close

                if(!debug){
                    
                    //jpg
                    saveFolder = new Folder(imagesFolder + "/output");
                    saveFolder.create();
                    saveFile = new File(imagesFolder + "/output/page." + pad(4, i+1, "0") + ".jpg");
                    var saveOptions = new JPEGSaveOptions();
                    saveOptions.embedColorProfile = true;
                    saveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
                    saveOptions.quality = 12;
                    docRef.saveAs(saveFile, saveOptions, true, Extension.LOWERCASE);

                    //psd
                    saveFile = new File(imagesFolder + "/output/psd_page." + pad(4, i+1, "0") + ".psd");
                    saveOptions = new PhotoshopSaveOptions();
                    docRef.saveAs(saveFile, saveOptions, true, Extension.LOWERCASE);

                    
                }
                docRef.close(SaveOptions.DONOTSAVECHANGES);
        }
        docRefOrigin.close(SaveOptions.DONOTSAVECHANGES);
    }
}
app.preferences.rulerUnits = units;

valette toshop

function e(s) {
    $.writeln(s);
}

function pad(width, string, padding) {
    return (width <= string.length) ? string : pad(width, padding + string, padding)
}
var units = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var script = new File($.fileName);
var template = new File(script.path + "/template.psd");
if (!template.exists) {
    alert("template not found with script: '" + script.path + "/template.psd'\n\nhurr durr");
} else {
    var docRefOrigin = app.open(template);
    var imagesFolder = new Folder(script.path);
    var files = imagesFolder.getFiles("*.jpg");
    files.sort();
    /*for(i=0;i<=files.length;i++){
        e(files[i]);
    }*/
    var layers = docRefOrigin.artLayers;
    var numItems = 0;
    var page = 0;

    //figure out how many elements per page
    for (i = 0; i < layers.length; i++) {

        if (layers[i].kind != LayerKind.TEXT && !layers[i].isBackgroundLayer && !layers[i].allLocked) {
            numItems++;
        }
    }

    var pages = files.length / numItems;
    //e(numItems + " > " + files.length + " = " + pages);
    //pages = 3;
    for (j = 0; j <= pages; j++) {
        docRef = docRefOrigin.duplicate();
        app.activeDocument = docRef;
        layers = docRef.artLayers;
        //for each layer, check if it's text and if is, add page number (only on the first text layer though

        for (i = 0; i < layers.length; i++) {
            var curLay = layers[i];
            if (curLay.kind == LayerKind.TEXT && !curLay.allLocked) {
                curLay.textItem.contents += " " + (j + 1);

                //if it's not a text layer (and not last layer), grab the corresponding image out the file list, flatten, copy it, paste it
                //in the template, resize it to the template layer size, delete template layer, move on

            } else if (curLay.kind != LayerKind.TEXT && !curLay.allLocked && !curLay.isBackgroundLayer) {
                curBounds = curLay.bounds;

                imgPath = files[j * numItems + i - (layers.length - numItems - 1)];

                e(imgPath);
                if (imgPath != undefined) {
                    var img = new File();
                    var imgRef = app.open(imgPath);

                    app.activeDocument = imgRef;
                    app.activeDocument.flatten()
                    app.activeDocument.selection.selectAll()
                    app.activeDocument.selection.copy()
                    app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                    app.activeDocument = docRef;
                    var newLayer = app.activeDocument.paste();
                    newLayer.move(curLay, ElementPlacement.PLACEBEFORE);
                    newBounds = newLayer.bounds;
                    curLay.remove();

                    // if the template's layers aren't the same size as the incoming images, resize new images, and move them to where they should be
                    if (((curBounds[2] - curBounds[0]) != (newBounds[2] - newBounds[0])) && ((curBounds[3] - curBounds[1]) != (newBounds[3] - newBounds[1]))) {

                        var newWidth = (curBounds[2] - curBounds[0]) / (newBounds[2] - newBounds[0]);
                        var newHeight = (curBounds[3] - curBounds[1]) / (newBounds[3] - newBounds[1]);
                        newLayer.resize(newWidth * 100, newHeight * 100);

                    }

                    var oldCenter = [curBounds[0], curBounds[1]];
                    var newCenter = [newLayer.bounds[0], newLayer.bounds[1]];
                    newLayer.translate(oldCenter[0] - newCenter[0], oldCenter[1] - newCenter[1]);
                }else{
                    curLay.visible = false;
                }
            }
        }
        //save documents as PSD and JPG
        saveFile = new File(imagesFolder + "/output" + pad(4, j, "0") + ".jpg");
        var saveOptions = new JPEGSaveOptions();
        saveOptions.embedColorProfile = true;
        saveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
        //saveOptions.matte = MatteType.NONE; 
        saveOptions.quality = 12;
        docRef.saveAs(saveFile, saveOptions, true, Extension.LOWERCASE);

        saveFile = new File(imagesFolder + "/output" + pad(4, j, "0") + ".psd");
        saveOptions = new PhotoshopSaveOptions();
        docRef.saveAs(saveFile, saveOptions, true, Extension.LOWERCASE);

        docRef.close(SaveOptions.DONOTSAVECHANGES);
    }
    docRefOrigin.close(SaveOptions.DONOTSAVECHANGES);
}
app.preferences.rulerUnits = units;