Jump to content
Macro Express Forums

How can I auto-insert " and " before and after a text string?


Recommended Posts

Maybe something like this? The first two "Modify" instructions are not absolutely necessary, but could be helpful sometimes.

 

Variable Set String %Input% to "This is a test and that's all there is to it!"
Variable Modify String %Input%: Trim // Remove white space from before and after
Variable Modify String: Replace "   " in %Input% with " " // Replace double spaces with single spaces
Variable Modify String: Replace " " in %Input% with " AND " // Replace every space with " AND "
Text Box Display: Result

 

 

<VARIABLE SET STRING Option="\x00" Destination="%Input%" Value="This is a test and that's all there is to it!" NoEmbeddedVars="FALSE"/>
<VARIABLE MODIFY STRING Option="\x00" Destination="%Input%" _COMMENT="Remove white space from before and after"/>
<VARIABLE MODIFY STRING Option="\x0F" Destination="%Input%" ToReplace="   " ReplaceWith=" " All="TRUE" IgnoreCase="FALSE" NoEmbeddedVars="FALSE" _COMMENT="Replace double spaces with single spaces"/>
<VARIABLE MODIFY STRING Option="\x0F" Destination="%Input%" ToReplace=" " ReplaceWith=" AND " All="TRUE" IgnoreCase="FALSE" NoEmbeddedVars="FALSE" _COMMENT="Replace every space with \" AND \""/>
<TEXT BOX DISPLAY Title="Result" Content="{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\fonttbl{\\f0\\fnil\\fcharset0 Tahoma;}{\\f1\\fnil Tahoma;}}\r\n\\viewkind4\\uc1\\pard\\lang4105\\f0\\fs20 %Input%\\lang1033\\f1\\fs14 \r\n\\par }\r\n" Left="Center" Top="Center" Width="278" Height="200" Monitor="0" OnTop="TRUE" Keep_Focus="TRUE" Mode="\x00" Delay="0"/>

 

Link to comment
Share on other sites

Thanks for taking the trouble to reply acantor.

 

Just to clarify, will your suggestion result in the below, which is what I need:

 

Before:

This is a test and that's all there is to it!

 

After:

"This is a test and that's all there is to it!"

 

