/**  Drop Import Files Delux

      PS CS2 ドロップレット用Script
      
      ドラッグ＆ドロップした"フォルダ"または"ファイル"と同じ階層にあるファイル( tga と psd のみ)を読み込み、２つのモードで処理。
      
      
      =Each Document Mode=(推奨)
         読み込んだドキュメントに対して、それぞれレイヤーセットを作成。
         チェックボックスのＯＮ・ＯＦＦで新規ドキュメントが作れる。
         新規にドキュメントを作成する場合には、読み込んだ複数ファイルの最大サイズで作成され、その時に解像度を指定できる。
         
         手動で各ドキュメントのレイヤーセットを新規ドキュメントに移動しないとダメ。
         そのかわりに高速で処理できる。
         
         
      =One Document Mode=
          読み込んだドキュメントをそれぞれレイヤーセットに分別して、新規psdファイル１つにまとめる。
          新規ドキュメントは読み込んだ複数ファイルの最大サイズで作成され、その時に解像度を指定できる。

          複数ファイルを一気に１つのドキュメントにまとめられるが、処理に多少時間がかかる。
          
          
          
          
      ファイルサイズが大きい場合やファイル数が多い場合などは、=Each～= モードで読み込んで
      手でまとめた方が遥かに早い。
      
      ドロップレットにしなくても、現在のアクティブドキュメントのファイルパスに対して実行できる。
      アクティブでない他のドキュメントがある場合、強制的に閉じる。(保存はしない)
      
      

 */

var originalUnit = preferences.rulerUnits
preferences.rulerUnits = Units.PIXELS;


var folderPath = null;
var strFolderPath = null;
var folderList = new Array();
var nFolderList = null;
var nFol = 0;
var FolName = new Array();
var mDlg = null;
var eachBtn = null;
var oneBtn = null;
var ResAlert = false;
var FolBtn = new Array();
var ResName = new Array();
var nRes = 0;
var ValAlert = true;
var strFolderPath = null;
var strResName = null;
var fFolPath = new Array();
var nResFolPath = null;
var ResFolPath = new Array();
var ResfileList = new Array();
var opFile = null;
var stropFile = new Array();
var actW = new Array();
var actH = new Array();
var actR = new Array();
var actDoc = new Array();
var nOpenDoc = null;
var ResActDoc = null;
var ResW = null;
var ResH = null;
var ResR = null;
var newDoc = null;
var newDocName = null;
var yesnoBtn = null;
var Btnon = null;
var resoNew = null;
var sl1Name = null;
var sl2Name = null;
var ResValName = null;
var RGBColor = new SolidColor();
RGBColor.red = 255;
RGBColor.green = 0;
RGBColor.blue = 0;

//*********Main Script**************************************************************

ResetColor();
//ExchangeColor();
	        
GetFolderPath();

FolderCount();

//フォルダが無かった場合[Error:A]
      if(ValAlert == false){
                alert("******フォルダがありませんでした******");
      }else{

                MyDialog();
                
                FolBtnChk();

         //キャンセル or 読み込みフォルダを選択しなかった場合
                if(ValAlert == false){
                        //[Error:B]
                }else{
                
                           //alert(ResName);

                           Ffiles();

                   //他にファイル(tga,psd)が無かった場合[Error:C]
                           if(ValAlert == false){
                                     alert("******他に開けるファイルがありませんでした******");

           //One Document Mode
                           }else if(oneBtn.value == true){
                           	
                                     NewDoc();
                                     //alert(ResW + "," + ResH);
                                     if(Btnon == "Cancel")
                                     {
                                     alert("******キャンセルされました******");

                                     }else if(Btnon == "OK")
                                     {
                                         //新規ファイルを保存しなかった場合[Error:D]
                                         if(ValAlert == false){
                                                                    alert("******キャンセルされました******");

　　                                     }else{

                                                CopyPasteLy();
                                                

                                        }
                                     }
           //Each Document Mode
                          }else if(eachBtn.value == true){
                          	
                                   if(newDocBtn.value == true)
                                   {
                                           NewDoc();
                                           //alert(ResW + "," + ResH);
                                           if(Btnon == "Cancel")
                                           {
                                                 alert("******キャンセルされました******");

                                           }else if(Btnon == "OK")
                                           {
                                             //新規ファイルを保存しなかった場合[Error:D]
                                                if(ValAlert == false){
                                                                    alert("******キャンセルされました******");

　　                                             }else{

                                                        Doc_LySet();
                                                        
                                                        activeDocument = newDoc
                                                        PreChkBox();
                                                        newDoc.save();

                                                 }
                                            }
                                     }else{
                                       Doc_LySet();
                                     }
                          }
                }                
       }

