Jump to content
Macro Express Forums

Modifying lines of text in clipboard


pepperpod

Recommended Posts

I'm looking at using MEX to add some html tags to the beginning and end of lines of text copied to the clipboard. I copy 3 lines of text to the clipboard and want to add one html tag to the beginning/end of the first line, another to the beginning/end of the second line and yet another to the third line. Can I do this directly on the clipboard contents or do I have to first save the clipboard to a file and then use something like the sample macro 'process text file'. Ideally I'd like each line of text in the clipboard saved as a variable. Any help here much appreciated as I'm a MEX novice.

 

P

Link to comment
Share on other sites

Can I do this directly on the clipboard contents ...
No.

 

do I have to first save the clipboard to a file and then use something like the sample macro 'process text file'.
You could save the clipboard to a file but it would not be very efficient. A better approach would be to copy the content of the clipboard to a variable, modify the variable, and save it back to the clipboard. This technique was discussed in some recent posts: Parsing and More parsing.

 

To parse the three lines of the clipboard into three separate variables you will need to search for the end of each line. Each line ends in CRLF. Read the post can I take a string variable and replace returns in it with spaces? to learn how to search for CRLF in a string. In your case, you will search for the CRLF but not replace them with spaces as discussed in that post but it will show how a search for CRLF is done.

 

In addition you should read the Macro Express Help to understand these commands:

  • Variable Set String %T1% from Clipboard
  • Variable Set Integer %N1% from Position of Text in Variable %T1%
  • Variable Modify String: Copy Part of %T1% to %T2%
  • Variable Modify String: Save %T1% to Clipboard

Link to comment
Share on other sites

I'm afraid I'm struggling! I understand how to set a single variable for CRLF but not how to search for that variable in a string. The 'Variable Set Integer %N1% from Position of Text in Variable %T1%' appears to require some specific text to search for, not a variable. Where am I going wrong?

 

P

Link to comment
Share on other sites

Many thanks Kevin. You've given me enough pointers to work through this myself and it will be an excellent learning exercise. I doubt I'd have been able to do it on my own.

 

P

 

The following is probably not the most elegant solution but it does the job:

 

// Add tags to 3 lines (for pepperpod)
Text Box Display: Opening message
Variable Set String %T1% from Clipboard
// Set a variable for a CR (EOL)
Variable Set %T2% to ASCII Char of 13
// Set variables for the 6 tags needed
Variable Set String %T21% "[LINE1TAG1]"
Variable Set String %T22% "[\LINE1TAG]"
Variable Set String %T23% "[LINE2TAG]"
Variable Set String %T24% "[\LINE2TAG]"
Variable Set String %T25% "[LINE3TAG]"
Variable Set String %T26% "[\LINE3TAG]"
// Create Line 1 in T4
Variable Set String %T3% ""
Variable Set Integer %N1% to 1
Variable Set String %T4% ""
Repeat Until %T3% = %T2%
 Variable Modify String: Copy Part of %T1% to %T3%
 Variable Modify String: Append %T3% to %T4%
 Variable Modify Integer: %N1% = %N1% + 1
Repeat End
// Remove unwanted final CR from T4
Variable Modify String: Strip CR/LF from %T4%
// Create Line 2 in T5
Variable Set String %T5% ""
Variable Modify Integer: %N1% = %N1% + 1
Repeat Until %T3% = %T2%
 Variable Modify String: Copy Part of %T1% to %T3%
 Variable Modify String: Append %T3% to %T5%
 Variable Modify Integer: %N1% = %N1% + 1
Repeat End
// Remove unwanted final CR from T5
Variable Modify String: Strip CR/LF from %T5%
// Create Line 3 in T6
Variable Set String %T6% ""
Variable Modify Integer: %N1% = %N1% + 1
Repeat Until %T3% = %T2%
 Variable Modify String: Copy Part of %T1% to %T3%
 Variable Modify String: Append %T3% to %T6%
 Variable Modify Integer: %N1% = %N1% + 1
Repeat End
// Remove unwanted final CR from T6
Variable Modify String: Strip CR/LF from %T6%
// Add tags
Variable Modify String: Append %T4% to %T21%
Variable Modify String: Append %T22% to %T21%
Variable Set String %T4% "%T21%"

Variable Modify String: Append %T5% to %T23%
Variable Modify String: Append %T24% to %T23%
Variable Set String %T5% "%T23%"

Variable Modify String: Append %T6% to %T25%
Variable Modify String: Append %T26% to %T25%
Variable Set String %T6% "%T25%"
Text Box Display: Tagged values
Delay 100 Milliseconds
Macro Return

 

And here's the Direct Editor output if you're impatient to run it <_<

 

<REM2:Add tags to 3 lines (for pepperpod)><TBOX4:T:4:000671Center000428000200:000:Opening messageThis assumes you already have 3 lines of text copied from somewhere onto the clipboard. If not, do so now.

Click OK to run the macro.><TVAR2:01:03:><REM2:Set a variable for a CR (EOL)><ASCIIC:2:1:13><REM2:Set variables for the 6 tags needed><TVAR2:21:01:[LINE1TAG1]><TVAR2:22:01:[\LINE1TAG]><TVAR2:23:01:[LINE2TAG]><TVAR2:24:01:[\LINE2TAG]><TVAR2:25:01:[LINE3TAG]><TVAR2:26:01:[\LINE3TAG]><REM2:Create Line 1 in T4><TVAR2:03:01:><IVAR2:01:01:1><TVAR2:04:01:><REP3:08:000001:000001:0003:1:01:T2><TMVAR2:10:03:01:N01:001:><TMVAR2:08:04:03:000:000:><NMVAR:01:01:1:0000001:2:0000001><ENDREP><REM2:Remove unwanted final CR from T4><TMVAR2:04:04:00:000:000:><REM2:Create Line 2 in T5><TVAR2:05:01:><NMVAR:01:01:1:0000001:2:0000001><REP3:08:000001:000001:0003:1:01:T2><TMVAR2:10:03:01:N01:001:><TMVAR2:08:05:03:000:000:><NMVAR:01:01:1:0000001:2:0000001><ENDREP><REM2:Remove unwanted final CR from T5><TMVAR2:04:05:00:000:000:><REM2:Create Line 3 in T6><TVAR2:06:01:><NMVAR:01:01:1:0000001:2:0000001><REP3:08:000001:000001:0003:1:01:T2><TMVAR2:10:03:01:N01:001:><TMVAR2:08:06:03:000:000:><NMVAR:01:01:1:0000001:2:0000001><ENDREP><REM2:Remove unwanted final CR from T6><TMVAR2:04:06:00:000:000:><REM2:Add tags><TMVAR2:08:21:04:000:000:><TMVAR2:08:21:22:000:000:><TVAR2:04:01:%T21%><REM2:><TMVAR2:08:23:05:000:000:><TMVAR2:08:23:24:000:000:><TVAR2:05:01:%T23%><REM2:><TMVAR2:08:25:06:000:000:><TMVAR2:08:25:26:000:000:><TVAR2:06:01:%T25%><TBOX4:T:4:001039000863000565000242:000:Tagged valuesT1 = Original clipboard =
%T1%

T4 = Tagged Line 1 = %T4%
T5 = Tagged Line 2 = %T5%
T6 = Tagged Line 3 = %T6%><MSD:100><MRETURN>

 

Finally, here's an example of the output (in a message box):

 

T1 = Original clipboard =

Text line 1

A second line

And a third final line of text

 

 

T4 = Tagged Line 1 = [LINE1TAG1]Text line 1[\LINE1TAG]

T5 = Tagged Line 2 = [LINE2TAG]A second line[\LINE2TAG]

T6 = Tagged Line 3 = [LINE3TAG]And a third final line of text[\LINE3TAG]

 

 

--

Terry, East Grinstead, UK

Link to comment
Share on other sites

Terry, that's really very kind of you to go to so much trouble. I appreciate it very much. As you can imagine I tried it straight away but unfortunately when I ran it, it just hung. Still, I'll study your script in detail and hopefully learn from it.

 

Maybe an example of what I'm hoping to do will help. I have 3 lines on the clipboard, such as:

 

Joe Bloggs

user.name@isp.com

This is some descriptive text

 

I want to convert it to:

 

<strong>Joe Bloggs</strong>

<a href="mailto:user.name@isp.com">user.name@isp.com</a>

<em>This is some descriptive text</em>

 

As you see in line 2, I've used the contents of that line twice, hence the reason I thought saving each line to a variable would be helpful to me.

 

I wrote a script to do the above in AutoHotKey, but I do so much text processing in MEX that I thought it would make sense to use MEX for this too.

 

Thanks again,

 

P

Link to comment
Share on other sites

Terry, that's really very kind of you to go to so much trouble. I appreciate it very much. As you can imagine I tried it straight away but unfortunately when I ran it, it just hung. Still, I'll study your script in detail and hopefully learn from it.

 

Maybe an example of what I'm hoping to do will help. I have 3 lines on the clipboard, such as:

 

Joe Bloggs

user.name@isp.com

This is some descriptive text

 

I want to convert it to:

 

<strong>Joe Bloggs</strong>

<a href="mailto:user.name@isp.com">user.name@isp.com</a>

<em>This is some descriptive text</em>

 

As you see in line 2, I've used the contents of that line twice, hence the reason I thought saving each line to a variable would be helpful to me.

 

I wrote a script to do the above in AutoHotKey, but I do so much text processing in MEX that I thought it would make sense to use MEX for this too.

 

Thanks again,

 

P

 

That's odd, it was working fine here. Your example would just need the 6 tags editing. Maybe there was some corruption during my cutting/pasting, or I screwed up the post somehow. Hope you manage to get it working. I'm packing in for the night here, but may have some time to look at it again tomorrow.

 

--

Terry, East Grinstead, UK

Link to comment
Share on other sites

My apologies Terry. Your script works perfectly if I have a CRLF at the end of the 3rd line. When I was copying 3 lines of text to the clipboard I highlighted the 3 lines to copy, and of course the last line didn't contain CRLF. All's well now. Many thanks indeed.

 

P

 

Pleased to hear that!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...