// JavaScript Document
//フォルダ保存先


var fileRef = Folder.desktop + '/sample.csv';
//グループ全解除。
function removeGroup(gpi){
	if(gpi.constructor.name !== 'GroupItem')return false;
	var gpis = gpi.pageItems;
	var mvs =[];
	for(i=0;i<gpis.length;i++){
				mvs.push(gpis[i]);
	}
	while(mvx = mvs.shift()){
				mvx.move(gpi,ElementPlacement.PLACEBEFORE);		
	}
	gpi.remove();
	return true
}



fileRef = new File (fileRef);
flag = fileRef.open ("w");
docObj = activeDocument;

var appVersion = parseInt(version.substr(0,2),10);
// アクティブドキュメント取得しておく　レイヤー１枚にオブジェクトをすべて配置してください。

var actDoc = activeDocument;
var document = app.activeDocument;

// ドキュメント上の画像を取得 
var placed = actDoc.placedItems; 

var raster = actDoc.rasterItems; 
var rastername;
imgObj = activeDocument.pluginItems;
// 画像配列
var resultArr = new Array;

// 宣言
var placedLen = placed.length;
var imgfile;
var pItem;
var text;
var form;
var textfont;
var style;
	var backgroundcolor;
var textsize;
var color;
 var contents;
 //var selectedItems = pageItems;
 var textcolor;
 var style;
 var rastername;
 var layObj;
 var objname;
 var g;
 var pItemn;
 var formstart;
    var formend;
     var formtext;
     var formumber;
     var formtel;
     var formtextare;
     var formadress;
     var rep;
     var colorback;
    var backgroundcolor;
    var i5;
    var imgfile1;
    var imgfile2;
    var borderstyle;
    var bordersize;

var bordercolor;
var bordercolors;
var backgroundcolor;
 var bordersize;
   var bordercolor;
     var borderstyle;
     var colors;
    var colorsred;
var colorsgreen;
var colorsblue;
var copacity;
var obj2;
 var backgroundcolor;
 var opacityg;
 var backgroundcolor;
 var  backgroundcolor;
 var colorbackString;
 var  opacityf;
 var ormsubmit;
 var formreset;
 var link;
var link2;
var formfile;
var formurl;
var wrap;

//keycode
var br = String.fromCharCode(13);
var br2 = String.fromCharCode( 3 ) ;
var ctr = String.fromCharCode(17);
var a = String.fromCharCode( 65 ) ;
var alt = String.fromCharCode(18);
var o = String.fromCharCode(70 ) ;
var shift = String.fromCharCode(16);
var g = String.fromCharCode( 71 ) ;
var obj0;
var r;
var text2;




var br3;
defaultStroked = true;
defaultFillColor = true;
 
 //１行目書込み


fileRef.write("");
fileRef.write(',');
fileRef.write("名前");
fileRef.write(',');
fileRef.write("テキスト");
fileRef.write(',');
fileRef.write("テキストカラー");

fileRef.write(',');
fileRef.write("テキストフォント");
fileRef.write(',');

fileRef.write("テキストサイズ");

fileRef.write(',');
fileRef.write("横幅");
fileRef.write(',');
fileRef.write("縦幅");
fileRef.write(',');
fileRef.write("x");
fileRef.write(',');
fileRef.write("y");
fileRef.write(',');
fileRef.write("img_file");
fileRef.write(',');
fileRef.write("リンク先");
fileRef.write(',');
fileRef.write("id");
fileRef.write(',');
fileRef.write("class");
fileRef.write(',');
fileRef.write("name2");
fileRef.write(',');
fileRef.write("フォーム");
fileRef.write(',');
fileRef.write("線種類");
fileRef.write(',');
fileRef.write("線幅");
fileRef.write(',');
fileRef.write("線色");
fileRef.write(',');
fileRef.write("背景色");

fileRef.write("\r");

var obj=app.activeDocument.pageItems;

var i = 0;

pageObj=app.activeDocument.pages;

  var rasnum =  docObj.rasterItems.length ;
   var textnum = docObj.textFrames.length;
   var len = rasnum + textnum;
  
  var rastername;
  var htmlindex ;
  var layObj = app.activeDocument.layers;
  var layer = document.layers;
  var la;
  var la2;
  var rem;
  var layers;
  var lay2
  var brn;
  var t;
   len  = app.activeDocument.pageItems.length;
 lay2=app.activeDocument.layers.length;