preferences.rulerUnits = originalUnit;

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

function GetFolderPath()
{

     var sFilePath = activeDocument.fullName;
     var strSfilePath = sFilePath.toString();
     //alert(sFilePath);

     var nFile = strSfilePath.length;
     var searchO = strSfilePath.lastIndexOf("/");
     //alert(searchO);

     var sFolderPath = strSfilePath.substr(0,searchO);
     //alert(sFolderPath);

     var searchP = sFolderPath.lastIndexOf("/",searchO);
     //alert(searchP);

     strFolderPath = sFolderPath.substr(0,searchP);
     //alert(strFolderPath);

//フォルダ元パス
     folderPath = new Folder(strFolderPath);

     var sDocNum = documents.length;
     do {
     	     activeDocument.close(SaveOptions.DONOTSAVECHANGES);
     	     sDocNum = documents.length;

     }while (sDocNum>0);

}

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

//フォルダ元直下のフォルダをカウント
function FolderCount()
{

//各フォルダパス
      folderList = folderPath.getFiles();
      nFolderList = folderList.length;
      //alert(nFolderList+"個のフォルダがあります");

      q = 0;
      for(i = 0; i < nFolderList; i++){
                      var strName = folderList[i].name;
                      //alert(folderList[i]);

             //フォルダかどうか選別
                      var searchP = strName.lastIndexOf(".");
                      if(searchP == -1){

                                        FolName[q] = strName;
                                        q++;
                      }
     }

     nFol = FolName.length;
     //alert(nFol);

//[Error:A]
     if(nFol == 0){
            ValAlert = false;
     }

}

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

function MyDialog()
{

//フォルダ数に合わせた縦サイズ
     mDlg = new Window("dialog","Select Folder",[200,100,700,(120+(90+(30*nFol)))]);
     clBtn = mDlg.add("button",[250,(20+(30*nFol)),355,(50+(30*nFol))], "CANCEL!", { name:"cancel"});
     okBtn = mDlg.add("button",[370,(20+(30*nFol)),475,(50+(30*nFol))], "OK!", { name:"ok"});
     
     selMode = mDlg.add("group" ,[5,(5+(30*nFol)),245,(100+(30*nFol))]);
          eachMode = selMode.add("panel" , [10,10,240,50], "Each Document Mode" );
               eachBtn = selMode.add("radiobutton" , [30,25,90,45], "Each Doc");eachBtn.value = true;
               newDocBtn = selMode.add("checkbox",[130,25,230,45],"newDocument ?");newDocBtn.value = true;
          oneMode = selMode.add("panel" , [10,50,240,90], "One Document Mode" );
               oneBtn = selMode.add("radiobutton" , [30,65,130,85], "One Doc");oneBtn.value = false;
               
               eachBtn.onClick = function(){newDocBtn.visible = true;}
               oneBtn.onClick = function(){newDocBtn.hide();}
     
     for (var i = 0; i < nFol; i++){
                                        
                      // alert(FolName[i]);
                      var aPos = (10+(30*i));
                      var bPos = (40+(30*i));
                      //alert(aPos,bPos);
       
                      FolBtn[i] = mDlg.add("checkbox",[20, aPos, 275, bPos], FolName[i]);
                                              
     }

     okBtnClick();
     clBtnClick();

     mDlg.center();
     mDlg.show();

}

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

