もっとスクリプトを簡単に書くために【再校】書き直したスクリプト本体

使い方の変更はない。
個人的に気に入らなかった部分と、
カスタマイズ性の向上を考えて書き直した。

var c;
(function() {
c  = function(s,t){return new c.prototype.init(s,t);}
c.fn = c.prototype = {
	"init" : function (s,t) {
		//$.bp();
		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;		
    	}
    	if(typeof s == 'object'){
		  return this.add(s);
		}else{
		  return 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) {
			if(typeof fnc == 'function'){
			for(var i = 0;i<this.length;i++)fnc.call(this[i],this[i],arg);
		    };
		    return this;
   	 	},
   	 	'add' : function(s){
   	 				switch(typeof s.length){
		 			 case 'number' :
		  				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(arg){
			if(!arg) return this;
   	 		var s = [];
			if(typeof arg == "function"){
				while(x = this.next())if(arg(x)){s.push(this);}
				return c(s);
			}
			if(typeof this.objectShortName[arg] == 'string')arg = this.objectShortName[arg];
			
			//filters has arg
			if(typeof c.filters[arg] == 'function'){
			    s = c.filters[arg].call(this,s);
			    return c(s);
			}else{
			     var f =function(val){
       	 		 var filter_f = new File("filters/"+val+".js");
       	 		 filter_f.open('r');
       	 		 eval(filter_f.read());
       	 		 filter_f.close();
   	 		     }
   	 		     f.call(this,arg);
			}
   	 	return c(s);
   	 	},
   	 	'filters' : {}
}

c.extend(c,extend);
c.extend(Array,extend);

/*
//Plugins
var Plugins = {
    'typename' : function(){
        this.each(function(){
        $.writeln(this.typename);
        });
        return this;
    }
}
c.extend(c,Plugins);
*/

//filters
c.filters = {
    'placedItems' : function(s){
                this.each(function(){
                    this.typename == "PlacedItem" && s.push(this);
                });
                return s;
        },
    'pathItems' : function(s){
                this.each(function(){
                var extract = function(a){
   	 			a.typename == 'PathItem' && s.push(a);
   	 			a.typename == 'CompoundPathItem' && c(a.pathItems).each(function(){extract(this);});
   	 			a.typename == 'GroupItem' && c(a.pageItems).each(function(){extract(this);});
   	 		    }
   	 			extract(this);
				});
   	 			return s;
        },
    'pathPoints' : function(s){
                this.each(function(){
   	 				var extract = function(a){
   	 				 a.typename == 'PathItem' && c(a.pathPoints).each(function(){s.push(this)});
   	 				 a.typename == 'CompoundPathItem' && c(a.pathItems).each(function(){extract(this);});
   	 				 a.typename == 'GroupItem' && c(a.pageItems).each(function(){extract(this);});
   	 				}
   	 			extract(this);
                });
                return s;
    
        },
    'groupItems' : function(s){
            this.each(function(){
                this.typename == 'GroupItem' && s.push(this);
            });
            return s;
    },
    'textFrames' : function(s){
            this.each(function(){
                this.typename == 'TextFrame' && s.push(this);
                this.typename == 'GroupItem' && c(this.textFrames).each(function(){s.push(this)});
            });
            return s;
    },
    'selection' : function(s){
            this.each(function(){
            	this.selected == true && s.push(this);
            	this.selected == "PathPointSelection.ANCHORPOINT" && s.push(this);
            });
            return s;
    }
}

var config = {
    'objectShortName' : {
        			"T" : "textFrames",
        			"G" : "groupItems",
        			"P" : "pathItems",
        			"PP" : "pathPoints",
        			"S" : "selection",
        			"PL" : "placedItems",
        			"L" : "layers"
        			},
    'f' : function(arg){return this.filter(arg)},
    'e' : function(fnc,arg){return this.each(fnc,arg)},
    'g' : function(num){return this.get(num)}
}

c.extend(c,config);
c.extend(Array,config);
})();