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

Switch Layers Script Question

Enthusiast ,
Sep 26, 2014 Sep 26, 2014

Hi I have a small script here which someone from these forums posted not too long ago... was it Noel? Either way, this script is fantastic and brings up a sub-menu to switch documents. When the menu pops up, it shows a list of all open documents in Photoshop. (I actually modified this script so all it does is switch documents instead of copying the active layer to another document.. somehow I managed it with the very basic knowledge I have )

The default document selection (the document name that is highlighted in the menu) when the list opens is "0"

I'm looking for a way to get the highlighted selection to reflect the active document instead of "0".

I think it's somewhere in this line of code:

win.NewList.selection = 0;

and I've tried changing the 0 to "actLay" and also "activeLayer" but it throws me an error message. Is there a way to get the active document as the highlighted selection? Here's the code...

var aDoc = app.activeDocument;

var AllDocs = app.documents;

var actLay = aDoc.activeLayer;

if (AllDocs.length > 1) {

var itemDoc = null;

var win = new Window("dialog","Switch Documents");

this.windowRef = win;

win.Txt1 = win.add ("statictext", undefined, "Switch to which document?");

win.NewList=win.add ("dropdownlist", undefined)

for (var m = 0; m < AllDocs.length; m++) {

win.NewList.add("item", AllDocs.name) 

}

win.NewList.selection = 0;

itemDoc = win.NewList.selection.actLay;

win.cancelBtn = win.add("button", undefined, "Switch");

win.cancelElement = win.cancelBtn;

win.NewList.onChange= function () {

    itemDoc = win.NewList.selection.index;

    return itemDoc;

    }

win.show();

app.activeDocument = app.documents[itemDoc];

app.refresh();

}

TOPICS
Actions and scripting
704
Translate
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

Community Expert , Sep 27, 2014 Sep 27, 2014

Here's your original code with just minor changes:

Added a var to hold activeDocument index.

Added if statement to set the above variable.

Added a line to set the dropdownlist selection.

You should also start scripts with the target application. In this case: #target photoshop