//選択フォルダをカウント
function FolBtnChk()
{
	if(ValAlert == true)
	{
          var cVal = 0;
          u = 0;
          for (var i = 0; i < nFol; i++){
                
                 if(FolBtn[i].value == true)
                 {

             //フォルダを選択したらカウントする
                              ResName[u] = FolName[i];
                              cVal = cVal + 1;
                         
                              strFolderPath = folderPath.toString();
                              strResName = ResName[u].toString();

                              fFolPath[u] = strFolderPath + "/" + strResName;
                              //alert(fFolPath[u]);

                              u++;
                 }
                                        
          }

     //[Error:B]
          if(cVal == 0){
                 ValAlert = false;
                 alert("******フォルダが選択されませんでした******");
          }

          nRes = ResName.length;
     }
}

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

function okBtnClick()
{
	Btnon = "OK";
	mDlg.close();
}

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

function clBtnClick() 
{
	clBtn.onClick = function()
	{
	    Btnon = "Cancel";
        ValAlert = false;
        mDlg.close();
     //[Error:B]
        //alert("******キャンセルされました******");
	}
}
     
//*********FUNCTION**************************************************************

//選択フォルダ内のファイルを検索
function Ffiles()
{ 

     uu = 0;
     for(i = 0; i < nRes; i++){

           ResFolPath = new Folder(fFolPath[i]);
           //alert(ResFolPath);

           ResfileList = ResFolPath.getFiles();
           nResFileList = ResfileList.length;
           //alert(ResfileList + "******" + nResFileList +"個のファイルがあります");
 
           fileOP();
     }

   nOpenDoc = documents.length;
   //alert(nOpenDoc);

//[Error:C]
   if(nOpenDoc < 2){
              ValAlert = false;
   }

}


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

function fileOP()
{
  
   for(q = 0; q < nResFileList; q++){

        var SearchQaS = null;
        var SearchQaB = null;
        var SearchQbS = null;
        var SearchQbB = null;

//開くファイルを[tga,psd]に限定
        SearchQa = ResfileList[q].name.lastIndexOf(".tga");
        SearchQa = ResfileList[q].name.lastIndexOf(".TGA");
        SearchQb = ResfileList[q].name.lastIndexOf(".psd");
        SearchQb = ResfileList[q].name.lastIndexOf(".PSD");

        if(SearchQaS != -1 & SearchQaB != -1 & SearchQbS != -1 & SearchQbB != -1){

             //alert(SearchQa + "," + SearchQb);

             var fileObj = null;
             stropFile[q] = ResfileList[q].toString();


             fileObj = new File(stropFile[q]);
             //alert(fileObj);
             open(fileObj);

             var colMode = activeDocument.mode;
             if(colMode != "DocumentMode.RGB"){
	                alert("******RGBモードではありません******");
	                activeDocument.close(SaveOptions.DONOTSAVECHANGES);
             }else{

                  var w = activeDocument.width.value;
                  var h = activeDocument.height.value;
                  var r = activeDocument.resolution;

                  actW[uu] = w;
                  actH[uu] = h;
                  actR[uu] = r;
                  actDoc[uu] = activeDocument;
                  uu++;
             }
        }
   }

}

//************Function********************************************
function resoDlg()
{
     

var resoDlg = new Window("dialog","Resolution",[200,100,350,200]);
resoInfo = resoDlg.add( "statictext" , [16,16,180,36] , "reso Info :" ); resoInfo.justify="left";
resoEdit = resoDlg.add( "edittext" , [55,40,105,60] , ResR ); resoEdit.justify="center";
resoEdit.active = true;

		RclBtn = resoDlg.add( "button" , [10,70,70,90], "Cancel" , {name:"cancel"});
		RokBtn = resoDlg.add( "button" , [80,70,140,90] , "OK" , {name:"ok"} );
		
		RokBtnClick();
		RclBtnClick();
		
		resoDlg.center();
		resoDlg.show();
		
		resoNew = resoEdit.text;

		function RokBtnClick()
		{
		     RokBtn.onClick = function(){ Btnon = "OK"; resoDlg.close();}
		}
		
		function RclBtnClick()
		{
			 RclBtn.onClick = function(){Btnon = "Cancel";  resoDlg.close();}
		}
}		

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

