Skip to main content
2fingertyping
Known Participant
May 18, 2016
Answered

Exporting Visible layers in photoshop using script.

  • May 18, 2016
  • 3 replies
  • 718 views

Hi,

I am new to adobe scripting, so please forgive me if this has been covered before.

I have a script from Chandler McWilliam's Adobe Scripting book, which I would like to use. Essentially it exports all the visible layers in an open PS document. The script works, but I cannot get it to export to a folder/location of my choosing. It defaults to exporting to the folder which contains the Photoshop application.

I am using a Mac.

The Script looks like this:

function exportVisible(doc) {

  var myJPEG = new JPEGSaveOptions();

  myJPEG.quality = 10;

  myJPEG.formatOptions = FormatOptions.STANDARDBASELINE;

  var myPath = "/Users/si/Desktop/JAVATESTFOLDER";

  var myFolder = new Folder(myPath);

  myFolder.create();

  var settings = new Array();

  var current; // current layer reference

  for (var i=0; i<doc.layers.length; i++) {

  current = doc.layers;

  settings = current.visible;

  doc.layers.visible = false;

  }

  for (var i=0; i<doc.layers.length; i++) {

  current = doc.layers;

  if (settings) {

  current.visible = true;

  var myFile = new File("Img-" + current.name + ".jpg");

  doc.saveAs(myFile, myJPEG, true);

  current.visible = false;

  }

  }

  for (var i=0; i<doc.layers.length; i++) {

  current = doc.layers;

  current.visible = settings;

  }

}

exportVisible(activeDocument);

I have tried numerous variations, but to no avail. I would be very grateful for any advice.

This topic has been closed for replies.
Correct answer Chuck Uebele

I didn't catch what JJ found, but you need to add the path, but with my corrections.

var myFile = new File(myPath + "/Img-" + current.name + ".jpg");

3 replies

Chuck Uebele
Community Expert
Chuck UebeleCommunity ExpertCorrect answer
Community Expert
May 18, 2016

I didn't catch what JJ found, but you need to add the path, but with my corrections.

var myFile = new File(myPath + "/Img-" + current.name + ".jpg");

2fingertyping
Known Participant
May 18, 2016

Thank you so much Chuck. That worked a treat.

Thank you JJ too for your input.

JJMack
Community Expert
Community Expert
May 18, 2016

The Save As is not using any Path so the save I guess goe into the current directory. Change the save as logoc in the script from

  var myFile = new File("Img-" + current.name + ".jpg");

  doc.saveAs(myFile, myJPEG, true);

To what you want...

JJMack
2fingertyping
Known Participant
May 18, 2016

Thank you JJ, but I am not sure what exactly you mean there.

I'll try Chuck's suggestion and get back to you both.

I appreciate your time.

Chuck Uebele
Community Expert
Community Expert
May 18, 2016

You don't put "Users" in your path. I'm not 100% sure about the Mac path, but it should be something like:

var myPath = new Folder('~/desktop/restOfPath/')

2fingertyping
Known Participant
May 18, 2016

Thank you for the swift reply Chuck.

Where do I put that line in the script. Should it be line 1, as implied in your reply?