Ditto icon indicating copy to clipboard operation
Ditto copied to clipboard

Feature request : suggested On Paste Scripts : Provide repository for sharing custom paste modifiers

Open shodanx2 opened this issue 1 year ago • 11 comments

Here is a common problem

There are 3 mac address formats

linux 90:E2:BA:10:1A:BE

microsoft 90-E2-BA-10-1A-BE

cisco 90E2.BA10.1ABE

So, I tried making a script to output any number of copied MACs in any format, to any format that you want

Here is a preliminary attempt

To Cisco format

// Get the current clipboard text assuming it's a MAC address
var macAddress = clip.Text;

// Remove any non-alphanumeric characters (like colons, hyphens, etc.)
var cleanMac = macAddress.replace(/[^a-zA-Z0-9]/g, '');

// Check if the cleaned MAC address has the expected length of 12 characters
if(cleanMac.length == 12) {
    // Insert a period every 4 characters
    var ciscoFormatMac = cleanMac.substring(0, 4) + '.' + cleanMac.substring(4, 8) + '.' + cleanMac.substring(8, 12);

    // Set the modified MAC address back to the clipboard
    clip.SetText(ciscoFormatMac);
} else {
    // If the MAC address is not of the expected length, log an error or handle it appropriately
    clip.SetText("Error: MAC address format incorrect");
}

return false; // Prevent the original text from being pasted

To microsoft format

// Get the current clipboard text assuming it's a MAC address
var macAddress = clip.Text;

// Remove any non-alphanumeric characters (like colons, periods, etc.)
var cleanMac = macAddress.replace(/[^a-zA-Z0-9]/g, '');

// Check if the cleaned MAC address has the expected length of 12 characters
if (cleanMac.length == 12) {
    // Insert a hyphen every 2 characters
    var microsoftFormatMac = cleanMac.replace(/(.{2})/g, '$1-').slice(0, -1);

    // Set the modified MAC address back to the clipboard
    clip.SetText(microsoftFormatMac);
} else {
    // If the MAC address is not of the expected length, log an error or handle it appropriately
    clip.SetText("Error: MAC address format incorrect");
}

return false; // Prevent the original text from being pasted

To linux format

// Get the current clipboard text assuming it's a MAC address
var macAddress = clip.Text;

// Remove any non-alphanumeric characters (like hyphens, periods, etc.)
var cleanMac = macAddress.replace(/[^a-zA-Z0-9]/g, '');

// Check if the cleaned MAC address has the expected length of 12 characters
if (cleanMac.length == 12) {
    // Insert a colon every 2 characters and convert to lowercase
    var linuxFormatMac = cleanMac.replace(/(.{2})/g, '$1:').slice(0, -1).toLowerCase();

    // Set the modified MAC address back to the clipboard
    clip.SetText(linuxFormatMac);
} else {
    // If the MAC address is not of the expected length, log an error or handle it appropriately
    clip.SetText("Error: MAC address format incorrect");
}

return false; // Prevent the original text from being pasted

Note these don't currently work due to syntax error.

Also they would only convert clips with one mac address each, not whole lists

shodanx2 avatar Jan 28 '24 23:01 shodanx2

You will need the latest build, fixed an issue with ReplaceRegex, https://github.com/sabrogden/Ditto/releases/tag/nightly

Cisco

// Remove any non-alphanumeric characters (like colons, hyphens, etc.)
clip.AsciiTextReplaceRegex("[^a-zA-Z0-9]", "");

// Insert a hyphen every 2 characters
clip.AsciiTextReplaceRegex("(.{4})", "$1.");

var text = clip.GetAsciiString();
if(text.size == 15) 
{
    //remove the - at the end of the string
    var text = clip.GetAsciiString();
    var clean = text.substr(0, 14);

    // Set the modified MAC address back to the clipboard
    clip.SetAsciiString(clean);
}
else
{
    // If the MAC address is not of the expected length, log an error or handle it appropriately
    clip.SetAsciiString("Error: MAC address format incorrect");
}

return false;

Microsoft

// Remove any non-alphanumeric characters (like colons, hyphens, etc.)
clip.AsciiTextReplaceRegex("[^a-zA-Z0-9]", "");

// Insert a hyphen every 2 characters
clip.AsciiTextReplaceRegex("(.{2})", "$1-");

var text = clip.GetAsciiString();
if(text.size == 18) 
{
    //remove the - at the end of the string
    var text = clip.GetAsciiString();
    var clean = text.substr(0, 17);

    // Set the modified MAC address back to the clipboard
    clip.SetAsciiString(clean);
}
else
{
    // If the MAC address is not of the expected length, log an error or handle it appropriately
    clip.SetAsciiString("Error: MAC address format incorrect");
}

return false;

Linux

// Remove any non-alphanumeric characters (like colons, hyphens, etc.)
clip.AsciiTextReplaceRegex("[^a-zA-Z0-9]", "");

// Insert a hyphen every 2 characters
clip.AsciiTextReplaceRegex("(.{2})", "$1:");

var text = clip.GetAsciiString();
if(text.size == 18) 
{
    //remove the - at the end of the string
    var text = clip.GetAsciiString();
    var clean = text.substr(0, 17);

    // Set the modified MAC address back to the clipboard
    clip.SetAsciiString(clean);
}
else
{
    // If the MAC address is not of the expected length, log an error or handle it appropriately
    clip.SetAsciiString("Error: MAC address format incorrect");
}

return false;

sabrogden avatar Jan 29 '24 02:01 sabrogden

Cool

Just gave this a try

to cisco works, however to linux & ms does not

here is test data

from cisco 90E2.BA10.1ABE to cisco 90E2.BA10.1ABE to linux 90E2.BA10.1ABE to ms 90E2.BA10.1ABE

from ms 90-E2-BA-10-1A-BE to cisco 90E2.BA10.1ABE to linux 90-E2-BA-10-1A-BE to ms 90-E2-BA-10-1A-BE

from linux 90:E2:BA:10:1A:BE to cisco 90E2.BA10.1ABE to linux 90:E2:BA:10:1A:BE to ms 90:E2:BA:10:1A:BE

Perhaps I pasted it wrong ?

Running the nightly you suggested

image image image

shodanx2 avatar Jan 29 '24 10:01 shodanx2

Microsoft and Linux scripts are not updated. I updated my post to format things better. Paste the mac text in the sample and click run to verify each in the script editor.

sabrogden avatar Jan 31 '24 17:01 sabrogden

Hello,

I have just tried the current on paste scripts

They work in all cases

from cisco 90E2.BA10.1ABE to cisco 90E2.BA10.1ABE to linux 90:E2:BA:10:1A:BE to ms 90-E2-BA-10-1A-BE

from ms 90-E2-BA-10-1A-BE to cisco 90E2.BA10.1ABE to linux 90:E2:BA:10:1A:BE to ms 90-E2-BA-10-1A-BE

from linux 90:E2:BA:10:1A:BE to cisco 90E2.BA10.1ABE to linux 90:E2:BA:10:1A:BE to ms 90-E2-BA-10-1A-BE

Chatgpt noticed that

var text = clip.GetAsciiString();

gets called twice and that might be unnecessary

I tested and its right

Here is updated version, confirmed to also work for single line pastes

Cisco

// Remove any non-alphanumeric characters (like colons, hyphens, etc.)
clip.AsciiTextReplaceRegex("[^a-zA-Z0-9]", "");

// Insert a hyphen every 2 characters
clip.AsciiTextReplaceRegex("(.{4})", "$1.");

var text = clip.GetAsciiString();
if(text.size == 15) 
{
    //remove the - at the end of the string
    var clean = text.substr(0, 14);

    // Set the modified MAC address back to the clipboard
    clip.SetAsciiString(clean);
}
else
{
    // If the MAC address is not of the expected length, log an error or handle it appropriately
    clip.SetAsciiString("Error: MAC address format incorrect");
}

return false;

Microsoft

// Remove any non-alphanumeric characters (like colons, hyphens, etc.)
clip.AsciiTextReplaceRegex("[^a-zA-Z0-9]", "");

// Insert a hyphen every 2 characters
clip.AsciiTextReplaceRegex("(.{2})", "$1-");

var text = clip.GetAsciiString();
if(text.size == 18) 
{
    //remove the - at the end of the string
    var clean = text.substr(0, 17);

    // Set the modified MAC address back to the clipboard
    clip.SetAsciiString(clean);
}
else
{
    // If the MAC address is not of the expected length, log an error or handle it appropriately
    clip.SetAsciiString("Error: MAC address format incorrect");
}

return false;

