#target illustrator
function getCoords(offset,centreSort,isXML,saveToFile){
//alert('offset: ' + offset+'\tcentreSort: '+centreSort+'\tisXML: '+isXML+'\t saveToFile: '+saveToFile);
var doc = app.activeDocument;
var hasDocCoords = app.coordinateSystem == CoordinateSystem.DOCUMENTCOORDINATESYSTEM;
var sel = doc.selection;
var selLen = sel.length;
var xml = ;
var code = 'var selection:Vector. = Vector.([';
var points = [];
if(selLen >= 1){
for(var i = 0 ; i < selLen ; i++){
var pos = hasDocCoords ? doc.convertCoordinate (sel[i].position, CoordinateSystem.DOCUMENTCOORDINATESYSTEM, CoordinateSystem.ARTBOARDCOORDINATESYSTEM) : sel[i].position;
var x = parseFloat((pos[0] + (sel[i].width * .5)).toFixed(2));
var y = parseFloat(Math.abs((pos[1] - (sel[i].height*.5))).toFixed(2));
points[i] = {x:x,y:y,distance:0,name:sel[i].name,toString : function(){ return 'name:' +this.name+' distance: ' + this.distance + '\n'}};
}
}
var xs = [].concat(points).sort(function (a, b) { return (a.x- b.x); });
var ys = [].concat(points).sort(function (a, b) { return (a.y- b.y); });
var centre = [xs[0].x + ((xs[selLen-1].x - xs[0].x) * .5),ys[0].y + ((ys[selLen-1].y - ys[0].y)* .5)];
xml.appendChild();
if(centreSort) {
for(var i = 0 ; i < selLen ; i++) points[i].distance = distance(points[i].x,points[i].y,centre[0],centre[1]);
points.sort(function (a, b) { return (a.distance - b.distance); });
}
if(offset){
for(var i = 0 ; i < selLen ; i++) {
points[i].x -= xs[0].x;
points[i].y -= ys[0].y;
}
}
for(var i = 0 ; i < selLen ; i++) {
code += 'new Point('+points[i].x + ' , ' + points[i].y + (i < selLen-1 ? '),' : ')]);selection.fixed=true;');
points[i].name.length > 0 ? xml.appendChild() : xml.appendChild();
}
if(isXML) code = xml.toXMLString();
//code
code += '\n';
if(saveToFile){
var file = File.saveDialog('Save Coordinates to File',"XML Files: *.xml;AS Files: *.as");
if(file){
file.open('w');
file.write(code);
file.close();
}
}else copyToClipboard(code,doc);
}
var dialog = makeDialog();
dialog.save.onClick = function(){
//alert(dialog.o.value+'\t'+dialog.xml.value+'\t'+dialog.as3.value+'\t'+dialog.c.text);
//getCoords(offset,centreSort,isXML,saveToFile){
getCoords(dialog.offset.value,dialog.centre.value,dialog.xml.value,dialog.file.value);
dialog.close();
}
dialog.show();
function makeDialog(){
var dialog = new Window('dialog','Save Selection Coordinates');
dialog.frameLocation = [300,300];
dialog.offset = dialog.add('checkbox',undefined,' offset to top left');
dialog.offset.prefferedSize = [200,50];
dialog.xml = dialog.add('radiobutton',undefined,' copy as XML');
dialog.xml.prefferedSize = [200,50];
dialog.xml.value = true;
dialog.as3 = dialog.add('radiobutton',undefined,' copy as AS3');
dialog.as3.prefferedSize = [200,50];
//dialog.data = dialog.add('edittext',undefined,' User Data');
//dialog.data.prefferedSize = [200,50];
dialog.centre = dialog.add('checkbox',undefined,' sort from centre');
dialog.centre.prefferedSize = [200,50];
dialog.clipboard = dialog.add('radiobutton',undefined,' save to clipboard');
dialog.clipboard.prefferedSize = [200,50];
dialog.clipboard.value = true;
dialog.file = dialog.add('radiobutton',undefined,' save to file');
dialog.file.prefferedSize = [200,50];
dialog.save = dialog.add('button',undefined,'Save');
return dialog;
}
//clipboard hack - add text in the document then cut it
function copyToClipboard (text, doc){
doc.layers.add().textFrames.add().contents = text;
doc.layers[0].hasSelectedArtwork = true;
app.copy();
doc.layers[0].remove();
}
function distance(x1,y1,x2,y2){
var dx = (x2-x1);
var dy = (y2-y1);
return Math.sqrt(dx*dx + dy*dy);
}
//var sh = app.system("osascript -e 'set the clipboard to \"" + code+ "\"'");//app.system is for Bridge, not present in Illustrator :(
//if(!sh) app.beep();
function inspect(o){
var result = '';
for(var i in o) result += i+':'+o[i] + '\n';
return result;
}