//新規作成
function NewDoc()
{

     var distFileObj = null;

//選択ファイルの最大解像度

     LoopW();
     LoopH();
     LoopR();
     
     resoDlg();

     if(Btnon == "Cancel")
     {
               //alert("******キャンセルされました******");

     }else if(Btnon == "OK")
     {
               newDoc = app.documents.add(ResW,ResH,resoNew);
           
           //+20ずつ大きくリサイズ
               ResizeDocA();
      
               do {
                       newFilePath = File.saveDialog("保存場所を選択してください","psdファイル:*.psd, tgaファイル:*.tga, 全てのファイル形式:*",strFolderPath);

              //OKが押された場合の処理
                       if(newFilePath) {

                               distFileObj = new File(newFilePath);
                               yesnoBtn = true;

                              //同じ名前のファイルがあった場合、上書き保存確認ダイアログを出す
                              if(distFileObj.exists) {

                                      res = confirm("上書き保存しますか?");
                                     //alert(res);

                                    //OKが押されたら元ファイルを削除
                                    if(res){
                                             distFileObj.remove();
                                    }else{

                                            //上書きをキャンセル
                                           yesnoBtn = false;
                                           continue;
                                    } 
                             }

                     }else{

                            //[Error:D]
                            yesnoBtn = null;
                            break;
                     }

                 } while(yesnoBtn == false);

          //[Error:D]
          if(yesnoBtn == null){
                                   ValAlert = false;
                         
　       　}else{

          var id411 = charIDToTypeID( "save" );
          var desc93 = new ActionDescriptor();
          var id412 = charIDToTypeID( "As  " );
          var desc94 = new ActionDescriptor();
          var id413 = charIDToTypeID( "Pht3" );
          desc93.putObject( id412, id413, desc94 );
          var id414 = charIDToTypeID( "In  " );
          desc93.putPath( id414, distFileObj );
          executeAction( id411, desc93, DialogModes.NO );

         newDoc.name = distFileObj
         newDocName = newDoc.name;
         //alert(newDocName);
        }
    }

}

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

//横サイズ検索
function LoopW()
{
  ResW = actW[0];
  if(nOpenDoc > 1){
    
     for(i = 1; i < nOpenDoc;i++){

         if(ResW < actW[i]){
             ResW = actW[i];
        }
     }
  }

}

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

//縦サイズ検索
function LoopH()
{
  ResH = actH[0];
  if(nOpenDoc > 1){

     for(i = 1; i < nOpenDoc;i++){

        if(ResH < actH[i]){
            ResH = actH[i];

        }
    }
  }

}

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

//解像度検索
function LoopR()
{
  ResR = actR[0];
  if(nOpenDoc > 1){

     for(i = 1; i < nOpenDoc;i++){

        if(ResR < actR[i]){
            ResR = actR[i];

        }
    }
  }

}

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