Linux

// Remove any non-alphanumeric characters (like colons, hyphens, etc.)
clip.AsciiTextReplaceRegex("[^a-zA-Z0-9]", "");

// Insert a hyphen every 2 characters
clip.AsciiTextReplaceRegex("(.{2})", "$1:");

var text = clip.GetAsciiString();
if(text.size == 18) 
{
    //remove the - at the end of the string
    var clean = text.substr(0, 17);

    // Set the modified MAC address back to the clipboard
    clip.SetAsciiString(clean);
}
else
{
    // If the MAC address is not of the expected length, log an error or handle it appropriately
    clip.SetAsciiString("Error: MAC address format incorrect");
}

return false;

shodanx2 avatar Feb 13 '24 08:02 shodanx2

Now I have three questions

How can I give these script in a way that they can add them easily, like, a two click operation to add all 3 ?

And, how to make these scripts multi-line

and, how to put these 3 scripts into a sub-context menu in the special paste menu as it is already getting long and presumably there are many other good paste scripts to create !

Example

image

Also also, there should be a way to re-order On Paste Script, without having to delete and re-type them

Here is what I mean https://github.com/sabrogden/Ditto/assets/10621885/25a32703-125e-4252-8993-31f671fb34c7

(Also I note, you can't use Ditto, while inside Ditto settings ! It is only because the window is unclickable due to having child windows, I think that is just a window hWnd flag ?)

shodanx2 avatar Feb 13 '24 08:02 shodanx2

Ok, I got it, multiline and single line capable version all in one

Convert MAC to Cisco format

// Function to manually remove non-alphanumeric characters and format a MAC address
def processLine(line) {
  // Manually remove non-alphanumeric characters
  var cleanedLine = "";
  for (c : line) {
    if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) {
      cleanedLine += c;
    }
  }

  // Manually insert periods to format as Cisco MAC address
  if (cleanedLine.size() == 12) {
    return cleanedLine.substr(0, 4) + "." + cleanedLine.substr(4, 4) + "." + cleanedLine.substr(8, 4);
  }

  return "Error: MAC address format incorrect"; // Error handling
}

// Get the entire clipboard content
var allText = clip.GetAsciiString();
var processedText = "";
var start = 0;

// Manually iterate over each line in allText
for (var i = 0; i <= allText.size(); ++i) {
  if (i == allText.size() || allText[i] == '\n') {
    // Extract the current line
    var line = allText.substr(start, i - start);

    // Process the current line and append to processedText
    processedText += processLine(line);

    // Add a newline if it's not the last line
    if (i != allText.size()) {
      processedText += "\n";
    }

    // Update the start index for the next line
    start = i + 1;
  }
}

// Set the modified text back to the clipboard
clip.SetAsciiString(processedText);

return false;  // Continue with the paste operation

Convert MAC to Microsoft format

// Function to manually format a MAC address in the Microsoft format
def processLineToMicrosoftFormat(line) {
  // Manually remove non-alphanumeric characters
  var cleanedLine = "";
  for (c : line) {
    if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) {
      cleanedLine += c;
    }
  }

  // Manually insert hyphens every 2 characters, except for the end
  var formattedLine = "";
  for (var i = 0; i < cleanedLine.size(); ++i) {
    formattedLine += cleanedLine[i];
    if (i % 2 == 1 && i < cleanedLine.size() - 1) { // Check to avoid adding hyphen at the end
      formattedLine += "-";
    }
  }

  // Check if the formatted line is of the correct length for a Microsoft format MAC address
  if (formattedLine.size() == 17) { // 12 characters + 5 hyphens = 17 characters
    return formattedLine;
  }

  return "Error: MAC address format incorrect"; // Error handling
}

// Get the entire clipboard content
var allText = clip.GetAsciiString();
var processedText = "";
var start = 0;

// Manually iterate over each line in allText
for (var i = 0; i <= allText.size(); ++i) {
  if (i == allText.size() || allText[i] == '\n') {
    // Extract the current line
    var line = allText.substr(start, i - start);

    // Process the current line and append to processedText
    processedText += processLineToMicrosoftFormat(line);

    // Add a newline if it's not the last line
    if (i != allText.size()) {
      processedText += "\n";
    }

    // Update the start index for the next line
    start = i + 1;
  }
}

// Set the modified text back to the clipboard
clip.SetAsciiString(processedText);