//forループ

 for(r=0;r<len; r++){
          removeGroup(app.activeDocument.pageItems[r]);
       }

 alert ('要素数は' + len +　'です。');
 
 
     obj0=app.activeDocument.pageItems[0];
         for(i=0;i<len; i++){
           
id2 ='id' + i + 1;
 
     obj=app.activeDocument.pageItems[i];
    
     obj2 = obj.name;
     htmlindex = obj2.lastIndexOf('html');
    
   
   
// 位置情報

var objPosi = obj.visibleBounds;


 x =(objPosi[0]);
 y = -(objPosi[1]);
x1 = (objPosi[2]);
y1 = -(objPosi[3]) ;

// サイズ
width = (x1 - x);
height=y1 - y;


//name1
name1 ="name1" + i;
//name2
name2 = "name2" + i;

//text

    
    chObj = activeDocument.activeChannels;
  

      
borderstyle = 'solid';

//border-size
bordersize =obj.strokeWidth;

//線
if (obj.stroked== true) {
        //選択した対象が線あり
        borderstyle = 'solid';
       bordersize=obj.strokeWidth;
       bordercolor =obj.strokeColor;
       
       
bordercolor.red = bordercolor.red;
bordercolor.green= bordercolor.green;
bordercolor.blue= bordercolor.blue;
opacity = obj.opacity;
    
    bordercolor= "rgba("+ bordercolor.red + "."+ bordercolor.green + "." + bordercolor.blue + "." + opacity / 100 + ")";
      
    }
else {borderstyle = '';
     bordersize = '';
        bordercolor = '';}
 

//文字 イラストレーターで文字は１行ずつに分解してください。アウトラインなしで。

if (obj.typename == "TextFrame") {
    
 if(obj0.contents =='undefined'){}
        else if(obj0.contents =='undefined'){
 if(  obj0.textRange.characterAttributes.textFont.style=='undefined'){
     
if(obj0.textRange.characterAttributes.size=='undefined'){
    if(obj0.textRange.characterAttributes.fillColor=='undefined'){
        
text = obj0.contents;


textfont  = obj0.textRange.characterAttributes.textFont.family;

	style    = obj0.textRange.characterAttributes.textFont.style;
	
	textsize     = obj0.textRange.characterAttributes.size;
    
	color = obj0.textRange.characterAttributes.fillColor;
        
        }
    }     
     }


      }
  else if(0<i<len){
    
text = obj.contents;

  
if(text !==''){
   
  br3 =text.indexOf(br,0);
  //br3 =text.replace('!',"\r");
  //alert (br3);
  //alert (text);
 
 //

    if(br3 ==-1){


     }else {
         
         text= text.replace(/\r/g,"!");
       
}


  
  textfont  =  obj.textRange.characterAttributes.textFont.family;

	style    =  obj.textRange.characterAttributes.textFont.style;
	
	textsize     =  obj.textRange.characterAttributes.size;
    
	color =  obj.textRange.characterAttributes.fillColor;

color.red =color.red;
color.green=color.green;
color.blue=color.blue;
opacity =obj.opacity;
colorString = "rgba("+ color.red + "."+ color.green + "." + color.blue + "." + opacity / 100 + ")";
  
textcolor =colorString;
}
}
}
else{
    text ="";
textfont  = "";
	style    = "";
	
	textsize     ="";
	
   textcolor ="";

textcolor ="";

}
imgfile = '';
 imgfile2 = ''; 
 
 //画像
 

 //画像 うめこみのみ
 
 
if (obj.typename !== "RasterItem") {
    imgfile = '';
    imgfile2 = '';

}
else if (obj.typename == "RasterItem" ){
//pItem = docObj.rasterItems;    
pItem = docObj.pageItems;   
    i5 = i  -1;
    if(i == 0){ 
        if( pItem[0].file.name =='undefined'){}
        else{imgfile2 = pItem[0].file.name;
      }
       }
    if(i<len +2){ 
        
       imgfile2 = pItem[i].file.name;
     
       }
   
    else if(i5 >=len +2 ){ imgfile2 ='';}
   }
   
     if(imgfile == "undefined"){
 imgfile= '';    
    imgfile2 = '';}

var link= htmlindex;
//id
var id ='id' + i;
//class

var class1 ='class' + i; 


 
 //塗り
 if(obj.filled == true) {
        //選択した対象が背景あり
        colorback = activeDocument.pageItems[i].fillColor;
colorback.red =colorback.red;
colorback.green=colorback.green;
colorback.blue=colorback.blue;
opacityf =obj.opacity;
colorbackString = "rgba("+ colorback.red + "."+ colorback.green + "." + colorback.blue + "." + opacityf / 100 + ")";
     backgroundcolor =colorbackString;
     
    }
else{backgroundcolor = '';
    
    /*
	color =  obj.textRange.characterAttributes.fillColor;

color.red =color.red;
color.green=color.green;
color.blue=color.blue;
opacity =obj.opacity;
colorString = "rgba("+ color.red + "."+ color.green + "." + color.blue + "." + opacity / 100 + ")";
  
textcolor =colorString;
*/
    }


// ＣＳＶ変換

// ファイル保存

fileRef.write(i + 1);
fileRef.write(',');
fileRef.write(name1);
fileRef.write(',');
fileRef.write(text);

fileRef.write(',');
if (textcolor !== -1){
   
textcolor=textcolor;
}
else {textcolor='';}
fileRef.write(textcolor);
fileRef.write(',');
fileRef.write(textfont);
fileRef.write(',');
fileRef.write(textsize);
fileRef.write(',');

fileRef.write(width);
fileRef.write(',');
fileRef.write(height);
fileRef.write(',');
fileRef.write(x);
fileRef.write(',');
fileRef.write(y);
fileRef.write(',');

fileRef.write(imgfile2);
fileRef.write(',');
formstart = obj2.indexOf('start',0);
if(link  !== -1){
link2= obj2 ;
 
}else{link2='';}

if(formstart !== -1){
         link2 =obj2.split("/");
         link2 =link2[1];
         form = 'start';
         }
    
fileRef.write(link2);

fileRef.write(',');
fileRef.write(id2);
fileRef.write(',');

fileRef.write('class' + i);
fileRef.write(',');
fileRef.write(name2);
fileRef.write(',');
//form staet とendで包む。endはstartより下に配置　start ,endは塗りおよび線なしでオブジェクト作る。
 
     formend = obj2.indexOf('end',0);
     formtext = obj2.indexOf('text',0);
     formumber = obj2.indexOf('number',0);
     formtel = obj2.indexOf('tel',0);
     formtextarea = obj2.indexOf('area',0);
     
     
     formurl = obj2.indexOf('url',0);
     
     formemail = obj2.indexOf('email',0);
     formsubmit = obj2.indexOf('submit',0);
     formreset= obj2.indexOf('reset',0);
     formfile = obj2.indexOf('file',0);
      formcheck = obj2.indexOf('check',0);
     if(form == -1){form ='';}
     else if(formstart !== -1){
         
       
         form = 'start';
 
}
     else if(formend !== -1){form = 'end';}
     else if(formtext !== -1){form = 'text';}
     else if(formumber !== -1){form = 'number';}
     else if(formtel !== -1){form = 'tel';}
     else if(formtextarea !== -1){form =obj2;}
     else if(formemail !== -1){form = 'email';}
      else if( formfile !== -1){form ='file';}
             else if( formurl !== -1){form ='url';}
            else if( formcheck  !== -1){form ='check ';}
      else if( formcheck  !== -1){form ='url ';}
      else if(formsubmit  !== -1){form = 'submit';}
       else if(formreset !== -1){form = 'reset';}
       
     else{form = '';}
     

fileRef.write(form);
fileRef.write(',');


     


       
      
fileRef.write(borderstyle);
fileRef.write(',');

fileRef.write(bordersize);
fileRef.write(',');
fileRef.write(bordercolor);
fileRef.write(',');
fileRef.write(backgroundcolor);

fileRef.write("\r");


}


fileRef.close;
alert ('ＣＳＶ保存完了しました。');