#target photoshop  

    var aDoc = app.activeDocument; 

    var AllDocs = app.documents; 

    var actLay = aDoc.activeLayer;

    var activeIndex;//add var to hold activeDocument's index

    

    if (AllDocs.length > 1) { 

...
Translate
Adobe
Community Expert ,
Sep 27, 2014 Sep 27, 2014

You could include the line

if (AllDocs == app.activeDocument) {win.NewList.selection = m}

in the for-clause.

Why do you use the CancelElement as you do?

Translate
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
Enthusiast ,
Sep 27, 2014 Sep 27, 2014

sorry, the Cancel element was left over from the previous code that I edited out.. hadn't even noticed that.

I've included


if (AllDocs == app.activeDocument) {win.NewList.selection = m}

and the script still runs, but it still isn't auto-selecting the active document in the drop-down list, it only selects index0. What am I doing wrong?

var aDoc = app.activeDocument; 

var AllDocs = app.documents; 

var actLay = aDoc.activeLayer; 

 

if (AllDocs.length > 1) { 

var itemDoc = null; 

 

var win = new Window("dialog","Switch

Documents"); 

this.windowRef = win; 

win.Txt1 = win.add ("statictext", undefined,

"Switch to which document?"); 

win.NewList=win.add ("dropdownlist",

undefined) 

for (var m = 0; m < AllDocs.length; m++) { 

win.NewList.add("item", AllDocs.name)

if (AllDocs == app.activeDocument)

{win.NewList.selection = m} 

win.NewList.selection = 0; 

itemDoc = win.NewList.selection.index; 

win.cancelBtn = win.add("button", undefined,

"Switch"); 

 

win.NewList.onChange= function () { 

    itemDoc = win.NewList.selection.index; 

    return itemDoc; 

    } 

 

win.show(); 

 

app.activeDocument = app.documents[itemDoc]; 

app.refresh(); 

}

Translate
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
Community Expert ,
Sep 27, 2014 Sep 27, 2014

In your second post, you're on the right track with if statement at line 24; however, you negate this by leaving line 29 in setting the selection back to 0.  I would run though the loop, store which index the active document is in an variable, then set the selection with that variable after the loop.

Translate
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
Enthusiast ,
Sep 27, 2014 Sep 27, 2014

I've removed line 29 so the selection isn't index0 and replaced it with the variable that I defined at the top of the script ( var Current = app.activeDocument; ) Problem is now it's throwing me another error message. It says that the current document (whatever its name may be in Photohop) is not a list index or listitem... This is close but I can't figure out where it's going wrong. The fact that the error message is now referencing my active document is a good sign but I'm still trying to successfully achieve this

var aDoc = app.activeDocument; 

var AllDocs = app.documents; 

var actLay = aDoc.activeLayer; 

var Current = app.activeDocument;

if (AllDocs.length > 1) { 

var itemDoc = null; 

var win = new Window("dialog","Switch

Documents"); 

this.windowRef = win; 

win.Txt1 = win.add ("statictext", undefined,

"Switch to which document?"); 

win.NewList=win.add ("dropdownlist",

undefined) 

for (var m = 0; m < AllDocs.length; m++) { 

win.NewList.add("item", AllDocs.name)  

{win.NewList.selection = Current}

itemDoc = win.NewList.selection.actLay; 

win.cancelBtn = win.add("button", undefined,

"Switch"); 

win.cancelElement = win.cancelBtn; 

win.NewList.onChange= function () { 

    itemDoc = win.NewList.selection.index; 

    return itemDoc; 

    } 

win.show(); 

app.activeDocument = app.documents[itemDoc]; 

app.refresh(); 

Translate
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
Community Expert ,
Sep 27, 2014 Sep 27, 2014

Here's your original code with just minor changes:

Added a var to hold activeDocument index.

Added if statement to set the above variable.

Added a line to set the dropdownlist selection.

You should also start scripts with the target application. In this case: #target photoshop

#target photoshop  

    var aDoc = app.activeDocument; 

    var AllDocs = app.documents; 

    var actLay = aDoc.activeLayer;

    var activeIndex;//add var to hold activeDocument's index

    

    if (AllDocs.length > 1) { 

    var itemDoc = null; 

    

    

    var win = new Window("dialog","Switch Documents"); 

    this.windowRef = win; 

    win.Txt1 = win.add ("statictext", undefined, "Switch to which document?"); 

    win.NewList=win.add ("dropdownlist", undefined) 

    for (var m = 0; m < AllDocs.length; m++) { 

    win.NewList.add("item", AllDocs.name)  

    if(AllDocs.name == aDoc.name){activeIndex = m}//set activeIndex if activeDocument name matches current doc in list

    }

var docR = activeDocument

    win.NewList.selection = activeIndex;   //Set selection for dropdownlist

 

    //itemDoc = win.NewList.selection.actLay;

  

    

    win.cancelBtn = win.add("button", undefined, "Switch"); 

    

    

    win.cancelElement = win.cancelBtn; 

    

    

    

    win.NewList.onChange= function () { 

        itemDoc = win.NewList.selection.index; 

        return itemDoc; 

        } 

    

    win.show(); 

    

    app.activeDocument = app.documents[itemDoc]; 

    

    app.refresh(); 

    } 

Translate
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
Enthusiast ,
Sep 27, 2014 Sep 27, 2014

Update, after playing with this for a few hours, I've managed to get around the error messages, but the problem now is that when the list pops up, some of the documents are listed as many as 3 times. I'm having some success in getting the active document to be the highlighted document by default, but it's still a little erratic. I'm pulling my hair out over this one. Can someone spot the error - it's almost there

var aDoc = app.activeDocument; 

var AllDocs = app.documents; 

var actLay = aDoc.activeLayer; 

var IndexCurrent = app.index

 

if (AllDocs.length > 1) { 

var itemDoc = null; 

 

var win = new Window("dialog","Switch

Documents"); 

this.windowRef = win; 

win.Txt1 = win.add ("statictext", undefined,

"Switch to which document?"); 

win.NewList=win.add ("dropdownlist",

undefined) 

for (var m = 0; m < AllDocs.length; m++) { 

win.NewList.add("item", AllDocs.name)

win.NewList.add("item", aDoc.name)

if (AllDocs == app.activeDocument)

{win.NewList.selection = m};

itemDoc = win.NewList.selection.index; 

 

win.cancelBtn = win.add("button", undefined,

"Switch"); 

win.cancelElement = win.cancelBtn; 

 

win.NewList.onChange= function () { 

    itemDoc = win.NewList.selection.index; 

    return itemDoc; 

    } 

 

win.show(); 

 

app.activeDocument = app.documents[itemDoc]; 

app.refresh(); 

Translate
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
Enthusiast ,
Sep 27, 2014 Sep 27, 2014

nevermind, I got it! WOOHOO! It works now   Thanks for your guys help.. and just want you to know that I'm not looking for handouts... I'm genuinely trying to learn this type of code and after a few hours of experimenting/researching, I finally figured it out. Here it is:

var aDoc = app.activeDocument; 

var AllDocs = app.documents; 

var actLay = aDoc.activeLayer; 

var IndexCurrent = app.index

 

if (AllDocs.length > 1) { 

var itemDoc = null; 

 

var win = new Window("dialog","Switch

Documents"); 

this.windowRef = win; 

win.Txt1 = win.add ("statictext", undefined,

"Switch to which document?"); 

win.NewList=win.add ("dropdownlist",

undefined) 

for (var m = 0; m < AllDocs.length; m++) { 

win.NewList.add("item", AllDocs.name)

if (AllDocs == app.activeDocument)

{win.NewList.selection = m};

itemDoc = win.NewList.selection.index; 

 

win.cancelBtn = win.add("button", undefined,

"Switch"); 

win.cancelElement = win.cancelBtn; 

 

win.NewList.onChange= function () { 

    itemDoc = win.NewList.selection.index; 

    return itemDoc; 

    } 

 

win.show(); 

 

app.activeDocument = app.documents[itemDoc]; 

app.refresh(); 

Translate
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
Enthusiast ,
Sep 27, 2014 Sep 27, 2014
LATEST

oh geez - thanks csuebele - I replied with my code literally 1 minute after you!! Thank you though.. what a coincidencel lo... I'll study your script as well, it's probably more stable than mine.

Translate
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