Processing Ajax...

Title

Message

Confirm

Confirm

Confirm

Confirm

Are you sure you want to delete this item?

Confirm

Are you sure you want to delete this item?

Confirm

Are you sure?

User Image
ChrisL
2 discussion posts
I've created this macro which appears to work fine in the macro editor

[size=10pt]using System;

public static class ClipboardFusionHelper
{
public static string ProcessText(string text)
{
string[] lines = text.Split( new string[] {"\n"} , StringSplitOptions.RemoveEmptyEntries);

text = "";

foreach (string line in lines)
{
text += line + "\n";
}
return text;
}
}[/size]

using the "Test Macro" button but it doesn't appear to do anything to the clipboard when run from tray icon. I've tried the beta release and it's the same.

I've tried adding "system.collections.generic;" and "system.text;" but that didn't change the results.

- am I still missing something I should be importing?

Chris
Jan 27, 2011  • #1
User Image
Splat
21 discussion posts
The problem could be that, depending on your text (and OS?), line breaks can either be "\n", "\r", "\r\n", or "\n\r". Your code only looks for "\n", so if your text also has "/r" as part of the line break, then you code leaves these in, and your output text can still appear to have line breaks.

Try this code instead:

Code

using System; 
 
public static class ClipboardFusionHelper 
{ 
    public static string ProcessText(string text) 
    { 
        string[] lines = text.Split( new string[] {"\r", "\n"} , StringSplitOptions.RemoveEmptyEntries);

        text = "";

        foreach (string line in lines)
        {
            text += line + "\r\n";
        }
     return text;
    }
}


This page might help you out : http://dotnetperls.com/string-split

I have a macro that I have been using that does something similar, but uses Regular Expressions:

Code

using System;
using System.Text.RegularExpressions;

public static class ClipboardFusionHelper
{
  public static string ProcessText(string text)
  {

    // Name:        Clear Blank Lines
    // Description:Clears white space (spaces and tabs) from any line that ONLY contains white space.  Optionally removes the empty lines too.

    bool bolRemoveEmptyLines = true;  // Removes empty lines

    text = Regex.Replace(text, @"^[ \t]+(?[\r\n]{1,2})+", "${linebreak}", RegexOptions.Multiline);

    if (bolRemoveEmptyLines) {
      text = Regex.Replace(text, @"([\r\n]{1,2})+", "\r\n", RegexOptions.Multiline);
      }

    MacroAPI.PasteText(text);  // Optional.  Pastes the text straight away.  Useful when a HotKey is used.
    return text;
  }
}


Using RegEx, it looks for one or both "\r" and "\n" and removes them.

If you just want to clear the empty lines (and not worry about the whitespace), you can just use the last RegEx in the code above.

I haven't fully tested this, but it seems to work well for me. Let me know if you have any trouble.
Jan 27, 2011  • #2
User Image
ChrisL
2 discussion posts
Hi Splat

you're right it does appear to be the line endings that are different between the "Test Macro" option and what's on the clipboard - I did look for "\n\r" but I didn't try it the way you had the Split function, i.e. the {"\n", "\r"}.

I get SQL statements via email - they look ok in Outlook but when pasted into notepad or wherever they have extra lines in between but this looks like it cures that particular issue.

I'll take a look at the RegEx stuff - that's really useful, especially as there are so many pre-tested regex examples out there.

thanks again

Chris
Jan 28, 2011  • #3
Jon Tackabury (BFS)'s profile on WallpaperFusion.com
@Splat: Thanks for jumping in and helping out. :)
Feb 4, 2011  • #4
Subscribe to this discussion topic using RSS
Was this helpful?  Login to Vote(-)  Login to Vote(-)