file:
/src/after_effects_scripts/launcher.jsx,
304 lines,
8190 characters
line numbers
[show]
[hide]
1:{
2:// file launcher.jsx
3://
4:// dvb2005.04.02 -- Build a palette containing all the scripts in specified
5:// folders. This makes it easier to launch 'em.
6:// Includes also the ability to a) rescan all folders, and b) drop into debugger
7:// just before executing.
8:
9:
10:// +----------------------------------
11:// | Modify this folder list to your liking!
12:// |
13:
14:var gFolderPaths =
15: [
16:// "./",
17: "./Scripts/",
18: "/Users/poly/src/after_effects_scripts/"
19: ];
20:
21:
22:var gLibraries; // global array of *Lib.jsx files, built when we scan folders
23:var gCurrentScriptFile; // while executing
24:
25:function endsWith(aString,ending)
26: {
27: var endingLength = ending.length;
28: var aStringLength = aString.length;
29: var maybeEnding = aString.substring(aStringLength - endingLength,aStringLength);
30: var result = (ending == maybeEnding);
31: return result;
32: }
33:
34:// rectangles are [0..3] [left,top,right,bottom]
35:function bumpBounds(bounds,y)
36: {
37: var h = bounds[3] - bounds[1];
38: var bump = h + y;
39:
40: bounds[1] += bump;
41: bounds[3] += bump;
42: }
43:
44:// given a rectangle [left,top,right,bottom]
45:// return a partial column from it
46:function getPartialBounds(bounds,index,columns)
47:{
48: var result = copyBounds(bounds);
49: var left = bounds[0];
50: var right = bounds[2];
51: var width = right - left;
52: result[0] = left + width * index / columns;
53: result[2] = left + width * (index + 1) / columns;
54:
55: return result;
56:}
57:
58:function sayBounds(bounds)
59:{
60: var s = "(";
61: s += bounds[0] + "," + bounds[1] + "," + bounds[2] + "," + bounds[3];
62: s += ")";
63: return s;
64:}
65:
66:function copyBounds(bounds)
67: {
68: var result = new Array();
69:
70: for(i = 0; i < 4; i++)
71: result[i] = bounds[i];
72:
73: return result;
74: }
75:
76:function doInfoButtonClick()
77:{
78: var s;
79:
80: s = "Script Directories:\n";
81: for(i = 0; i < gFolderPaths.length; i++)
82: {
83: var aPath = gFolderPaths[i];
84: s += " " + aPath + "\n";
85: }
86:
87: alert(s);
88:}
89:
90:function doScriptButtonClick()
91: {
92: var theButton = this;
93: var doDebug = theButton.debugWidget.value;
94:
95: if(0)
96: alert(theButton.theScriptName + " clicked.\n"
97: + "debug = " + doDebug + ".\n"
98: + "path = " + theButton.theScriptFile.fsName + ".\n"
99: );
100:
101: var theScriptFile = theButton.theScriptFile;
102: var oldCurrentFolder = Folder.current;
103: Folder.current = theScriptFile.parent;
104:
105: theScriptFile.open();
106: var theScriptContents = theScriptFile.read();
107: theScriptFile.close();
108:
109: gCurrentScriptFile = theScriptFile;
110:
111: if(doDebug)
112: debugger;
113:
114: // You have entered the debugger in Launcher...
115: // to debug the script you've launched, step
116: // into the eval() function below.
117: //
118: // IF your script has certain kinds of syntax
119: // errors such as mimatched braces,
120: // THEN stepping-in will fail!
121: //
122: // Thanks for using Launcher.
123: // -- David Van Brink, poly@omino.com, 2005
124:
125: eval( "{" + theScriptContents + "}" );
126:
127: Folder.current = oldCurrentFolder;
128: gCurrentScriptFile = "";
129: }
130:
131:function buildPalette(paletteLeft,paletteTop)
132: {
133: Folder.current = primordialDirectory; // always same starting point...
134:
135: var thePalette = new Window("palette","Launcher");
136:
137: var bounds = [10,10,180,24];
138:
139: thePalette.add('statictext',bounds,"Launcher, by poly@omino.com 2005-7");
140: bumpBounds(bounds,5);
141: thePalette.rescanWidget = thePalette.add('button',bounds,"Rescan Folders");
142:
143: // clever but lame way to make rescan rework the whole dialog.
144: thePalette.rescanWidget.onClick = function(){this.parent.relaunch = true;this.parent.hide();};
145:
146: bumpBounds(bounds,12);
147: debugBounds = getPartialBounds(bounds,0,2);
148: thePalette.debugWidget = thePalette.add('checkbox',debugBounds,"Debug");
149: infoBounds = getPartialBounds(bounds,5,6);
150: thePalette.infoWidget = thePalette.add('button',infoBounds,"?");
151: thePalette.infoWidget.onClick = doInfoButtonClick;
152:
153:
154: // now, scan them folders
155: var folderPaths = new Array();
156:
157: // get the main list from gFolderLists
158: var i;
159: for(i = 0; i < gFolderPaths.length; i++)
160: folderPaths[folderPaths.length] = gFolderPaths[i];
161:
162: // augment with project and project/scripts current folders
163:
164: var project = app.project;
165: if(project != null) // at startup, project is null
166: {
167: var projectFile = app.project.file;
168: if(projectFile != null)
169: {
170: var projectFolder = projectFile.parent;
171: var projectFolderPath = projectFolder.fsName;
172:
173: folderPaths[folderPaths.length] = projectFolderPath;
174: folderPaths[folderPaths.length] = projectFolderPath + "/scripts";
175: }
176: }
177:
178: var scriptCount = 0;
179: var scriptButtons = new Array();
180: gLibraries = new Array();
181: for(i = 0; i < folderPaths.length; i++)
182: {
183: var aFolderPath = folderPaths[i];
184: var aFolder = new Folder(aFolderPath);
185: var scripts = aFolder.getFiles("*.jsx");
186:
187: if(scripts.length > 0)
188: bumpBounds(bounds,3); // little extra gap for each group
189:
190: var j;
191: for(j = 0; j < scripts.length; j++)
192: {
193: var aScriptFile = scripts[j];
194: var aScriptName = aScriptFile.name;
195:
196: // stash libraries (...Lib.jsx) into array
197: // for later retrieval. The our routine "loadLibrary"
198: // finds it by name.
199:
200: if(endsWith(aScriptName,"Lib.jsx"))
201: {
202: gLibraries[gLibraries.length] = aScriptFile;
203: }
204: else
205: {
206: // make a button for it, too.
207: bumpBounds(bounds,5);
208: var aButton = thePalette.add('button',bounds,aScriptName);
209: aButton.theScriptFile = aScriptFile;
210: aButton.theScriptName = aScriptName;
211: aButton.onClick = doScriptButtonClick;
212: aButton.debugWidget = thePalette.debugWidget;
213:
214: scriptButtons[scriptCount++] = aButton;
215: }
216: } // for j
217: } // for i
218:
219: thePalette.scriptButtons = scriptButtons;
220:
221: var paletteBounds = copyBounds(bounds);
222: paletteBounds[0] = paletteLeft;
223: paletteBounds[1] = paletteTop;
224: paletteBounds[2] += paletteLeft + 10;
225: paletteBounds[3] += paletteTop + 10;
226: thePalette.bounds = paletteBounds;
227:
228: thePalette.relaunch = false;
229: thePalette.onClose = paletteCloseAction;
230:
231: var resultCode = thePalette.show();
232:
233: return resultCode;
234: }
235:
236:function paletteCloseAction()
237: {
238: var thePalette = this;
239:
240: // kind of lame; each time you hit rescan you burn up
241: // more memory. But functionally works ok. but lame. dvb05
242:
243: if(thePalette.relaunch)
244: buildPalette(thePalette.bounds[0],thePalette.bounds[1]); // retain position, too
245: }
246:
247:// Load and return library contents, by name
248:// we'll try to use the one locally but use any otherwise
249:function loadLibrary(libraryName)
250: {
251: var i;
252: var fileLocal = "";
253: var fileAny = "";
254: var libraryList = "";
255: for(i = 0; i < gLibraries.length; i++)
256: {
257: var aLibraryFile = gLibraries[i];
258:
259: libraryList += "\n" + aLibraryFile.name; // for alert
260:
261: if(libraryName == aLibraryFile.name)
262: {
263: fileAny = aLibraryFile;
264: if(aLibraryFile.path == gCurrentScriptFile.path)
265: fileLocal = aLibraryFile;
266: }
267: }
268:
269: var foundLibrary = fileAny;
270: if(fileLocal != "")
271: foundLibrary = fileLocal;
272:
273: var theLibraryContents = "";
274: if(foundLibrary != "")
275: {
276: foundLibrary.open();
277: theLibraryContents = foundLibrary.read();
278: foundLibrary.close();
279: }
280: else
281: {
282: alert("Unable to load library: " + libraryName + ".\n"
283: + "Known libraries: "
284: + libraryList);
285: }
286:
287: return theLibraryContents;
288: }
289:
290:function main()
291: {
292: buildPalette(0,300);
293: }
294:
295:// +------------------------------
296:// | Start Here
297:// |
298:
299:var primordialDirectory = Folder.current; // remember this from the initial launch.
300:main();
301:
302:}
303:// end of file
304:
formatted by saShowCode.php