function Qwertyfly_name_to_text(){
if( app.documents.length = 0){return;}
//USER SET VARIABLES
// offset per pass
var increment = 5;
// final area
var finalArea = 200;
//
var doc = app.activeDocument;
var sel = doc.selection;
var textLayer = doc.layers.add();
textLayer.name = "State Name Tags";
function duplicate_for_processing(sel){
// create copies of all selected items in new layer
var workingLayer = doc.layers.add();
workingLayer.name = "Working Layer";
for(var i=0; i<sel.length; i++){
sel[i].duplicate(workingLayer, ElementPlacement.PLACEATEND);
}
return workingLayer;
}
function add_text_frame(textLocator, textContents){
var txt= textLayer.textFrames.add();
txt.contents = textContents;
txt.position = [textLocator[0] - (txt.width/2),textLocator[1] + (txt.height/2)];
}
function offset_item_via_action(Offset){
var ActionString = [
'/version 3',
'/name [ 16',
'53637269707465645f416374696f6e73',
']',
'/isOpen 1',
'/actionCount 1',
'/action-1 {',
'/name [ 13',
'4f66667365745f416374696f6e',
']',
'/keylndex 0',
'/colorlndex 0',
'/isOpen 1',
'/colorlndex 0',
'/isOpen 1',
'/eventCount 1',
'/event-1 {',
'/useRulersIn1stQuadrant 0',
'/internalName (ai_plugin_offset)',
'/localizedName [ 11',
'4f66667365742050617468',
']',
'/isOpen 1',
'/isOn 1',
'/hasDialog 1',
'/showDialog 0',
'/parameterCount 3',
'/parameter-1 {',
'/key 1868985204',
'/showinPalette -1',
'/type (unit real)',
'/value ' + Offset + '',
'/unit 592476268',
'}',
'/parameter-2 {',
'/key 1835627634',
'/showlnPalette -1',
'/type (real)',
'/value 4.0',
'}',
'/parameter-3 {',
'/key 1785623664',
'/showInPalette -1',
'/type (enumerated)',
'/name [ 5',
'4d69746572',
']',
'/value 2',
'}',
'}',
'}'
].join('\n');
createAction(ActionString);
var ActionString = null;
app.doScript("Offset_Action", "Scripted_Actions", false);
app.unloadAction("Scripted_Actions", "");
function createAction(str){
var f = new File('~/ScriptAction.aia');
f.open('w');
f.write(str);
f.close();
app.loadAction(f);
f.remove();
}
}
//main code
var WL = duplicate_for_processing (sel);
var len = WL.pathItems.length;
for(var i=0;i<len; i++){
doc.selection = null
var offset = -2;
var item = WL.pathItems[i];
var gotIt = false;
while(gotIt == false){
item.selected = true;
offset_item_via_action (offset + ".0");
if(doc.selection[0].typename == "CompoundPathItem"){
doc.selection[0].remove();
offset = offset - increment;
}else if(doc.selection[0].area<finalArea){
gotIt = true;
}else{
doc.selection[0].remove();
offset = offset - increment;
}
}
//get the center of this shape
var location = [doc.selection[0].left+(doc.selection[0].width/2),doc.selection[0].top-(doc.selection[0].height/2)];
// now remove the working items
doc.selection[0].remove();
var txt = item.name;
//place new text centered to point found above
add_text_frame(location, txt)
}
// remove working layer
for(var i=WL.pathItems.length-1; i>0; i--){
WL.pathItems[i].remove();
}
WL.remove();
}
Qwertyfly_name_to_text();