// DESCRIPTION: Launch a script by typing its name or picking it from a resent-history list // Peter Kahrel 2005-2008 #target indesign // Global declarations ----------------------------------------- history_length = 15; script_folder = script_dir()+'/'; f = File (script_folder + 'runscript-last.txt') //-------------------------------------------------------------- main(); function main () { script = get_script (); app.doScript (script) } //-------------------------------------------------------------- function get_script () { var history = get_history (f); var scripts = get_scripts (script_folder); var droplist = history.concat('------------').concat(scripts); var dlg = app.dialogs.add ({name: 'Launch a script'}); with (dlg) with (dialogColumns.add()) with(borderPanels.add()) { staticTexts.add ({staticLabel: 'Script:'}); with (dialogColumns.add()) { var script = textEditboxes.add ({minWidth: 200}); var dropDown = dropdowns.add ({ stringList : droplist, selectedIndex : 0, minWidth : 200 }) } } if (!dlg.show()) {dlg.destroy(); exit()}; if (script.editContents != "") // user entered script name, use it { var scr_name = script.editContents; // if user didn't type extension, add it if (scr_name.indexOf ('.jsx') < 0) scr_name += '.jsx'; script = scr_name; if ( !File (script_folder+script).exists) { alert ('Error: ' + scr_name + ' doesn\'t exist.'); dlg.destroy; exit() } } else // user pressed Enter, run last-run script script = droplist[dropDown.selectedIndex]; store_history (f, history, script) return File (script_folder+script) } function get_history (f) { var h = []; if (f.exists) { f.open ('r'); var temp = f.read (); f.close(); var h = temp.split (/[\r\n]/); } return h } function get_scripts (dir) { var array = []; var s = find_all (dir, dir, [], 'jsx'); for (var i = 0; i < s.length; i++) array.push (s[i]); return array } function store_history (f, history, new_item) { // var f = File (script_folder + history_file); history = insert_item (history.join('\n'), new_item) f.open ('w'); f.write (history); f.close () } function insert_item (s, item) { s = '\n' + s + '\n'; s = item + s.replace ('\n'+item+'\n', '\n'); s = clip (s); return s.replace (/\n$/, ""); } function clip (s) { if (s.match (/\n/g).length > history_length) s = s.replace (/\n[^\n]+\n$/, "\n"); return s } // Remove comments to include subdirectories function find_all (scriptdir, dir, array, mask) { var f = Folder (dir).getFiles ('*.*'); for (var i = 0; i < f.length; i++) // if (f[i] instanceof Folder) // find_all (scriptdir, f[i], array, mask); // else if (f[i].name.slice (-mask.length) == mask) array.push (f[i].fullName.slice (scriptdir.length)); return array; } function script_dir() { try {return File (app.activeScript).path.replace (/%20/g, ' ')} catch (e) {return File (e.fileName).path.replace (/%20/g, ' ')} }