もっとスクリプトを簡単に書くために【3回目?】かも。

ちょっと(一週間以上)間があいてしまいました。
その間いろんなことをしていたのですが、今の所、徒労に終わっています。
今後、個々の点が線になりますかどうか。こうご期待。


さて、久々に自前のスクリプトの「c.js」の改良を行いました。
前回は「InDesign」への取っ掛かりまでは作ったのですがそのまま放置していたので
今回は、「InDesign」の対応を強化しました。
とりあえず、「テーブル」や「セル」を選択範囲から拾ってこれるようになりました。
けっこうスッキリと書けたかも。
足りない部分はまた必要に応じて付け足していきます。

#include 'c.js';
//選択範囲のセルを拾ってきて番号を振るスクリプト。
var i = 1;
c("selection").filter('cells').each(function(){this.contents = (i++).toString();});

選択したまん中のセルに無事、数字が入りました。

http://svn.coderepos.org//share/platform/illustrator/javascript/lib/c.js

var c;
(function() {
c  = function(s){return new c.prototype.init(s);}
c.fn = c.prototype = {
	'init' : function (s) {
		this.length = 0;
		if(app.documents.length == 0)return false;
		switch(typeof s){
    		case 'undefined' : return false;
    		case 'function' : return c(s());
    		case 'string' :
    		      if(typeof this.objectShortName[s] == 'string')s= this.objectShortName[s];
    		      try{
					  s = app.activeDocument[s];
					  }catch(e){
						  throw e;
					}
    			  break;
    	}
		
    	return  typeof s == 'object' ?  this.add(s) : this.add(app.activeDocument);
	}
};
c.extend = c.fn.extend = function(target,source) {for (var property in source)try{target.prototype[property] = source[property];}catch(e){target[property] = source[property];};return target;}
c.prototype.init.prototype = c.prototype;

var extend = {
		'position' : 0,
		'get' : function(num){if(typeof num == 'number' &&  num  < this.length && num >= 0)return this[num];return false;},
		'next' : function(){return this.get(this.position++);},
		'prev': function(){return this.get(--this.position);},
		'dofunc' : function(func,arg){
		          if(func.constructor.name == 'Function')func.call(this);
		          return this;
		},
	  	'each' : function(fnc,arg) {
	  	    var l = this.length;
	  	    var res = [];
			if(typeof fnc == 'function'){
			for(var i = 0;i<l;i++){
				 ret = fnc.call(this[i],this[i],arg);
				 typeof ret != "undefined" && res.push(ret); 
			 }
			}
			if(res.length > 0){
                return res;
			}else{
                return this;
		    }
   	 	},
   	 	'add' : function(s){
   	 	          if(typeof s == 'string')s = c(s);
   	 	          
   	 				switch(typeof s.length){
		 			 case 'number' :
		 			    var s_length = s.length;
		  				for(var i = 0 ; i < s_length; i++)this[this.length+i] = s[i];
		  				this.length = this.length+s.length;
		  				break;
		 			 default : 
		  				this[0] = s;
		  				this.length = 1;
					}
		      return this;
   	 	},
   	 	'filter' : function(){
			if(arguments.length == 0)return this;
			var s = {'TRUE' : [],'FALSE' : []};
			
			for(i=0;i<arguments.length;i++){
				arg = arguments[i];
				if(typeof arg == "function"){
					while(x = this.next()){
					   arg(x) ? s.TRUE.push(this) : s.FALSE.push(this);
				}
			}else(typeof arg == 'string'){
					if(typeof this.objectShortName[arg] == 'string')arg = this.objectShortName[arg];
					if(typeof c.filters[arg] == 'function')s = c.filters[arg].call(this,s);
			}
		}
		try{
			return c(s.TRUE);
		}catch(e){
			return c();
		}
   	 	},
   	 	'filters' : {},
   	 	'temp' : []
}
c.extend(c,extend);
c.extend(Object,extend);
c.extend(Array,extend);

//イラストレーター用
//
var config_for_illustrator = {
    'filters' : {
        'placedItems' : function(s){
                var extract = function(a){a.constructor.name == 'PlacedItem' ? s.TRUE.push(a) : (a.constructor.name == 'GroupItem' ? c(a.pageItems).each(function(){extract(this);}) : s.FALSE.push(a));}
                this.each(function(){extract(this);});
    	 		return s;
            },
        'rasterItems' : function(s){
                var extract = function(a){a.constructor.name == 'RasterItem' ? s.TRUE.push(a) : (a.constructor.name == 'GroupItem' ? c(a.pageItems).each(function(){extract(this);}) : s.FALSE.push(a));}
                this.each(function(){extract(this);});
    	 		return s;  
            },
        'pathItems' : function(s){
      	 		var extract = function(a){a.constructor.name == 'PathItem' ? s.TRUE.push(a)  :  ((a.constructor.name == 'CompoundPathItem' || a.constructor.name == 'GroupItem') ? c(a.pathItems).each(function(){extract(this);}) : s.FALSE.push(a) );}
                this.each(function(){extract(this);});
    	 		return s;
        },
        'pathPoints' : function(s){
      	 		var extract = function(a){a.constructor.name == 'PathItem' ? c(a.pathPoints).each(function(){s.TRUE.push(a)})  :  ((a.constructor.name == 'CompoundPathItem' || a.constructor.name == 'GroupItem') ? c(a.pathItems).each(function(){extract(this);}) : s.FALSE.push(a) );}
                this.each(function(){extract(this);});
                return s;
            },
        'groupItems' : function(s){
                this.each(function(){this.constructor.name == 'GroupItem' ? s.TRUE.push(this) : s.FALSE.push(this);});
                return s;
        },
        'textFrames' : function(s){
                this.each(function(){this.constructor.name == 'TextFrame' ? s.TRUE.push(this) : (this.constructor.name == 'GroupItem' ? c(this.textFrames).each(function(){s.TRUE.push(this)}) : s.FALSE.push(this));});
                return s;
        },
        'selection' : function(s){
                this.each(function(){(this.selected == true ||  this.selected == 'PathPointSelection.ANCHORPOINT') ? s.TRUE.push(this) : s.FALSE.push(this);});
                return s;
        }
    },
    'objectShortName' : {
        "I" : 'pageItems',
    	"T" : 'textFrames', 
    	"G" : 'groupItems', 
    	"P" : 'pathItems', 
    	"PP" : 'pathPoints', 
    	"S" : 'selection', 
    	"PL" : 'placedItems', 
    	"L" : 'layers', 
    	"R" : 'rasterItems',
        },
    'f' : this.filter,
    'e' : this.each,
    'g' : this.get
}

//インデザイン用
//
var config_for_indesign = {
'filters' : {
    'selection' : function(s){},
    'groupItems' : function(s){
			this.each(function(){
				this.constructor.name == 'Group' ? s.TRUE.push(this) : s.FALSE.push(this);
			});
			return s;
	},
    'textFrames' : function(s){
            this.each(function(){
                    this.constructor.name == 'TextFrame' ? s.TRUE.push(this) : (this.constructor.name == 'Group'  ? c(this.textFrames).each(function(){s.TRUE.push(this)}) : s.FALSE.push(this));
            }); 
            return s;
    },
	
	'paragraphs' : function(s){
			var textFrames = this.filters.textFrames.call(this,{'TRUE' : [],'FALSE' : []});
			c(textFrames.FALSE).each(function(){
				//$.bp();
				(this.constructor.name == 'Text' || this.constructor.name == 'TextColumn' || this.constructor.name == 'Paragraph' ||  this.constructor.name == 'Cell' )&& textFrames.TRUE.push(this);
				this.constructor.name == 'Table' && c(this.cells).each(function(){textFrames.TRUE.push(this)});
			});
			c(textFrames.TRUE).each(function(){
				c(this.paragraphs).each(function(){s.TRUE.push(this)});
			});
			return s;
	},
	'words' : function(s){
			var textFrames = this.filters.textFrames.call(this,{'TRUE' : [],'FALSE' : []});
			c(textFrames.FALSE).each(function(){
				(this.constructor.name == 'Text' || this.constructor.name == 'TextColumn' || this.constructor.name == 'Paragraph'  || this.constructor.name == 'Cell' ) && textFrames.TRUE.push(this);
				this.constructor.name == 'Table' && c(this.cells).each(function(){textFrames.TRUE.push(this)});
			});
			c(textFrames.TRUE).each(function(){
				c(this.words).each(function(){s.TRUE.push(this)});
			});
			return s;
	},
	'characters' : function(s){
			var textFrames = this.filters.textFrames.call(this,{'TRUE' : [],'FALSE' : []});
			c(textFrames.FALSE).each(function(){
				(this.constructor.name == 'Text' || this.constructor.name == 'TextColumn' || this.constructor.name == 'Paragraph' || this.constructor.name == 'Cell' ) && textFrames.TRUE.push(this);
				this.constructor.name == 'Table' && c(this.cells).each(function(){textFrames.TRUE.push(this)});
			});
			c(textFrames.TRUE).each(function(){
				c(this.characters).each(function(){s.TRUE.push(this)});
			});
			return s;
	},
    'tables' : function(s){
			var textFrames = this.filters.textFrames.call(this,{'TRUE' : [],'FALSE' : []});
			c(textFrames.FALSE).each(function(){
				(this.constructor.name == 'Table' || this.constructor.name == 'Cell' )&& s.TRUE.push(this);
			});
			c(textFrames.TRUE).each(function(){
				c(this.tables).each(function(){s.TRUE.push(this)});
			});
			return s;
            },
    'cells' : function(s){
			var tables = this.filters.tables.call(this,{'TRUE' : [],'FALSE' : []});	
			c(tables.TRUE).each(function(){
				c(this.cells).each(function(){s.TRUE.push(this)});
			});
			return s;
		},
    'rows' : function(s){
			var tables = this.filters.tables.call(this,{'TRUE' : [],'FALSE' : []});
			c(tables.TRUE).each(function(){
				c(this.rows).each(function(){s.TRUE.push(this)});
				});
			return s;
		
	},
},
'objectShortName' : {
        "I" : 'pageItems',
    	"T" : 'textFrames', 
    	"G" : 'groupItems', 
    	"P" : 'pathItems', 
    	"PP" : 'pathPoints', 
    	"S" : 'selection', 
    	"PL" : 'placedItems', 
    	"L" : 'layers', 
    	"R" : 'rasterItems',
        },
'f' : this.filter,
'e' : this.each,
'g' : this.get

}

//
if(app.name == 'Adobe Illustrator'){
c.extend(c,config_for_illustrator);
c.extend(Object,config_for_illustrator);
c.extend(Array,config_for_illustrator);
}

if(app.name == 'Adobe InDesign'){
	c.extend(c,config_for_indesign);
	c.extend(Object,config_for_indesign);
	c.extend(Array,config_for_indesign);
}
})();