• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Exporting layer names but only visible layer

Explorer ,
May 09, 2023 May 09, 2023

Copy link to clipboard

Copied

Hello all. What is wrong with this script that it doesn't export visible layer names?

 

// Bring application forward
app.bringToFront();

// Set active Document variable and decode name for output
var docRef = app.activeDocument;
var docName = decodeURI(activeDocument.name);

// Define pixels as unit of measurement
var defaultRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;

// Define variable for the number of layers in the active document
var layerNum = app.activeDocument.artLayers.length;

// Define variable for the active layer in the active document
var layerRef = app.activeDocument.activeLayer;

// Define a variable for layer names
var layerNames = "";

function pad(num, size) {
  var s = num+"";
  while (s.length < size) s = "0" + s;
  return s;
}

// Loop to iterate through all layers
function recurseLayers(currLayers) {
  for ( var i = currLayers.layers.length - 1; i >= 0; i-- ) {
    layerRef = currLayers.layers[i];
	
	// test if layer is visible
	if (layerRef.visible) {
    var name = layerRef.name.replace(/_/g, " ");
    name = name.charAt(0).toUpperCase() + name.slice(1);
    layerNames += name + "\n";
	 }

    //test if it's a layer set
    if ( isLayerSet(currLayers.layers[i]) ) {
      recurseLayers(currLayers.layers[i]);
    }
  }
}

//a test for a layer set
function isLayerSet(layer) {
  try {
    if ( layer.layers.length > 0 ) {
      return true;
    }
  }

  catch(err) {
    return false;
  }
}

// Ask the user for the folder to export to
var FPath = Folder.selectDialog("Save exported layer names to");

// Detect line feed type
if ( $.os.search(/windows/i) !== -1 ) {
  fileLineFeed = "Windows";
}
else {
  fileLineFeed = "Macintosh";
}

// Export to txt files
function writeFiles(info, fileName) {
  try {
    var f = new File(FPath + "/" + fileName + ".txt");
    f.remove();
    f.open('a');
    f.lineFeed = fileLineFeed;
    f.write(info);
    f.close();
  }
  catch(e){}
}

// Run the functions
recurseLayers(docRef);
preferences.rulerUnits = defaultRulerUnits;

// Set preferences back to user's defaults
writeFiles(layerNames, "layername");

// Show results
if ( FPath == null ) {
  alert("Export aborted", "Canceled");
}
else {
  alert("Exported " + layerNum + " layer names to " + FPath + "/layername.txt " + "using " + fileLineFeed + " line feeds.", "Success!");
}

 Any help is appreciated.

 

Thanks 

TOPICS
Actions and scripting

Views

293

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Explorer , May 09, 2023 May 09, 2023

hmm the scripts work as intended when comment out the lines 9,10,85 which refers to preferences, units etc.

 

// Bring application forward
app.bringToFront();

// Set active Document variable and decode name for output
var docRef = app.activeDocument;
var docName = decodeURI(activeDocument.name);

// Define pixels as unit of measurement
//var defaultRulerUnits = preferences.rulerUnits;
//preferences.rulerUnits = Units.PIXELS;

// Define variable for the number of layers in the active document
var 
...

Votes

Translate

Translate
Explorer ,
May 09, 2023 May 09, 2023

Copy link to clipboard

Copied

LATEST

hmm the scripts work as intended when comment out the lines 9,10,85 which refers to preferences, units etc.

 

// Bring application forward
app.bringToFront();

// Set active Document variable and decode name for output
var docRef = app.activeDocument;
var docName = decodeURI(activeDocument.name);

// Define pixels as unit of measurement
//var defaultRulerUnits = preferences.rulerUnits;
//preferences.rulerUnits = Units.PIXELS;

// Define variable for the number of layers in the active document
var layerNum = app.activeDocument.artLayers.length;

// Define variable for the active layer in the active document
var layerRef = app.activeDocument.activeLayer;

// Define a variable for layer names
var layerNames = "";

function pad(num, size) {
  var s = num+"";
  while (s.length < size) s = "0" + s;
  return s;
}

// Loop to iterate through all layers
function recurseLayers(currLayers) {
  for ( var i = currLayers.layers.length - 1; i >= 0; i-- ) {
    layerRef = currLayers.layers[i];
	
	// test if layer is visible
	if (layerRef.visible) {
    var name = layerRef.name.replace(/_/g, " ");
    name = name.charAt(0).toUpperCase() + name.slice(1);
    layerNames += name + "\n";
	 }

    //test if it's a layer set
    if ( isLayerSet(currLayers.layers[i]) ) {
      recurseLayers(currLayers.layers[i]);
    }
  }
}

//a test for a layer set
function isLayerSet(layer) {
  try {
    if ( layer.layers.length > 0 ) {
      return true;
    }
  }

  catch(err) {
    return false;
  }
}

// Ask the user for the folder to export to
var FPath = Folder.selectDialog("Save exported layer names to");

// Detect line feed type
if ( $.os.search(/windows/i) !== -1 ) {
  fileLineFeed = "Windows";
}
else {
  fileLineFeed = "Macintosh";
}

// Export to txt files
function writeFiles(info, fileName) {
  try {
    var f = new File(FPath + "/" + fileName + ".txt");
    f.remove();
    f.open('a');
    f.lineFeed = fileLineFeed;
    f.write(info);
    f.close();
  }
  catch(e){}
}

// Run the functions
recurseLayers(docRef);
//preferences.rulerUnits = defaultRulerUnits;

// Set preferences back to user's defaults
writeFiles(layerNames, "layername");

// Show results
if ( FPath == null ) {
  alert("Export aborted", "Canceled");
}
else {
  alert("Exported " + layerNum + " layer names to " + FPath + "/layername.txt " + "using " + fileLineFeed + " line feeds.", "Success!");
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines