/////////////////////////////////////////////////////////////////////////////// //v0.3.1 - 2018.08.04 //- fix FSMagic.getBasename() returning "" for files without extension //v0.3 - 1o/2o15 //- added placeholder %item.new_name_stem% //v0.2.15034 //- using TextMagic class //v0.2.2 //- prepared to support updates/scriptwizard //v0.2.1 //- performance gain by reusing the Command object for all rename operations // (28 vs. 4secs = 700% speed increase for 200 files on my AMD quad 2.8Ghz) //v0.2 //- trim clipboard items from text data //- filter dquotes //- show number of selected/clipboard items if in "nomatch" popup //- usage of ConfigHelper for easier maintenance //- config item descriptions (including all supported variables) //todo //- autodetecting for existence of fileextensions in the clip content // to not accidentally remove extensions on renamed files if clip content // contains only pure filenames and no extensions //- KEEPEXTension mode /////////////////////////////////////////////////////////////////////////////// function OnInit(data){ var uid = "8C18600D-07C3-403E-947F-B8DE1B444E05"; var url = "https://resource.dopus.com/t/command-renamefromclipboard/19023"; //"http://resource.dopus.com/viewtopic.php?f=35&t=22867"; data.name = "Command.Item: RenameFromClipboard"; data.desc = "Rename selected items with text/items from clipboard."; data.copyright = "tbone"; data.version = "0.3.1"; data.default_enable = true; var cmd = data.AddCommand(); cmd.name = "RenameFromClipboard"; cmd.method = "Command_RenameFromClipboard"; cmd.desc = data.desc; cmd.label = "RenameFromClipboard"; cmd.template = "QUIET/K,DESELECT/S,SAFEMODE/S,COMMANDS/R"; /////////////////////////////////////////////////////////////////////////// function ConfigHelper(data){ //v1.0 this.data=data; this.descriptions=null; this.last=null; this.add = function(name, val, description){ this.data.config[name]=val; this.last=[this.data.config[name],name]; if (description!=undefined) this.des(description); return this;} this.des = function(description){ if (!(description && DOpus.version.AtLeast("11.6.1"))) return this; if (!this.descriptions){ this.descriptions=DOpus.NewMap(); data.config._descriptions=this.descriptions; } this.descriptions(this.last[1])=description; return this;} this.val = function(val){ if (typeof this.last[0]=="object") this.last[0].push_back(val); else this.last[0]=val; return this;} }////////////////////////////////////////////////////////////////////////// var cfg = new ConfigHelper(data); cfg.add('ConsoleOutput', true). des('Enable output in the script console.'); cfg.add('SafeMode', false). des('Enable safe mode, no files will be renamed. This is for watching '+ 'the script console in "whatif"-scenarios.'); cfg.add('DeSelect', false). des('Deselect items after renaming.'); cfg.add('Quiet', false). des('Do not show any confirmation dialogs.'); cfg.add('Commands', DOpus.NewVector()). des('Rename command(s) to use. Supported vars: %item.realpath%, '+ '%item.name%, %item.name_stem%, %item.path%, %item.ext%, '+ '%item.new_name%, %item.new_path%, %item.new_realpath%, %item.new_ext%'). val('Rename FROM="%item.realpath%" TO="%item.new_name%"'); } /////////////////////////////////////////////////////////////////////////////// var SUCCESS=false; var FAILURE=true; var TextMagic = new TextMagic(); var FSMagic = new FSMagic(); /////////////////////////////////////////////////////////////////////////////// String.prototype.lTrim = doLTrim; function doLTrim(){return this.replace(/^\s*/,'');} String.prototype.rTrim = doRTrim; function doRTrim(){return this.replace(/\s*$/,'');} String.prototype.trim = doTrim; function doTrim(){return this.lTrim().rTrim();} /////////////////////////////////////////////////////////////////////////////// function Command_RenameFromClipboard(data) { Log("RenameFromClipboard():"); var selItems = GetSelectedItems(data); if(!selItems){ Log(" No items selected, aborting."); return FAILURE; //no items selected } //get clean and secure array of names from clipboard var newItems = TextMagic.TextToFilePaths( DOpus.GetClip("text") ); if (!newItems.length){ Log(" No usable clipboard content, aborting."); return FAILURE; //no valid clipboard content } var quiet = GetParameter(data, "QUIET", "Quiet", false); var deselect = GetParameter(data, "DESELECT", "DeSelect", false); var safe = GetParameter(data, "SAFEMODE", "SafeMode", false); var commands = GetParameter(data, "COMMANDS", "Commands", 'Rename FROM="%item.realpath%" TO="%item.new_name%"'); if (typeof commands == "string") commands = commands.split("\\n"); if (!deselect) data.func.command.ClearFiles(); Log(" DESELECT : " + deselect); Log(" QUIET : " + quiet); Log(" SAFEMODE : " + safe); Log(" COMMANDS : " + commands); Log(" CLIPBOARD : " + newItems); var result = RenameSelItemsFromClip( selItems, newItems, quiet, safe, commands); return result; } /////////////////////////////////////////////////////////////////////////////// function RenameSelItemsFromClip(selItems, newItems, quiet, safe, commands){ if (selItems.count!=newItems.length && !quiet){ Log(" Number of selected items ("+selItems.count+") and clipboard entries "+ " ("+newItems.length+") don't match, confirm.."); if (!DOpus.Dlg.Request("The number of selected items ("+selItems.count+") and"+ " clipboard entries "+newItems.length+" do not match, continue?", "Ok|Cancel", "Rename from clipboard..")){ Log(" Cancel"); return FAILURE; } Log(" Continue"); } var commandTmp = ""; Log(" Renaming:"); var cmd = DOpus.NewCommand(); for(var c=0;cselItems.count || selItems.enumerator.atEnd()) break; var selItem = selItems.enumerator.item(); selItems.enumerator.moveNext(); var newItem = DOpus.FSUtil.NewPath(newItems[c]); Log(" ["+selItem.name+"] -> ["+newItem.filepart+"]"); for(var ce=0;ce