/**
====================================================================================================================================
     ****** SelLyColor_Chg_EXX ******

     複数選択したレイヤーのレイヤーカラーを一気に変換

 
     ドキュメント内に同じ名前のレイヤーが複数あっても、元のリンク状態に戻るように改良。
    
   
    ==不具合アリマス==
     ドキュメント内に同じ名前のレイヤーが複数ある場合、選択中のレイヤーにその内の１つが含まれていると
     元の選択状態とは違うレイヤーが選択されて完了する場合があります。(元の選択状態にならない場合がある)

     
     2008.0909

====================================================================================================================================
*/


var linkLys = new Array();
var linkSets = new Array();
var valLink = new Array();

var actDoc = activeDocument;
var actLy = actDoc.activeLayer;
var selLinkLys = new Array();
var resLys = new Array();
var dumNames = new Array();
var visLys = new Array();
var motoNames = new Array();
var motoLinkName = new Array();


var numColor = 8;
var lyBtn = new Array();
var lyName = new Array();
var valColor = null;
var resLyColor = null;
var valAlert = true;

         //カラーコレクション
		var colorStr = new Array();
		colorStr[0] = "None";
		colorStr[1] = "Rd  ";
		colorStr[2] = "Orng";
		colorStr[3] = "Ylw ";
		colorStr[4] = "Grn ";
		colorStr[5] = "Bl  ";
		colorStr[6] = "Vlt ";
		colorStr[7] = "Gry ";

		var colorName = new Array();
		colorName[0] = "カラーなし";
		colorName[1] = "レッド";
		colorName[2] = "オレンジ";
		colorName[3] = "イエロー";
		colorName[4] = "グリーン";
		colorName[5] = "ブルー";
		colorName[6] = "バイオレット";
		colorName[7] = "グレー";


//************Main_Script******************************************************



SearchLinked(actDoc,0,0);


UnLinkSelLys();

try
{
     LinkSelLys();
}
catch(e)
{
}
finally
{
}

SelLy();

UnLinkSelLys();





           MyDialog();
                
                
           LyBtnChk();
                
           if(valAlert == true)
           {
                
                VisLy();
                LyColor();

           }
                                        


ReLinked();


ReSelected();



//***********FUNCTION*******************************************************

function SearchLinked(layObj, indent,u)
{
//元のリンク状態を取得
     var i,k;
     nn=0;
     var n = layObj.artLayers.length;
     for (i=0; i<n; i++)
     {
          linkLys = layObj.artLayers[i].linkedLayers;
          if(linkLys.length > 0)
          {
               valLink[u] = linkLys;
	           valLink[u].push(layObj.artLayers[i]);
	           nn++;
	           u++;

          }
     }
     
     var ns = layObj.layerSets.length;
     for (i=0; i<ns; i++)
     {
          for (k=0; k<indent; k++);
          linkSets = layObj.layerSets[i].linkedLayers;
          if(linkSets.length > 0)
          {

	           valLink[u] = linkSets;
	           valLnk[u].push(layObj.layerSets[i]);
	           u++;
	           
	      }
         SearchLinked(layObj.layerSets[i],indent+1,u);
     }
}

//******************************************************************************

function MyDialog()
{
     var mDlg = new Window("dialog","Select Color",[200,100,580,(100+(90+(30*numColor)))]);
     var grpDlg = mDlg.add("group",[20,20,570,(30+(30*numColor))]);
     var okBtn = mDlg.add("button",[130,(50+(30*numColor)),225,(80+(30*numColor))], "OK!", { name:"ok"});


                for (var i = 0; i < numColor; i++)
                {
                          var aPos = (10+(30*i));
                          var bPos = (40+(30*i));
                          //alert(aPos,bPos);
       
                          lyBtn[i] = grpDlg.add("radiobutton",[100, aPos, 275, bPos], colorName[i]);                                              
                }
     mDlg.center();
     mDlg.show();

}

//******************************************************************************

function LyBtnChk()
{
                var valChk = false;
                
                for (var i = 0; i < numColor; i++)
                {
                
                             if(lyBtn[i].value == true){
                              
                                     valColor = colorStr[i];
                             
                                     valChk = true;
                             }
                                        
               }
               
               if(valChk == false)
               {
                           alert("******色が選ばれていません******");
                           valAlert = false;
               }else if(valChk == true)
               {
                    resLyColor = valColor;
               }
               
}

//******************************************************************************

function LyColor()
{

     nresLys = resLys.length;
     for(q=0;q<nresLys;q++)
     {

          actDoc.activeLayer = resLys[nresLys-1];
          actDoc.activeLayer = resLys[q];

//選択レイヤーの名前を一時変更(同じ名前が複数あった場合に"SetColor()"で都合が悪い為)
          motoNames = resLys[q].name.toString();
          actDoc.activeLayer.name = "ChangeName"+q;

          dumNames[q] = actDoc.activeLayer.name;

          SetColor();

//名前を復元
          actDoc.activeLayer.name = motoNames;
//非表示だったものを復元
          if(visLys[q] == false)
          {
                    resLys[q].visible = false;
          }
     }
}

//******************************************************************************