return false;  // Continue with the paste operation

Convert MAC to Linux format

// Function to manually format a MAC address in the Linux format
def processLineToLinuxFormat(line) {
  // Manually remove non-alphanumeric characters
  var cleanedLine = "";
  for (c : line) {
    if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) {
      cleanedLine += c;
    }
  }

  // Manually insert colons every 2 characters, except for the end
  var formattedLine = "";
  for (var i = 0; i < cleanedLine.size(); ++i) {
    formattedLine += cleanedLine[i];
    if (i % 2 == 1 && i < cleanedLine.size() - 1) { // Check to avoid adding colon at the end
      formattedLine += ":";
    }
  }

  // Check if the formatted line is of the correct length for a Linux format MAC address
  if (formattedLine.size() == 17) { // 12 characters + 5 colons = 17 characters
    return formattedLine;
  }

  return "Error: MAC address format incorrect"; // Error handling
}

// Get the entire clipboard content
var allText = clip.GetAsciiString();
var processedText = "";
var start = 0;

// Manually iterate over each line in allText
for (var i = 0; i <= allText.size(); ++i) {
  if (i == allText.size() || allText[i] == '\n') {
    // Extract the current line
    var line = allText.substr(start, i - start);

    // Process the current line and append to processedText
    processedText += processLineToLinuxFormat(line);

    // Add a newline if it's not the last line
    if (i != allText.size()) {
      processedText += "\n";
    }

    // Update the start index for the next line
    start = i + 1;
  }
}

// Set the modified text back to the clipboard
clip.SetAsciiString(processedText);

return false;  // Continue with the paste operation

Proof testing, multi line and single line

From cisco
0FF0.A9F5.61EE
90EA.BA10.1A77
30BF.C298.A529
50C5.C258.D59A
to cisco
0FF0.A9F5.61EE 0FF0.A9F5.61EE
90EA.BA10.1A77
30BF.C298.A529
50C5.C258.D59A
to microsoft
0F-F0-A9-F5-61-EE 0F-F0-A9-F5-61-EE
90-EA-BA-10-1A-77
30-BF-C2-98-A5-29
50-C5-C2-58-D5-9A
to linux
0F:F0:A9:F5:61:EE 0F:F0:A9:F5:61:EE
90:EA:BA:10:1A:77
30:BF:C2:98:A5:29
50:C5:C2:58:D5:9A

From microsoft
0F-F0-A9-F5-61-EE
90-EA-BA-10-1A-77
30-BF-C2-98-A5-29
50-C5-C2-58-D5-9A
to cisco
0FF0.A9F5.61EE 0FF0.A9F5.61EE
90EA.BA10.1A77
30BF.C298.A529
50C5.C258.D59A
to microsoft
0F-F0-A9-F5-61-EE 0F-F0-A9-F5-61-EE
90-EA-BA-10-1A-77
30-BF-C2-98-A5-29
50-C5-C2-58-D5-9A
to linux
0F:F0:A9:F5:61:EE 0F:F0:A9:F5:61:EE
90:EA:BA:10:1A:77
30:BF:C2:98:A5:29
50:C5:C2:58:D5:9A

From linux
0F:F0:A9:F5:61:EE
90:EA:BA:10:1A:77
30:BF:C2:98:A5:29
50:C5:C2:58:D5:9A
to cisco
0FF0.A9F5.61EE 0FF0.A9F5.61EE
90EA.BA10.1A77
30BF.C298.A529
50C5.C258.D59A
to microsoft
0F-F0-A9-F5-61-EE 0F-F0-A9-F5-61-EE
90-EA-BA-10-1A-77
30-BF-C2-98-A5-29
50-C5-C2-58-D5-9A
to linux
0F:F0:A9:F5:61:EE 0F:F0:A9:F5:61:EE
90:EA:BA:10:1A:77
30:BF:C2:98:A5:29
50:C5:C2:58:D5:9A

Here is my discussion to make this with chatgpt

https://chat.openai.com/share/569fad8b-871a-456b-abe2-310521a6d604

https://pastebin.com/Y5zdE8HC

shodanx2 avatar Feb 13 '24 09:02 shodanx2

Now, I kind of want to add all the "Line Operations" from Notepad++ to Ditto On Paste scripts !

image

shodanx2 avatar Feb 13 '24 09:02 shodanx2