//新規ファイルにレイヤーでまとめる
function CopyPasteLy()
{
     for(i = 0; i < nOpenDoc; i++){
     	  ResActDoc = actDoc[i];
          activeDocument = actDoc[i];
          var nActLy = actDoc[i].artLayers.length;
          var valName = actDoc[i].name;
          var nValName = valName.toString().length;
          ResValName = valName.substr(0,nValName -4 );
          
          var strNewDoc = newDoc.toString();
          var ValNewDoc = new File(strNewDoc);
          var chkValName = valName.toString();
          var tgaS = chkValName.lastIndexOf(".tga");
          var tgaB = chkValName.lastIndexOf(".TGA");
          var psdS = chkValName.lastIndexOf(".psd");
          var psdB = chkValName.lastIndexOf(".PSD");
          
          activeDocument = newDoc;
          var lsObj = newDoc.layerSets.add();
          lsObj.name = ResValName;

         if(tgaS > -1 || tgaB > -1){
         	   activeDocument = actDoc[i];
               actDoc[i].activeLayer = actDoc[i].layers[0];
               actDoc[i].layers[0].opacity = 100;
               actDoc[i].selection.selectAll();
               actDoc[i].selection.copy();
               actDoc[i].close(SaveOptions.DONOTSAVECHANGES);

               activeDocument = newDoc;
               newDoc.activeLayer = newDoc.layers[0];
               newDoc.selection.selectAll();
               newDoc.paste();
               newDoc.layerSets[0].artLayers[0].name = ResValName;
         }else if(psdS > -1 || psdB > -1){
         	    activeDocument = actDoc[i];
              	var actLys = activeDocument.layers;
                var nLys = actLys.length;
                
                if(nLys > 1)
                {

                    if(actLys[nLys-1].isBackgroundLayer == true){actLys[nLys-1].opacity = 100;}
                
                    ResizeDocB();

                     for(gg = nLys-1; gg >= 0; gg--){
                     	   actLys[gg].visible = true;
                     	   var layType = actLys[gg].kind;
                           if(layType == "LayerKind.NORMAL" && actLys[gg].transparentPixelsLocked == true){actLys[gg].transparentPixelsLocked = false;}
                     	   actDoc[i].activeLayer = actLys[gg];
        	               var lyName = actLys[gg].name;
        	               
        	           //コピー元に枠線を作成
        	               LoadChannel();
        	               
                     	   activeDocument.artLayers.add();
                           activeDocument.activeLayer.remove();
                           actDoc[i].activeLayer = actLys[gg];
                           
                           activeDocument.selection.fill(RGBColor);
        	               activeDocument.selection.selectAll();
	                       activeDocument.activeLayer.copy();
	                       
	                   //新規ファイルにペースト
	                       activeDocument = newDoc;
	                       newDoc.activeLayer = newDoc.layers[0];	                       
	                       newDoc.selection.selectAll();
	                       newDoc.paste();
	                       newDoc.layerSets[0].artLayers[0].name = lyName;
	                       
	                   //枠線を削除
	                       LoadChannel();
	                       activeDocument.artLayers.add();
                           activeDocument.activeLayer.remove();
                           newDoc.activeLayer = newDoc.layerSets[0].artLayers[0];
	                       activeDocument.selection.clear();
	                       activeDocument.selection.deselect();
	                       
	                       activeDocument = actDoc[i];
	                       
	                       lyName = null;
                     }

                    actDoc[i].close(SaveOptions.DONOTSAVECHANGES);
                    activeDocument = newDoc;
	                newDoc.activeLayer = newDoc.layers[0];
	                nLys = null;
	                actLys = null;
	                nActLy = null;
	                
                }else if(nLys == 1)
                {
                	if(actLys[0].isBackgroundLayer == true){actLys[0].opacity = 100;}
                
                    ResizeDocB();
                    
                     	   actLys[0].visible = true;
                     	   var layType = actLys[0].kind;
                     	   if(layType == "LayerKind.NORMAL" && actLys[0].transparentPixelsLocked == true){actLys[0].transparentPixelsLocked = false;}
                     	   actDoc[i].activeLayer = actDoc[i].artLayers[0];
        	               var lyName = actLys[0].name;
        	               
        	            //コピー元に枠線を作成
        	               LoadChannel();
        	               
                           activeDocument.artLayers.add();
                           actDoc[i].activeLayer = actDoc[i].layers[0];
                           actDoc[i].layers[0].remove();
                           
                           activeDocument.selection.fill(RGBColor);
        	               activeDocument.selection.selectAll();
	                       activeDocument.activeLayer.copy();
	                       
	                       activeDocument = newDoc;
	                       newDoc.activeLayer = newDoc.layers[0];
	                       
	                   //新規ファイルにペースト
	                       newDoc.selection.selectAll();
	                       newDoc.paste();
	                       newDoc.layerSets[0].artLayers[0].name = lyName;
	                       
	                   //枠線を削除
	                       LoadChannel();
	                       activeDocument.artLayers.add();
                           activeDocument.activeLayer.remove();
                           newDoc.activeLayer = newDoc.layerSets[0].artLayers[0];
	                       activeDocument.selection.clear();
	                       activeDocument.selection.deselect();
	                       
	                       activeDocument = actDoc[i];
	                       
	                       lyName = null;

                    actDoc[i].close(SaveOptions.DONOTSAVECHANGES);
                    activeDocument = newDoc;
	                newDoc.activeLayer = newDoc.layers[0];
	                nLys = null;
	                actLys = null;
	                nActLy = null;
                }
                
                newDoc.channels[3].remove();
           
         }
    }
  
  EndFunc();
  
  PreChkBox();
  newDoc.save();

}