Now I must apologise as I should have said that I'm a noob with custom macros.  In other words, I don't know how or where I should insert your code :-(

 

Could you help me please?

 

Thank you!

Link to comment
Share on other sites

Hi,

 

With time zones in mind, before @acantor replies perhaps you could clarify your requirement. Is it really simply to add quotation marks before and after a target string, as your last post implies?

 

Or is it the much more complex requirement to add " and "? If the latter, which is how I first interpreted it could you give three 'before and after' examples of text to which you will apply your macro:

 

1. Containing the target string, say 'xyz' or 'abc xyz' or whatever.

2. As #1 but also containing one or more instances of 'and' or 'And' inside the target string, say 'abc and xyz'.

3. As #2 but containing one or more instances of 'and' or 'And' inside and outside the target string.

 

--------------------

To test the macro, copy the second section (the Code section starting "<VARIABLE SET STRING Option=") and paste it into a new empty macro. Or show us your code so that we can be more specific. For instance, @cantor's macro assumes a variable %Input%, so you will need to integrate that into your own code.

 

 

 

Terry, UK

Sunday 27 December 2020, 0851

 

 

Link to comment
Share on other sites

Sure, thanks for assisting terrypin.

 

Yes, it is simply 1.  Ignore 2. and 3. as neither are required.

 

All I need is to be able to automatically enclose any text string with quotation marks so I can return more targeted search results in another app.

 

"this is what I mean"

"note that it's double commas, not singles"

"many thanks!"

 

P.S.  I've just thought - I would be expecting the first (") (ignore parentheses) to be inserted immediately, but as I will still be typing the text, the second (") needs to be inserted after I have finished typing the text string.  Typically the search will be in the format dd.mm.yyyy (e.g. "02.01.1973") but it could also be text that takes me 5-10 secs to type.  If possible, I wouldn't want the second (") to be time-related (e.g. not 'continue macro after 5/10/15 seconds') as I don't know how long it's likely to take me to type and I don't want to either be twiddling my thumbs waiting for the other half of the macro to execute or being rushed to enter the data in time!

Edited by MarathonZephead
P.S.
Link to comment
Share on other sites

Can you post your macro, showing both the script commands and the direct code, so that we can see how far you’ve got. Include Comments wherever a command is not obvious.

 

What is the work environment? Is this all to be done within the macro, or while typing in a text editor or word processor application.

 

Have you experimented by running the macro from @acantor? And extended it by setting %Input% from the clipboard or a Prompt command?

 

Or, if you haven't got anywhere at all yet, work with this simple macro before tackling what sounds a rather challenging one for a beginner.

 

COMMANDS:


// Start by understanding the basic stuff.
// This simple macro searches for instances of a specified string within text that has first been copied to the Windows clipboard.
// Assumes you are searching for the string  15.01.1987
 
Variable Set String %tInput% from the clipboard contents
Variable Modify String: Replace "15.01.1987" in %tInput% with ""15.01.1987""
Text Box Display: Result

 

DIRECT CODE

<COMMENT Value="Start by understanding the basic stuff."/>
<COMMENT Value="This simple macro searches for instances of a specified string within text that has first been copied to the Windows clipboard."/>
<COMMENT Value="Assumes you are searching for the string  15.01.1987"/>
<COMMENT/>
<VARIABLE SET STRING Option="\x02" Destination="%tInput%" NoEmbeddedVars="FALSE"/>
<VARIABLE MODIFY STRING Option="\x0F" Destination="%tInput%" ToReplace="15.01.1987" ReplaceWith="\"15.01.1987\"" All="TRUE" IgnoreCase="FALSE" NoEmbeddedVars="FALSE"/>
<TEXT BOX DISPLAY Title="Result" Content="{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang2057{\\fonttbl{\\f0\\fnil Tahoma;}}\r\n\\viewkind4\\uc1\\pard\\f0\\fs20 %tInput%\r\n\\par }\r\n" Left="123" Top="485" Width="1114" Height="649" Monitor="0" OnTop="TRUE" Keep_Focus="TRUE" Mode="\x00" Delay="0"/>

 

Run it either by the method I described earlier, or from the exported macro attached.

 

EnclosingSpecifiedTextInQuotes.mex

ImportingMacro.jpg

Link to comment
Share on other sites

From your description, wrapping text in quote marks might be best accomplished by choosing ShortKey as an activation rather than Hotkey -- provided you type or paste the date.


I would do it this way:


1. In the Macro Express Explorer, choose Options > Preferences...


2. Choose "Activations" in the left pane, and "ShortKeys" in the right.


3. Choose "Use prefix keys"


4. Change the prefix key from ## to the quote symbol: "


5. Click "OK."


6. In the Macro Express Explorer, create a new macro.


7. Choose "Shortkey" as the activation. In the field labelled "Original Shortkey:" type a quote mark.


8. Make sure the "Script" tab is active. Paste the following line into the right pane:


<TEXT TYPE Action="0" Text="\"\"<ARROW LEFT>"/>


Once pasted, it will look like this:


Text Type (Simulate Keystrokes): ""<ARROW LEFT>


9. Save the macro and test it.

 

To test the macro, open a document and type the quote symbol twice: ""

 

Typing these two characters should activate the macro. It will run in a fraction of a second.

 

The cursor will be between the quote marks instead of to the right of the second quote mark.

 

Then type or paste the date. Press the right arrow key once to continue, which will move the cursor past the second quote mark.

Link to comment
Share on other sites

Glad it helped!

 

If you want to take this to the next level, the script can be modified to do more. For example, it's possible to detect whether text is selected. If text is not selected, the script does what it does now. But if text is selected, the script "wraps" the selected text in quote marks.

 

Another alternative: press the hotkey to output the content of the clipboard, but with quote marks around it.

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...
 Share

×
×
  • Create New...