Reputation: 13
So what I want to do is move each country layer according to the coordinate list.
Using Javascript I have been able to load the text file, but I am at a loss how to pass the coordinates to each layer and actually move the layers into place. I cant seem to add a picture here or even a link - just get an error message. Trying a hyper link: [Link to image][1] Sergey Kritskiy , Ghoul Fool - Thank you for all your patience and help with this.
This is what I have for "Code" as of now - lots missing..
main();
var originalUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
function main(){
//declare listFile variable, prompt to browse for text file
var listFile = File.openDialog("Open list of coordinates","text(*.txt):*.txt;");
//if no file selected, return
if(listFile == null) return;
//read the selected text file (listFile) to a string(listString) - read coordinates from text File
listFile.open('r') ;
var listString = listFile.read();
listFile.close();
//splitting at line breaks, convert into array of strings
fileList = listString.split(' ');
//Need to tell what is X-value and what is Y-value
// call the source document
var srcDoc = app.activeDocument;
var numOfLayers = srcDoc.layers.length;
// main loop
for (var i = numOfLayers -1; i >= 0 ; i--)
{
//select that layer as you go along
srcDoc.activeLayer = srcDoc.artLayers[i];
// Here is where each layer should be paired to its coordinate from the text file - But how to do it?
// move the layer
moveLayer(moveX , moveY);
}
}
//iterate array of strings (coordinates), move layers acording to coordinates in the loaded text document
//for(var i=0; i<=fileList.length; i++){
// var theFile =new File(fileList[i]);
}
};```
[1]: https://www.mediafire.com/view/oms4kv7kt6l6zsf/coordinates_list_and_layers.JPG/file
Upvotes: 1
Views: 532
Reputation: 6937
Basically you want a function to translate a layer (from its original position to its intended position)
You'll also need to work in pixels, which'll make life easier.
You'll either have an object on an array of countries with their respective offsets.
Loop over all the layers. See if the layer name matches a country. It does? Then move that layer.
This'll give you a rough idea, but only works with maps from 1883 where Lichtenstein was the only known country in the world, which naturally made mapping easier.
var originalUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
// call the source document
var srcDoc = app.activeDocument;
var numOfLayers = srcDoc.layers.length;
// main loop
for (var i = numOfLayers -1; i >= 0 ; i--)
{
//select that layer as you go along
srcDoc.activeLayer = srcDoc.artLayers[i];
if (srcDoc.artLayers[i].name == "Lichtenstein")
{
var moveX = 100;
var moveY = 40;
// move the layer
moveLayer(moveX , moveY);
}
}
// Put unit preferences back
app.preferences.rulerUnits = originalUnits;
// FUNCTION moveLayer (dx, dy) translates layer by dx, dy
function moveLayer(dx, dy)
{
// =======================================================
var id9297 = charIDToTypeID( "move" );
var desc2040 = new ActionDescriptor();
var id9298 = charIDToTypeID( "null" );
var ref1412 = new ActionReference();
var id9299 = charIDToTypeID( "Lyr " );
var id9300 = charIDToTypeID( "Ordn" );
var id9301 = charIDToTypeID( "Trgt" );
ref1412.putEnumerated( id9299, id9300, id9301 );
desc2040.putReference( id9298, ref1412 );
var id9302 = charIDToTypeID( "T " );
var desc2041 = new ActionDescriptor();
var id9303 = charIDToTypeID( "Hrzn" );
var id9304 = charIDToTypeID( "#Pxl" );
desc2041.putUnitDouble( id9303, id9304, dx );
var id9305 = charIDToTypeID( "Vrtc" );
var id9306 = charIDToTypeID( "#Pxl" );
desc2041.putUnitDouble( id9305, id9306, dy );
var id9307 = charIDToTypeID( "Ofst" );
desc2040.putObject( id9302, id9307, desc2041 );
executeAction( id9297, desc2040, DialogModes.NO );
}
So putting it all together you want something like this:
// Switch off any dialog boxes
displayDialogs = DialogModes.ERROR; // OFF
var originalUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
main();
function main()
{
//declare listFile variable, prompt to browse for text file
var listFile = File.openDialog("Open list of coordinates","text(*.txt):*.txt;");
//if no file selected, return
if(listFile == null) return;
//read the selected text file (listFile) to a string(listString) - read coordinates from text File
listFile.open('r') ;
var listString = listFile.read();
listFile.close();
//splitting at line breaks, convert into array of strings
fileList = listString.split("\n");
//Need to tell what is X-value and what is Y-value
// call the source document
var srcDoc = app.activeDocument;
var numOfLayers = srcDoc.layers.length;
// main loop
var c = 0;
// ignore the background? Start at numOfLayers -2
for (var i = numOfLayers -2; i >= 0 ; i--)
{
//select that layer as you go along
srcDoc.activeLayer = srcDoc.artLayers[i];
var theLayerName = srcDoc.artLayers[i].name;
var line = fileList[c].split(",");
var moveX = line[0];
var moveY = line[1];
// alert(theLayerName + " : " + moveX + ", " + moveY);
// Here is where each layer should be paired to its coordinate from the text file - But how to do it?
// move the layer
moveLayer(moveX , moveY);
// increment the file list counter
c+=1;
}
}
// set ruler units back to how it was
app.preferences.rulerUnits = originalUnits;
// Set Display Dialogs back to normal
displayDialogs = DialogModes.ALL; // NORMAL
// FUNCTION moveLayer (dx, dy) translates layer by dx, dy
function moveLayer(dx, dy)
{
// =======================================================
var id9297 = charIDToTypeID( "move" );
var desc2040 = new ActionDescriptor();
var id9298 = charIDToTypeID( "null" );
var ref1412 = new ActionReference();
var id9299 = charIDToTypeID( "Lyr " );
var id9300 = charIDToTypeID( "Ordn" );
var id9301 = charIDToTypeID( "Trgt" );
ref1412.putEnumerated( id9299, id9300, id9301 );
desc2040.putReference( id9298, ref1412 );
var id9302 = charIDToTypeID( "T " );
var desc2041 = new ActionDescriptor();
var id9303 = charIDToTypeID( "Hrzn" );
var id9304 = charIDToTypeID( "#Pxl" );
desc2041.putUnitDouble( id9303, id9304, dx );
var id9305 = charIDToTypeID( "Vrtc" );
var id9306 = charIDToTypeID( "#Pxl" );
desc2041.putUnitDouble( id9305, id9306, dy );
var id9307 = charIDToTypeID( "Ofst" );
desc2040.putObject( id9302, id9307, desc2041 );
executeAction( id9297, desc2040, DialogModes.NO );
}
Upvotes: 0