//*********FUNCTION**************************************************************
function ResizeDocA()
{

activeDocument.selection.deselect();
nW = activeDocument.width;
nH = activeDocument.height;
nA = nW / nH;
activeDocument.resizeCanvas(nW + 20,nH + 20,AnchorPosition.MIDDLECENTER);

nW = null;
nH = null;
nA = null;
}

//*********FUNCTION**************************************************************
function ResizeDocB()
{

activeDocument.channels.add();
activeDocument.activeLayer.invert();
activeDocument.selection.selectAll();
activeDocument.activeLayer.copy();

activeDocument = newDoc;
newDoc.selection.selectAll();
newDoc.channels.add();
newDoc.paste();
newDoc.selection.deselect();
activeDocument.artLayers.add();
activeDocument.activeLayer.remove();
newDoc.activeLayer = newDoc.layerSets[0];

activeDocument = ResActDoc;
nW = activeDocument.width;
nH = activeDocument.height;
nA = nW / nH;
activeDocument.resizeCanvas(nW + 20,nH + 20,AnchorPosition.MIDDLECENTER);

activeDocument.selection.deselect();

nW = null;
nH = null;
nA = null;
}

//*********FUNCTION**************************************************************
function LoadChannel()
{
nCh = activeDocument.channels.length;
chObj = activeDocument.channels[nCh-1];
activeDocument.selection.load(chObj);
activeDocument.selection.invert();
}

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

//各ファイルをレイヤーセットでまとめる
function Doc_LySet()
{
     for(i = 0; i < nOpenDoc; i++){
        activeDocument = actDoc[i];
     	AllLys_LySet();
     }
}

//*********FUNCTION**************************************************************
function AllLys_LySet()
{
var valName = activeDocument.name;
var nValName = valName.toString().length;
ResValName = valName.substr(0,nValName -4 );
var oLys = activeDocument.layers;

var nLy = oLys.length;

if(oLys[nLy-1].isBackgroundLayer == true){oLys[nLy-1].opacity = 100;}

sl1Name = oLys[0].name;

sl2Name = oLys[nLy-1].name;

Select1();
Select2();



SetGroup();

ReName();

}

// =======================================================
function Select1()
{
var oID1 = charIDToTypeID( "slct" );
    var oDesc1 = new ActionDescriptor();
    var oID2 = charIDToTypeID( "null" );
        var oRef1 = new ActionReference();
        var oID3 = charIDToTypeID( "Lyr " );
        oRef1.putName( oID3, sl1Name );
    oDesc1.putReference( oID2, oRef1 );
    var oID4 = charIDToTypeID( "MkVs" );
    oDesc1.putBoolean( oID4, false );
executeAction( oID1, oDesc1, DialogModes.NO );
}

// =======================================================
function Select2()
{
var oID5 = charIDToTypeID( "slct" );
    var oDesc2 = new ActionDescriptor();
    var oID6 = charIDToTypeID( "null" );
        var oRef2 = new ActionReference();
        var oID7 = charIDToTypeID( "Lyr " );
        oRef2.putName( oID7, sl2Name );
    oDesc2.putReference( oID6, oRef2 );
    var oID8 = stringIDToTypeID( "selectionModifier" );
    var oID9 = stringIDToTypeID( "selectionModifierType" );
    var oID10 = stringIDToTypeID( "addToSelectionContinuous" );
    oDesc2.putEnumerated( oID8, oID9, oID10 );
    var oID11 = charIDToTypeID( "MkVs" );
    oDesc2.putBoolean( oID11, false );
executeAction( oID5, oDesc2, DialogModes.NO );
}

