/* =================================================================================================== AUTHOR : Ken Function : Set Tags & Add Tags by List Created : 2022-12-07 Modified : 2022-12-25 Version : v1.2 =================================================================================================== */ // Set Tags & Add Tags by List. // (c) 2022 Ken // This is a script for Directory Opus. // See https://www.gpsoft.com.au/DScripts/redirect.asp?page=scripts for development information. // Called by Directory Opus to initialize the script function OnInit(initData) { initData.name = "Set Tags & Add Tags by List"; initData.version = "1.2"; initData.copyright = "(c) 2022 Ken"; // initData.url = "https://resource.dopus.com/c/buttons-scripts/16"; initData.desc = "atbl, settag"; initData.default_enable = true; initData.min_version = "12.0"; } function OnAddCommands(addCmdData) { var cmd = addCmdData.AddCommand(); cmd.name = "atbl"; cmd.method = "Onatbl"; cmd.desc = "Add Tags by List"; cmd.label = "atbl"; var cmd2 = addCmdData.AddCommand(); cmd2.name = "settag"; cmd2.method = "OnSetTag"; cmd2.desc = "Set Tags"; cmd2.label = "settag"; cmd.template = ""; cmd.hide = false; cmd.icon = 'script'; } // Add string trim() method String.prototype.trim = function(s){s=s||"\\s";return this.replace(new RegExp("^("+s+"){1,}\|("+s+"){1,}$","g"),"");} var fsu = DOpus.FSUtil; var ST = DOpus.Create.StringTools(); var cmd = DOpus.Create().Command(); function OnSetTag(scriptCmdData) { InitTags(); var dlgEdit = scriptCmdData.func.Dlg(); var tab = scriptCmdData.func.sourcetab; var cmd = scriptCmdData.func.command; dlgEdit.title = "Directory Opus"; dlgEdit.template = "dlgSetTags"; dlgEdit.detach = true; dlgEdit.Create(); dlgEdit.Control("static").style = "b"; dlgEdit.Control("static").label = 'Set Tags'; // Topic dlgEdit.Control("static2").label = 'One tag per line, or enter new tags separated by commas.'; // Notes dlgEdit.Control("static3").label = Script.LoadImage("Label.png"); // Icon var ADS = tab.vars.Get('ADScheck'); dlgEdit.Control("SetTags").value = ADS; // Default value dlgEdit.Show(); var newTags, tagsTMP = "|"; // msg loop do { msg = dlgEdit.GetMsg(); if (msg.qualifiers == "none") {tagsTMP = dlgEdit.Control("SetTags").value}; // Result if (dlgEdit.result == 1 || msg.qualifiers == "ctrl" && msg.event == "editchange") { if (msg.qualifiers == "ctrl" && msg.event == "editchange") { dlgEdit.EndDlg; if (tagsTMP != "|") { newTags = tagsTMP } else {newTags = ADS} } else { newTags = dlgEdit.Control("SetTags").value } newTags = format_tags(newTags); newTags = remove_duplicate(newTags); // Add the new tags to the selected items and restore the timestamp cmd.deselect = false; var modified, ADSfile, fileWrite, eItem, text, text2, textbom, myArray, ADS = "", ADS1 = "", ADS2 = "", ADS3 = ""; for (var eSel = new Enumerator(tab.selected); !eSel.atEnd(); eSel.moveNext()) { eItem = eSel.item(); modified = eItem.modify; ADSfile = eItem + ":Tags"; // If ADSfile exists, get ADS if (fsu.Exists(ADSfile)) { text = fsu.OpenFile(ADSfile).Read(); // Read file // Unlock the file text2 = fsu.OpenFile(ADSfile, "r"); text2.Close; textbom = ST.Decode(text, "utf-8 bom"); // Decode as UTF-8 with BOM if (textbom.trim() == "") {ADS = newTags}; else { myArray = textbom.split("\r\n"); // The seprator is \r\n ADS2 = myArray[1]; // Get the second line ADS3 = myArray[2]; // Get the third line ADS = newTags + "\r\n" + ADS2 + "\r\n" + ADS3; } } else {ADS = newTags} if (ADS == "") { fileWrite = fsu.OpenFile(ADSfile,"d"); //fileWrite = fsu.GetItem(ADSfile).Open("d"); fileWrite.Close; } else { fileWrite = fsu.OpenFile(ADSfile, "wa"); if (fileWrite.error == 0) { fileWrite.Write(ADS); } fileWrite.Close; } modified = modified.Format("D#yyyy-MM-dd T#HH:mm:ss") //cmd.Runcommand('SetAttr MODIFIED "'+ modified +'"') cmd.RunCommand ('SetAttr "' + eItem + '" Meta "lastmodifieddate:' + modified + '"') } cmd.Runcommand("RefreshColumnADS"); // Refresh column if (newTags == "") return; var array = newTags.split(", "); var tags = tab.vars.Get('DOpusTag'); var tagsArray = tags.split(", "); // Seprator var newTags2 = "", tagCase; // Remove Duplicate for (i = 0; array[i]; i++) { tagCase = ""; for (ii = 0; tagsArray[ii]; ii++) { if (array[i] == tagsArray[ii]) { tagCase = 1; } } if (tagCase != 1) { newTags2 += array[i] + ", "; } } if (newTags2 == "") return; var newTags3 = newTags2 + tags; tab.vars.Set('DOpusTag', newTags3); // Write new tags to DOpusTag.dat var tagFile = "%appdata%\\GPSoftware\\Directory Opus\\Scripts\\INI\\DOpusTag.dat"; // File path var oFileWrite = fsu.OpenFile(tagFile, "wa"); var utf8bom = ST.Encode(newTags3, "utf-8 bom"); // Decode as UTF-8 with BOM if (oFileWrite.error == 0) { oFileWrite.Write(utf8bom) } oFileWrite.Close() } } while (msg == true) } var tab = DOpus.listers.lastactive.activetab; function Onatbl(scriptCmdData) { var dlg = scriptCmdData.func.Dlg(); var tab = scriptCmdData.func.sourcetab; var cmd = scriptCmdData.func.command; var strName; dlg.title = "Directory Opus"; dlg.template = "dlgList"; dlg.detach = true; dlg.Create(); dlg.Control("static").style = "b"; dlg.Control("static").label = "Add Tags by List"; // Topic dlg.Control("static2").label = "Tick the tags that shall be added to the tags of the selected items."; // Notes dlg.Control("static3").label = Script.LoadImage("Label.png"); // Icon dlg.Control("static4").label = Script.LoadImage("Find.png"); // Icon //dlg.Control("static5").label = "D:\\Icons\\Clear.png"; // Clear updateList(dlg, strName); dlg.Show(); var lst = dlg.Control("list"); var col = lst.columns; while (true) { var msg = dlg.GetMsg(); if (!msg.result) break; // Result if (msg.event == "focus" || dlg.Control("btnOK").focus == true) { var tags = ""; for (i=0; i < lst.count; i ++) { if (lst.GetItemAt(i).checked == 1) { tags += lst.GetItemAt(i).name; tags += ", "; } } } // Right click list menu if (msg.event == "rclick" && lst.focus == true) { var dlgMenu = scriptCmdData.func.Dlg(); if (lst.value.index > -1) { dlgMenu.choices = DOpus.Create.Vector("New", "Edit", "Delete", "Clear Filter", "Refresh", "Copy", "Cancel"); dlgMenu.menu = DOpus.Create.Vector(1, 0, 0, 0, 0, 0, 1); } else { dlgMenu.choices = DOpus.Create.Vector("OK", "Cancel"); dlgMenu.menu = DOpus.Create.Vector(1, 1); } var menuReturn = dlgMenu.Show; if (menuReturn == 1) { NewTags(scriptCmdData); updateList(dlg, strName); } if (menuReturn == 2) { var editTag = lst.GetItemAt(lst.value.index).name; tab.vars.Set('editTag', editTag); EditTag(scriptCmdData); updateList(dlg, strName); } if (menuReturn == 3) { var deleteTag = lst.GetItemAt(lst.value.index).name; tab.vars.Set('deleteTag', deleteTag); DeleteTag(scriptCmdData); updateList(dlg, strName); } if (menuReturn == 4) { dlg.Control("editFilter").value = ""; updateList(dlg, strName); } if (menuReturn == 5) { updateList(dlg, strName); } if (menuReturn == 6) { var item = lst.GetItemAt(lst.value.index).name; DOpus.SetClip(item); } } // Filter if (msg.event == "editchange" || msg.event == "click") { updateList(dlg, strName); } } // Add the checked tags to the selected items and restore the timestamp if (dlg.result == 1) { tags = tags.substr(0, tags.length-2); cmd.deselect = false; var modified, ADSfile, fileWrite, eItem, text, text2, textbom, myArray, ADS = "", ADS1 = "", ADS2 = "", ADS3 = ""; for (var eSel = new Enumerator(tab.selected); !eSel.atEnd(); eSel.moveNext()) { eItem = eSel.item(); modified = eItem.modify; ADSfile = eItem + ":Tags"; // If ADSfile exists, get ADS if (fsu.Exists(ADSfile)) { text = fsu.OpenFile(ADSfile).Read(); // Read file // Unlock the file text2 = fsu.OpenFile(ADSfile, "w"); text2.Close; textbom = ST.Decode(text, "utf-8 bom"); // Decode as UTF-8 with BOM if (textbom.trim() == "") {ADS = tags}; else { myArray = textbom.split("\r\n"); // The seprator is \r\n ADS2 = myArray[1]; // Get the second line ADS3 = myArray[2]; // Get the third line ADS = tags + "\r\n" + ADS2 + "\r\n" + ADS3; } } if (ADS == "") { fileWrite = fsu.OpenFile(ADSfile, "d"); fileWrite.Close; } else { fileWrite = fsu.OpenFile(ADSfile, "wa"); if (fileWrite.error == 0) { fileWrite.Write(ADS); } fileWrite.Close; } modified = modified.Format("D#yyyy-MM-dd T#HH:mm:ss") //cmd.Runcommand('SetAttr MODIFIED "'+ modified +'"') cmd.RunCommand ('SetAttr "' + eItem + '" Meta "lastmodifieddate:' + modified + '"') } cmd.Runcommand("RefreshColumnADS"); // Refresh column } } function updateList(dlg, strName) { if (!DOpus.version.AtLeast("12.30.1")) { dlg.request("This script requires 12.30.1 and above!", "OK"); return } InitTags(); var lst = dlg.Control("list"); var col = lst.columns; // Add columns var vecOrder = col.GetDisplayOrder(); if (vecOrder.size < 1) { col.AddColumn("0:Name"); col.AddColumn("1:Index"); } lst.redraw = false; lst.RemoveItem(-1); // List items //var text = "tag1, tag2, tag3, tag4, tag5, tag6, tag7, tag8, tag9, tag10, tag11, tag12, tag13, tag14, tag15, tag16, tag17, tag18, tag19, tag20, tag21, tag22, tag23, tag24, tag25, tag26, tag27, tag28, tag29, tag30, tag31, tag32, tag33, tag34, tag35"; var text = tab.vars.Get('DOpusTag'); var myArray = text.split(", "); // Seprator var idx; var count = 0; var ii = 0; // Get var ADScheck var ADS = tab.vars.Get('ADScheck'); // Add checked list if (ADS != "") { var array = ADS.split(", "); // Seprator for (i = 0; array[i]; i++) { count++; var checkedTag = array[i]; var strFilter = dlg.Control("editFilter").value; // Get filter value strFilter = strFilter.toLowerCase(); if (checkedTag.search(strFilter) == -1) { continue; } idx = lst.AddItem(array[i]); // Add each item to list lst.GetItemAt(idx).subitems(0) = count; // Add number to column 1 (Index) lst.GetItemAt(idx).checked = 1; ii++; var checkedCase = 1; checkedTag = ""; strFilter = ""; } } // Add list var tagCase; for (i = 0; myArray[i]; i++) { strName = myArray[i]; var strRealName = myArray[i]; // Remove Duplicate tagCase = ""; if (checkedCase == 1) { for (ii = 0; array[ii]; ii++) { if (array[ii] == strName) { tagCase = 1; } } } if (tagCase == "") { count++; var strFilter = dlg.Control("editFilter").value; // Get filter value strFilter = strFilter.toLowerCase(); if (strRealName.search(strFilter) == -1) { continue; } idx = lst.AddItem(strName); // Add each item to list lst.GetItemAt(idx).subitems(0) = count; // Add number to column 1 (Index) ii++; strRealName = ""; strFilter = ""; } } // Set the columns display order if (true) { col.SetDisplayOrder(1,0); } // Resize columns based on number of lists //col.AutoSize(); col.GetColumnAt(1).width = String(i).length+25; if (ii > 27) { col.GetColumnAt(0).width = 420-String(i).length; } else { col.GetColumnAt(0).width = 437-String(i).length; } lst.redraw = true; } function InitTags() { var tab = DOpus.listers.lastactive.activetab; // Create folder if does not exist if (!DOpus.FSUtil.Exists("%appdata%\\GPSoftware\\Directory Opus\\Scripts\\INI\\")) { cmd.Runcommand('CreateFolder "%appdata%\\GPSoftware\\Directory Opus\\Scripts\\INI\\"'); } // Get ADS of the selected items var eItem, ADSfile, text, textbom, myArray, ADS2, ADS = "", i = 0; for (var eSel = new Enumerator(tab.selected); !eSel.atEnd(); eSel.moveNext()) { i++; ADS2 = ADS; eItem = eSel.item(); ADSfile = eItem + ":Tags"; // If ADSfile exists if (fsu.Exists(ADSfile)) { text = fsu.OpenFile(ADSfile).Read(); // Read file // Unlock the file text2 = fsu.OpenFile(ADSfile, "r"); text2.Close; textbom = ST.Decode(text, "utf-8 bom"); // Decode as UTF-8 with BOM myArray = textbom.split("\r\n"); // The seprator is \r\n ADS = myArray[0]; // Get the first line } else {ADS = ""; break} if (i > 1 && ADS != ADS2) {ADS = ""; break}; } ADS = format_tags(ADS); tab.vars.Set('ADScheck', ADS); // Read the tag file DOpusTag.dat and set it to variable DOpusTag var tagFile = "%appdata%\\GPSoftware\\Directory Opus\\Scripts\\INI\\DOpusTag.dat"; // File path if (DOpus.FSUtil.Exists(tagFile)) { var tags = fsu.OpenFile(tagFile).Read(); tags = ST.Decode(tags, "utf-8 bom"); } else {var tags = "";} tab.vars.Set('DOpusTag', tags); } function NewTags(scriptCmdData) { var dlgEdit = scriptCmdData.func.Dlg(); var tab = scriptCmdData.func.sourcetab; dlgEdit.title = "Directory Opus"; dlgEdit.template = "dlgEditTags"; dlgEdit.detach = true; dlgEdit.Create(); dlgEdit.Control("static").style = "b"; dlgEdit.Control("static").label = 'Enter new Tags'; // Topic dlgEdit.Control("static2").label = 'One tag per line, or enter new tags separated by commas.'; // Notes dlgEdit.Control("static3").label = Script.LoadImage("Edit-new.png"); // Icon dlgEdit.Control("editTags").value = ""; // Default value dlgEdit.Show(); var newTags, tagsTMP = "|", tagCase; // msg loop do { msg = dlgEdit.GetMsg(); if (msg.qualifiers == "none") {tagsTMP = dlgEdit.Control("editTags").value}; // Result if (dlgEdit.result == 1 || msg.qualifiers == "ctrl" && msg.event == "editchange") { if (msg.qualifiers == "ctrl" && msg.event == "editchange") { dlgEdit.EndDlg; if (tagsTMP != "|") { newTags = tagsTMP } else {newTags = ""} } else { newTags = dlgEdit.Control("editTags").value } if (newTags.trim() == "") {return;} newTags = format_tags(newTags); newTags = remove_duplicate(newTags); var array = newTags.split(", "); // Seprator var tags = tab.vars.Get('DOpusTag'); var tagsArray = tags.split(", "); // Seprator // Remove Duplicate newTags = ""; for (i = 0; array[i]; i++) { tagCase = ""; for (ii = 0; tagsArray[ii]; ii++) { if (array[i] == tagsArray[ii]) { tagCase = 1; } } if (tagCase != 1) { newTags += array[i] + ", "; } } if (newTags == "") {return}; var newTags2 = newTags + tags; tab.vars.Set('DOpusTag', newTags2); // Write new tags to DOpusTag.dat var tagFile = "%appdata%\\GPSoftware\\Directory Opus\\Scripts\\INI\\DOpusTag.dat"; // File path var oFileWrite = fsu.OpenFile(tagFile, "wa"); var utf8bom = ST.Encode(newTags2 , "utf-8 bom"); // Decode as UTF-8 with BOM if (oFileWrite.error == 0) { oFileWrite.Write(utf8bom) } oFileWrite.Close() } } while (msg == true) } function EditTag(scriptCmdData) { var dlgEdit = scriptCmdData.func.Dlg(); var tab = scriptCmdData.func.sourcetab; dlgEdit.title = "Directory Opus"; dlgEdit.template = "dlgEditTags"; dlgEdit.detach = true; dlgEdit.Create(); dlgEdit.Control("static").style = "b"; dlgEdit.Control("static").label = 'Edit Tags'; // Topic dlgEdit.Control("static2").label = 'One tag per line, or enter new tags separated by commas.'; // Notes dlgEdit.Control("static3").label = Script.LoadImage("Edit.png"); // Icon var editTag = tab.vars.Get('editTag', editTag); dlgEdit.Control("editTags").value = editTag; // Default value dlgEdit.Show(); var newTag, tagsTMP = "|"; // msg loop do { msg = dlgEdit.GetMsg(); if (msg.qualifiers == "none") {tagsTMP = dlgEdit.Control("editTags").value}; // Result if (dlgEdit.result == 1 || msg.qualifiers == "ctrl" && msg.event == "editchange") { if (msg.qualifiers == "ctrl" && msg.event == "editchange") { dlgEdit.EndDlg; if (tagsTMP != "|") { newTag = tagsTMP } else {newTag = editTag} } else { newTag = dlgEdit.Control("editTags").value } if (newTag.trim() == "") {return;} newTag = format_tags(newTag); newTag = remove_duplicate(newTag); var tags = tab.vars.Get('DOpusTag'); // Remove duplicate var tags = tab.vars.Get('DOpusTag'); var tagsArray = newTag.split(", "); var tagsArray2 = tags.split(", "); // Seprator var tagCase, newTags = ""; for (i = 0; tagsArray2[i]; i++) { tagCase = ""; for (ii = 0; tagsArray[ii]; ii++) { if (tagsArray2[i] == tagsArray[ii]) { tagCase = 1; } } if (tagCase != 1 && tagsArray2[i] != editTag) { newTags += tagsArray2[i] + ", "; } } if (newTags == "") return; newTags = newTag + ", " + newTags; newTags = newTags.substr(0, newTags.length-2); tab.vars.Set('DOpusTag', newTags); // Write new tags to DOpusTag.dat var tagFile = "%appdata%\\GPSoftware\\Directory Opus\\Scripts\\INI\\DOpusTag.dat"; // File path var oFileWrite = fsu.OpenFile(tagFile, "wa"); var utf8bom = ST.Encode(newTags, "utf-8 bom"); // Decode as UTF-8 with BOM if (oFileWrite.error == 0) { oFileWrite.Write(utf8bom) } oFileWrite.Close() } } while (msg == true) } function DeleteTag(scriptCmdData) { var tab = scriptCmdData.func.sourcetab; var deleteTag = tab.vars.Get('deleteTag'); if (deleteTag == "") return; var tags = tab.vars.Get('DOpusTag'); var tagsArray = tags.split(", "); // Seprator var newTags = ""; for (i = 0; tagsArray[i]; i++) { if (tagsArray[i] != deleteTag) { newTags += tagsArray[i] + ", "; } } newTags = newTags.substr(0, newTags.length-2) tab.vars.Set('DOpusTag', newTags); // Write new tags to DOpusTag.dat var tagFile = "%appdata%\\GPSoftware\\Directory Opus\\Scripts\\INI\\DOpusTag.dat"; // File path var oFileWrite = fsu.OpenFile(tagFile, "wa"); var utf8bom = ST.Encode(newTags, "utf-8 bom"); // Decode as UTF-8 with BOM if (oFileWrite.error == 0) { oFileWrite.Write(utf8bom) } oFileWrite.Close() } function format_tags(str) { var array = String(str).split(","); // Seprator var newTag1 = array[0].trim().replace(/\|/g,''); if (newTag1 == "") {return newTag1}; var tag, newTags = ""; for (i = 0; array[i]; i++) { tag = array[i].trim().replace(/\|/g,''); if (tag != "") { newTags += tag + ", "; } } newTags = newTags.substr(0, newTags.length-2); var array2 = newTags.split("\r\n"); // Seprator newTags = ""; for (i = 0; array2[i]; i++) { tag = array2[i].trim().replace(/\|/g,''); if (tag != "") { newTags += tag + ", "; } } newTags = newTags.substr(0, newTags.length-2); return newTags; } function remove_duplicate(str) { var array = String(str).split(", "); // Seprator var newTag1 = array[0].trim().replace(/\|/g,''); if (newTag1 == "") {return newTag1}; var vecTags = DOpus.NewVector(); for (i=0; array[i]; i++) { vecTags.push_back = array[i]; } var removedNum = vecTags.unique(); if (removedNum == 0) {return String(str)}; var newTags = ""; for (var eItems = new Enumerator(vecTags); !eItems.atEnd(); eItems.moveNext() ) { newTags += eItems.item() + ", "; } newTags = newTags.substr(0, newTags.length-2); return newTags; } ==SCRIPT RESOURCES