function SetColor()
{
//レイヤーカラーを指定
    var cID1 = charIDToTypeID( "setd" );
    var cDesc1 = new ActionDescriptor();
    var cID2 = charIDToTypeID( "null" );
    var cRef1 = new ActionReference();
    var cID3 = charIDToTypeID( "Lyr " );
    cRef1.putName( cID3, dumNames[q] );         //一時変更後のレイヤー名で指定
    cDesc1.putReference( cID2, cRef1 );
    var cID4 = charIDToTypeID( "T   " );
    var cDesc2 = new ActionDescriptor();
    var cID5 = charIDToTypeID( "Clr " );
    var cID6 = charIDToTypeID( "Clr " );
    var cID7 = charIDToTypeID( resLyColor );
    cDesc2.putEnumerated( cID5, cID6, cID7 );
    var cID8 = charIDToTypeID( "Lyr " );
    cDesc1.putObject( cID4, cID8, cDesc2 );
    executeAction( cID1, cDesc1, DialogModes.NO );

}

//******************************************************************************

function LinkSelLys()
{
//選択レイヤーをリンク付け
    var lID1 = stringIDToTypeID( "linkSelectedLayers" );
    var lDesc1 = new ActionDescriptor();
    var lID2 = charIDToTypeID( "null" );
    var lRef1 = new ActionReference();
    var lID3 = charIDToTypeID( "Lyr " );
    var lID4 = charIDToTypeID( "Ordn" );
    var lID5 = charIDToTypeID( "Trgt" );
    lRef1.putEnumerated( lID3, lID4, lID5 );
    lDesc1.putReference( lID2, lRef1 );
    executeAction( lID1, lDesc1, DialogModes.NO );

}

//******************************************************************************

function UnLinkSelLys()
{
//選択レイヤーのリンク解除
    var uID1 = stringIDToTypeID( "unlinkSelectedLayers" );
    var uDesc1 = new ActionDescriptor();
    var uID2 = charIDToTypeID( "null" );
    var uRef1 = new ActionReference();
    var uID3 = charIDToTypeID( "Lyr " );
    var uID4 = charIDToTypeID( "Ordn" );
    var uID5 = charIDToTypeID( "Trgt" );
    uRef1.putEnumerated( uID3, uID4, uID5 );
    uDesc1.putReference( uID2, uRef1 );
    executeAction( uID1, uDesc1, DialogModes.NO );

}

//******************************************************************************

function SelLy()
{
//リンクされたレイヤーを取得
     selLinkLys = actLy.linkedLayers;
     nactLy = selLinkLys.length;
     resLys[0] = actLy;
     u=1;

     for(i=0;i<nactLy;i++)
     {
          resLys[u] = selLinkLys[i];
          u++;
     }
}

//******************************************************************************

function VisLy()
{
//非表示レイヤーを表示
     nresLys = resLys.length;
     for(i=0;i<nresLys;i++)
     {
          visLys[i] = resLys[i].visible;
          //alert(visLys[i]);
          resLys[i].visible = true;
     }
}

//******************************************************************************

function ReLinked()
{
//リンクを元の状態に復元
     nVal = valLink.length;
     for(uu=0;uu<nVal;uu++)
     {
	     for(uuu=0;uuu<valLink[uu].length;uuu++)
	     {
              //alert(valLink[uu][uuu]);
      
         //リンクの重複を回避
              if(valLink[uu][uuu].linkedLayers.length == 0)
              {
              	//選択レイヤーの名前を一時変更(同じ名前が複数あった場合に"LinkSelLys()"に都合が悪い為)
              	   motoLinkName[0] = valLink[uu][0].name;
              	   valLink[uu][0].name = uu+"chgLinkName0";
      	           activeDocument.activeLayer = valLink[uu][0];
      	           for(p=1;p<valLink[uu].length;p++)
      	           {
      	           	    motoLinkName[p] = valLink[uu][p].name;
      	           	    valLink[uu][p].name = uu+"chgLinkName"+p;     	  
      	           }
      	            
      	            for(ppp=0;ppp<valLink[uu].length;ppp++)
      	            {
      	            	sl3Name = valLink[uu][ppp].name;
      	         //トグル選択
      	            	ToggleSelect(sl3Name);
      	            }
      	            
      	            UnLinkSelLys();
      	            
      	           try
      	           {
      	                LinkSelLys();      	
      	           }
      	           catch(e)
      	           {
      	           }
      	           finally
      	           {
      	           }
      	           
      	           for(pp=0;pp<valLink[uu].length;pp++)
      	           {
      	       //元の名前に復元
      	           	activeDocument.activeLayer = valLink[uu][valLink[uu].length-1];
      	           	valLink[uu][pp].name = motoLinkName[pp];
      	           }
      	           
               }
	     }
	      //alert("************");
     }
}

//******************************************************************************

function ToggleSelect(TslName)
{
//トグル選択(Ctrl+Click)
    var oID12 = charIDToTypeID( "slct" );
    var oDesc3 = new ActionDescriptor();
    var oID13 = charIDToTypeID( "null" );
    var oRef3 = new ActionReference();
    var oID14 = charIDToTypeID( "Lyr " );
    oRef3.putName( oID14, TslName );
    oDesc3.putReference( oID13, oRef3 );
    var oID15 = stringIDToTypeID( "selectionModifier" );
    var oID16 = stringIDToTypeID( "selectionModifierType" );
    var oID17 = stringIDToTypeID( "addToSelection" );
    oDesc3.putEnumerated( oID15, oID16, oID17 );
    var oID18 = charIDToTypeID( "MkVs" );
    oDesc3.putBoolean( oID18, false );
    executeAction( oID12, oDesc3, DialogModes.NO );

}

//******************************************************************************

function ReSelected()
{
//元のレイヤー選択状態に復元
	activeDocument.activeLayer = resLys[0];
	for(g=1;g<resLys.length;g++)
	{
		sl3Name = resLys[g].name;
		ToggleSelect(sl3Name);
	}

}

//*********FUNCTION   END******************************************************