// =======================================================
function SetGroup()
{

    var gID1 = charIDToTypeID( "Mk  " );
    var gDesc1 = new ActionDescriptor();
    var gID2 = charIDToTypeID( "null" );
    var gRef1 = new ActionReference();
    var gID3 = stringIDToTypeID( "layerSection" );
    gRef1.putClass( gID3 );
    gDesc1.putReference( gID2, gRef1 );
    var gID4 = charIDToTypeID( "From" );
    var gRef2 = new ActionReference();
    var gID5 = charIDToTypeID( "Lyr " );
    var gID6 = charIDToTypeID( "Ordn" );
    var gID7 = charIDToTypeID( "Trgt" );
    gRef2.putEnumerated( gID5, gID6, gID7 );
    gDesc1.putReference( gID4, gRef2 );
    executeAction( gID1, gDesc1, DialogModes.NO );

}

// =======================================================
function ReName()
{

    var gID8 = charIDToTypeID( "setd" );
    var gDesc2 = new ActionDescriptor();
    var gID9 = charIDToTypeID( "null" );
    var gRef3 = new ActionReference();
    var gID10 = charIDToTypeID( "Lyr " );
    gRef3.putName( gID10, "グループ 1" );
    gDesc2.putReference( gID9, gRef3 );
    var gID11 = charIDToTypeID( "T   " );
    var gDesc3 = new ActionDescriptor();
    var gID12 = charIDToTypeID( "Nm  " );
    gDesc3.putString( gID12, ResValName );
    var gID13 = charIDToTypeID( "Lyr " );
    gDesc2.putObject( gID11, gID13, gDesc3 );
    executeAction( gID8, gDesc2, DialogModes.NO );

}

//*********FUNCTION**************************************************************
function ResetColor()                                                                                     //描画・背景色をリセット
{
var id719 = charIDToTypeID( "Rset" );
    var desc152 = new ActionDescriptor();
    var id720 = charIDToTypeID( "null" );
        var ref82 = new ActionReference();
        var id721 = charIDToTypeID( "Clr " );
        var id722 = charIDToTypeID( "Clrs" );
        ref82.putProperty( id721, id722 );
    desc152.putReference( id720, ref82 );
executeAction( id719, desc152, DialogModes.NO );
}

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

function ExchangeColor()                                                                                   //描画・背景色を反転
{
var id723 = charIDToTypeID( "Exch" );
    var desc153 = new ActionDescriptor();
    var id724 = charIDToTypeID( "null" );
        var ref83 = new ActionReference();
        var id725 = charIDToTypeID( "Clr " );
        var id726 = charIDToTypeID( "Clrs" );
        ref83.putProperty( id725, id726 );
    desc153.putReference( id724, ref83 );
executeAction( id723, desc153, DialogModes.NO );

}

//*********FUNCTION**************************************************************
function EndFunc()
{
  newDoc.artLayers[0].remove();
  
//新規ファイルを元のサイズにリサイズ
  var valW = activeDocument.width;
  if(valW > ResW){newDoc.resizeCanvas(ResW,ResH,AnchorPosition.MIDDLECENTER);}

}

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

//環境設定_互換性を優先チェックを非表示
function PreChkBox()
{
var id527 = charIDToTypeID( "setd" );
    var desc117 = new ActionDescriptor();
    var id528 = charIDToTypeID( "null" );
        var ref54 = new ActionReference();
        var id529 = charIDToTypeID( "Prpr" );
        var id530 = charIDToTypeID( "FlSP" );
        ref54.putProperty( id529, id530 );
        var id531 = charIDToTypeID( "capp" );
        var id532 = charIDToTypeID( "Ordn" );
        var id533 = charIDToTypeID( "Trgt" );
        ref54.putEnumerated( id531, id532, id533 );
    desc117.putReference( id528, ref54 );
    var id534 = charIDToTypeID( "T   " );
        var desc118 = new ActionDescriptor();
        var id535 = stringIDToTypeID( "maximizeCompatibility" );
        var id536 = charIDToTypeID( "QurS" );
        var id537 = charIDToTypeID( "QurA" );
        desc118.putEnumerated( id535, id536, id537 );
    var id538 = charIDToTypeID( "FlSv" );
    desc117.putObject( id534, id538, desc118 );
executeAction( id527, desc117, DialogModes.NO );

}

//*********FUNCTION_END**************************************************************
