もっとスクリプトを簡単に書くために【二校目】

もっとスクリプトを簡単に書くために【再校】に引き続き二校目。


変更箇所:

  • indesignにも対応できるようにtypename使っていた所をconstructor.nameに変更。
  • addの動作が期待どおり動くようにした。
テキストフレームとパスアイテムを削除する。
c('textFrames').add('pathItems').each(function(){
this.remove();
});
  • filterの引数の数を可変にした。
選択した中から配置画像とラスタライズ画像を削除する。
c("selection").filter("placedItems","rasterItems").each(function(){
this.remove();
})
  • eachの時に戻り値を返せるようにした。
c("selection").filter("textFrames").each(function(){
return this.contents
}).each(function(){
$.writeln(this)
});
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);},
	  	'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;
			//$.writeln(typeof arguments);
			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'){
					//$.writeln(arg.split (",").length);
					if(typeof this.objectShortName[arg] == 'string')arg = this.objectShortName[arg];
					if(typeof c.filters[arg] == 'function')s = c.filters[arg].call(this,s);
				}
		}
			return c(s.TRUE);
   	 	},
   	 	'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 = {

}

//
c.extend(c,config_for_illustrator);
c.extend(Object,config_for_illustrator);
c.extend(Array,config_for_illustrator);
})();

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