Jump to content
Macro Express Forums

Challenge: script to automatically output HTML code


Recommended Posts

1 hour ago, acantor said:

Finally, a challenge within the challenge: I realize now that there is a way to reduce the number of lines of every example we have posted by one. What is that method? (Hint: It's not a particularly practical method so you might not use it in practice. But it does work.)

I give up.  The only thing I can see is to eliminate the display of statistics at the end. 

Link to comment
Share on other sites

Oops! Scratch the challenge within the challenge.

 

I was thinking about early versions of my script that included a copy to clipboard command:

 

Clipboard Copy

 

The idea was to get rid of the copy instruction, and change the activation from hotkey to clipboard (contains "  ").

 

But I got rid of the copy command days ago. Sorry for raising false hope of a shorter solution. My impetus to save a millionth of a second was motivated by my growing realization that the end of the universe is nigh! :)

Link to comment
Share on other sites

1 hour ago, acantor said:

the end of the universe is nigh!

Great idea for a cartoon.  I wish I could draw. 

 

First panel shows a man carrying the head end a long snake.  Another man behind him carrying more of the snake.

Second panel two more men carrying more and more of the snake.

Third panel two more men with more snake, but one of the men is carrying a sign that says, "The End is Near."

Last panel a man carrying the tail end. 

 

OK, I'll keep my day job.  Besides, somebody's probably done it already. 

  • Haha 1
Link to comment
Share on other sites

Back to the original challenge. Rberq, you were bang-on with your suggestion to use Cobol-like variable names. I defined Boolean variables with descriptive names to bring order to an otherwise messy macro. My initial attempt of nested IF-ELSE statements strewn with multiple AND and OR statements was almost impossible to follow. The version with descriptive names is much easier to understand. Although the script is lengthy, I found it relatively easy to fill in the blanks once I had worked out the variables that I actually needed to cover the scenarios the macro was likely to encounter.

 

I added a "feature" since setting out the challenge, borne of the experience of actually using this macro to update a website. The script processes the clipboard and the selected text. There are times when I would like to be able to empty the clipboard. I modified the script so that when the selection and the clipboard are identical, the macro offers to clear the clipboard.

 

I don't remember whether Macro Express 3 and 5 support Boolean variables. If they don't, one can define Integer variables that do the same thing:

 

%IsClipboardPopulated% = "1"    [True]

%IsClipboardPopulated% = "0"    [False]

 

Most of the comments are at the start of the script, and intended to give an overview of how it works. The comments embedded in the code are more sign-posts of what's happening than detailed descriptions.  

// HTML hypertext link code generator
//      Last updated: 6 June 2020
 
// This script analyzes the data in two locations:
//      1. The clipboard
//      2. The selection (i.e., whatever text is selected)
 
// The script tests both to decide whether they contain a web address, 
//      a string of "ordinary text," one of each, or neither.
// My test to identify a web address is that it always contains: 
//      "http" OR "www." OR ".html"
// Therefore, "ordinary text" contains none of the above.
//      This test is not foolproof, but it works most of the time!
 
// Once decided, the script outputs code for an HTML hypertext link:
//      <a href="WEB_ADDRESS">ORDINARY_TEXT</a>
 
// If nothing is selected, the script applies the rules to the clipboard
//      to decide whether it's a web address or ordinary text. 
//      If it's a web address, the script outputs this:
//      <a href="WEB_ADDRESS"></a>
//      If it's ordinary text, the script outputs this:
//      <a href="">ORDINARY_TEXT</a>
 
// If the clipboard and selection are identical (but not empty), 
//      the script clears the clipboard and outputs this for a web address:
//      <a href="WEB_ADDRESS"></a>
//      or this for ordinary text:
//      <a href="">ORDINARY_TEXT</a>
 
// If both the clipboard and selection are empty, the script outputs this:
//      <a href=""></a>
 
// When data is missing, the script moves the cursor (insertion point) back
//      to the appropriate place in the HTML code.
 
// Assign %Clip% to the clipboard
Variable Set String %Clip% from the clipboard contents
Variable Modify String %Clip%: Trim
Variable Set Integer %ClipLength% to the length of variable %Clip%
 
// Assign %Selection% to the selected text (if any)
Clipboard Empty
Clipboard Copy
Variable Set String %Selection% from the clipboard contents
Variable Modify String %Selection%: Trim
Variable Set Integer %SelectionLength% to the length of variable %Selection%
 
// Offer to clear the clipboard if clipboard = selection. Then proceed as usual
If Variable %Clip% Equals "%Selection%"
  If Message "OK to clear the clipboard?"
    Clipboard Empty
    Variable Set String %Clip% to ""
    Variable Set Integer %ClipLength% to 0
  End If
End If
 
// SET BOOLEAN VALUES
// (If a Boolean variable is not explicitly set, its value is FALSE)
 
If Variable %SelectionLength% Is Greater Than "0" // Test if anything is selected
  Variable Set Bool %IsSelected% to "True"
End If
 
If Variable %ClipLength% Is Greater Than "0" // Test if clipboard contains something
  Variable Set Bool %IsClipPopulated% to "True"
End If
 
If Variable %Clip% Contains "http" // Test if clipboard contain a web address
  OR
If Variable %Clip% Contains "www."
  OR
If Variable %Clip% Contains ".html"
  Variable Set Bool %IsWebAddressInClip% to "True"
End If
 
If Variable %ClipLength% Is Greater Than "0" // Test if clipboard contains ordinary text
  AND
If Variable %Clip% Does not Contain "http"
  AND
If Variable %Clip% Does not Contain "www."
  AND
If Variable %Clip% Does not Contain ".html"
  Variable Set Bool %IsOrdinaryTextInClip% to "True"
End If
 
If Variable %Selection% Contains "http" // Test if selection contains a web address
  OR
If Variable %Selection% Contains "www."
  OR
If Variable %Selection% Contains ".html"
  Variable Set Bool %IsWebAddressInSelection% to "True"
End If
 
If Variable %SelectionLength% Is Greater Than "0" // Test if selection contains ordinary text
  AND
If Variable %Selection% Does not Contain "http"
  AND
If Variable %Selection% Does not Contain "www."
  AND
If Variable %Selection% Does not Contain ".html"
  Variable Set Bool %IsOrdinaryTextInSelection% to "True"
End If
 
// THE NEXT SECTION DOES THE HEAVY LIFTING!
 
// Web address in clipboard, ordinary text in selection
If Variable %IsWebAddressInClip% Equals "True"
  AND
If Variable %IsOrdinaryTextInSelection% Equals "True"
  Text Type (Simulate Keystrokes): <a href="%Clip%">%Selection%</a>
  Macro Stop
End If
 
// Ordinary text in clipboard, web address in selection
If Variable %IsOrdinaryTextInClip% Equals "True"
  AND
If Variable %IsWebAddressInSelection% Equals "True"
  Text Type (Simulate Keystrokes): <a href="%Selection%">%Clip%</a>
  Macro Stop
End If
 
// Nothing in clipboard, nothing selected
If Variable %IsClipPopulated% Equals "False"
  AND
If Variable %IsSelected% Equals "False"
  Text Type (Simulate Keystrokes): <a href=""></a>
  Text Type (Simulate Keystrokes): <ARROW LEFT><ARROW LEFT><ARROW LEFT><ARROW LEFT><ARROW LEFT><ARROW LEFT>
  Macro Stop
End If
 
// Nothing in clipboard, web address is selected
If Variable %IsClipPopulated% Equals "False"
  AND
If Variable %IsWebAddressInSelection% Equals "True"
  Text Type (Simulate Keystrokes): <a href="%Selection%"></a>
  Text Type (Simulate Keystrokes): <ARROW LEFT><ARROW LEFT><ARROW LEFT><ARROW LEFT>
  Macro Stop
End If
 
// Nothing in clipboard, ordinary text is selected
If Variable %IsClipPopulated% Equals "False"
  AND
If Variable %IsOrdinaryTextInSelection% Equals "True"
  Text Type (Simulate Keystrokes): <a href="">%Selection%</a>
  Variable Set Integer %TextLength% to the length of variable %Selection% // Move cursor to URL location
  Variable Modify Integer: %TextLength% = %TextLength% + 6
  Repeat Start (Repeat %TextLength% times)
    Text Type (Simulate Keystrokes): <ARROW LEFT>
  End Repeat
  Macro Stop
End If
 
// Web address in clipboard, nothing selected
If Variable %IsWebAddressInClip% Equals "True"
  AND
If Variable %IsSelected% Equals "False"
  Text Type (Simulate Keystrokes): <a href="%Clip%"></a>
  Text Type (Simulate Keystrokes): <ARROW LEFT><ARROW LEFT><ARROW LEFT><ARROW LEFT>
  Macro Stop
End If
 
// Ordinary text in clipboard, nothing selected
If Variable %IsOrdinaryTextInClip% Equals "True"
  AND
If Variable %IsSelected% Equals "False"
  Text Type (Simulate Keystrokes): <a href="">%Clip%</a>
  Variable Set Integer %TextLength% to the length of variable %Clip% // Move cursor to URL location
  Variable Modify Integer: %TextLength% = %TextLength% + 6
  Repeat Start (Repeat %TextLength% times)
    Text Type (Simulate Keystrokes): <ARROW LEFT>
  End Repeat
  Macro Stop
End If
 
// Ordinary text in clipboard, ordinary text in selection (result may not be meaningful)
If Variable %IsOrdinaryTextInClip% Equals "True"
  AND
If Variable %IsOrdinaryTextInSelection% Equals "True"
  Text Type (Simulate Keystrokes): <a href="%Selection%">%Clip%</a>
  Variable Set Integer %TextLength% to the length of variable %Clip%
  Variable Modify Integer: %TextLength% = %TextLength% + 6 // Move cursor to URL location
  Repeat Start (Repeat %TextLength% times)
    Text Type (Simulate Keystrokes): <ARROW LEFT>
  End Repeat
  Macro Stop
End If
 
// Web address in clipboard, web address in selection (result may not be meaningful)
If Variable %IsWebAddressInClip% Equals "True"
  AND
If Variable %IsWebAddressInSelection% Equals "True"
  Text Type (Simulate Keystrokes): <a href="%Clip%">%Selection%</a>
  Variable Set Integer %TextLength% to the length of variable %Selection%
  Variable Modify Integer: %TextLength% = %TextLength% + 6 // Move cursor to URL location
  Repeat Start (Repeat %TextLength% times)
    Text Type (Simulate Keystrokes): <ARROW LEFT>
  End Repeat
  Macro Stop
End If
<COMMENT Value="HTML hypertext link code generator"/>
<COMMENT Value="     Last updated: 6 June 2020"/>
<COMMENT/>
<COMMENT Value="This script analyzes the data in two locations:"/>
<COMMENT Value="     1. The clipboard"/>
<COMMENT Value="     2. The selection (i.e., whatever text is selected)"/>
<COMMENT/>
<COMMENT Value="The script tests both to decide whether they contain a web address, "/>
<COMMENT Value="     a string of \"ordinary text,\" one of each, or neither."/>
<COMMENT Value="My test to identify a web address is that it always contains: "/>
<COMMENT Value="     \"http\" OR \"www.\" OR \".html\""/>
<COMMENT Value="Therefore, \"ordinary text\" contains none of the above."/>
<COMMENT Value="     This test is not foolproof, but it works most of the time!"/>
<COMMENT/>
<COMMENT Value="Once decided, the script outputs code for an HTML hypertext link:"/>
<COMMENT Value="     <a href=\"WEB_ADDRESS\">ORDINARY_TEXT</a>"/>
<COMMENT/>
<COMMENT Value="If nothing is selected, the script applies the rules to the clipboard"/>
<COMMENT Value="     to decide whether it's a web address or ordinary text. "/>
<COMMENT Value="     If it's a web address, the script outputs this:"/>
<COMMENT Value="     <a href=\"WEB_ADDRESS\"></a>"/>
<COMMENT Value="     If it's ordinary text, the script outputs this:"/>
<COMMENT Value="     <a href=\"\">ORDINARY_TEXT</a>"/>
<COMMENT/>
<COMMENT Value="If the clipboard and selection are identical (but not empty), "/>
<COMMENT Value="     the script clears the clipboard and outputs this for a web address:"/>
<COMMENT Value="     <a href=\"WEB_ADDRESS\"></a>"/>
<COMMENT Value="     or this for ordinary text:"/>
<COMMENT Value="     <a href=\"\">ORDINARY_TEXT</a>"/>
<COMMENT/>
<COMMENT Value="If both the clipboard and selection are empty, the script outputs this:"/>
<COMMENT Value="     <a href=\"\"></a>"/>
<COMMENT/>
<COMMENT Value="When data is missing, the script moves the cursor (insertion point) back"/>
<COMMENT Value="     to the appropriate place in the HTML code."/>
<COMMENT/>
<COMMENT Value="Assign %Clip% to the clipboard" _BACK="0080FFFF"/>
<VARIABLE SET STRING Option="\x02" Destination="%Clip%" NoEmbeddedVars="FALSE"/>
<VARIABLE MODIFY STRING Option="\x00" Destination="%Clip%"/>
<VARIABLE SET INTEGER Option="\x0D" Destination="%ClipLength%" Text_Variable="%Clip%"/>
<COMMENT/>
<COMMENT Value="Assign %Selection% to the selected text (if any)" _BACK="0080FFFF"/>
<CLIPBOARD EMPTY/>
<CLIPBOARD COPY/>
<VARIABLE SET STRING Option="\x02" Destination="%Selection%" NoEmbeddedVars="FALSE"/>
<VARIABLE MODIFY STRING Option="\x00" Destination="%Selection%"/>
<VARIABLE SET INTEGER Option="\x0D" Destination="%SelectionLength%" Text_Variable="%Selection%"/>
<COMMENT/>
<COMMENT Value="Offer to clear the clipboard if clipboard = selection. Then proceed as usual" _BACK="0080FFFF"/>
<IF VARIABLE Variable="%Clip%" Condition="\x00" Value="%Selection%" IgnoreCase="FALSE"/>
<IF MESSAGE Caption="OK to clear the clipboard?" Message="Clipboard and the selection are the same: \r\n\r\n%Clip%" BtnMode="\x00" Default="TRUE" Left="Center" Top="Center" Monitor="0" Delay="0"/>
<CLIPBOARD EMPTY/>
<VARIABLE SET STRING Option="\x00" Destination="%Clip%" NoEmbeddedVars="FALSE"/>
<VARIABLE SET INTEGER Option="\x00" Destination="%ClipLength%" Value="0"/>
<END IF/>
<END IF/>
<COMMENT/>
<COMMENT Value="SET BOOLEAN VALUES" _BACK="0080FFFF"/>
<COMMENT Value="(If a Boolean variable is not explicitly set, its value is FALSE)"/>
<COMMENT/>
<IF VARIABLE Variable="%SelectionLength%" Condition="\x03" Value="0" IgnoreCase="FALSE" _COMMENT="Test if anything is selected" _BACK="0080FFFF"/>
<VARIABLE SET BOOL Destination="%IsSelected%" Command="263" Value="TRUE"/>
<END IF/>
<COMMENT/>
<IF VARIABLE Variable="%ClipLength%" Condition="\x03" Value="0" IgnoreCase="FALSE" _COMMENT="Test if clipboard contains something" _BACK="0080FFFF"/>
<VARIABLE SET BOOL Destination="%IsClipPopulated%" Command="263" Value="TRUE"/>
<END IF/>
<COMMENT/>
<IF VARIABLE Variable="%Clip%" Condition="\x06" Value="http" IgnoreCase="FALSE" _COMMENT="Test if clipboard contain a web address" _BACK="0080FFFF"/>
<OR/>
<IF VARIABLE Variable="%Clip%" Condition="\x06" Value="www." IgnoreCase="FALSE"/>
<OR/>
<IF VARIABLE Variable="%Clip%" Condition="\x06" Value=".html" IgnoreCase="FALSE"/>
<VARIABLE SET BOOL Destination="%IsWebAddressInClip%" Command="263" Value="TRUE"/>
<END IF/>
<COMMENT/>
<IF VARIABLE Variable="%ClipLength%" Condition="\x03" Value="0" IgnoreCase="FALSE" _COMMENT="Test if clipboard contains ordinary text" _BACK="0080FFFF"/>
<AND/>
<IF VARIABLE Variable="%Clip%" Condition="\x07" Value="http" IgnoreCase="FALSE"/>
<AND/>
<IF VARIABLE Variable="%Clip%" Condition="\x07" Value="www." IgnoreCase="FALSE"/>
<AND/>
<IF VARIABLE Variable="%Clip%" Condition="\x07" Value=".html" IgnoreCase="FALSE"/>
<VARIABLE SET BOOL Destination="%IsOrdinaryTextInClip%" Command="263" Value="TRUE"/>
<END IF/>
<COMMENT/>
<IF VARIABLE Variable="%Selection%" Condition="\x06" Value="http" IgnoreCase="FALSE" _COMMENT="Test if selection contains a web address" _BACK="0080FFFF"/>
<OR/>
<IF VARIABLE Variable="%Selection%" Condition="\x06" Value="www." IgnoreCase="FALSE"/>
<OR/>
<IF VARIABLE Variable="%Selection%" Condition="\x06" Value=".html" IgnoreCase="FALSE"/>
<VARIABLE SET BOOL Destination="%IsWebAddressInSelection%" Command="263" Value="TRUE"/>
<END IF/>
<COMMENT/>
<IF VARIABLE Variable="%SelectionLength%" Condition="\x03" Value="0" IgnoreCase="FALSE" _COMMENT="Test if selection contains ordinary text" _BACK="0080FFFF"/>
<AND/>
<IF VARIABLE Variable="%Selection%" Condition="\x07" Value="http" IgnoreCase="FALSE" _BACK="00FFFFFF"/>
<AND/>
<IF VARIABLE Variable="%Selection%" Condition="\x07" Value="www." IgnoreCase="FALSE"/>
<AND/>
<IF VARIABLE Variable="%Selection%" Condition="\x07" Value=".html" IgnoreCase="FALSE"/>
<VARIABLE SET BOOL Destination="%IsOrdinaryTextInSelection%" Command="263" Value="TRUE"/>
<END IF/>
<COMMENT/>
<COMMENT Value="THE NEXT SECTION DOES THE HEAVY LIFTING!" _BACK="0080FFFF"/>
<COMMENT/>
<COMMENT Value="Web address in clipboard, ordinary text in selection" _BACK="0080FFFF"/>
<IF VARIABLE Variable="%IsWebAddressInClip%" Condition="\x00" Value="True" IgnoreCase="FALSE"/>
<AND/>
<IF VARIABLE Variable="%IsOrdinaryTextInSelection%" Condition="\x00" Value="True" IgnoreCase="FALSE"/>
<TEXT TYPE Action="0" Text="<a href=\"%Clip%\">%Selection%</a>"/>
<MACRO STOP/>
<END IF/>
<COMMENT/>
<COMMENT Value="Ordinary text in clipboard, web address in selection" _BACK="0080FFFF"/>
<IF VARIABLE Variable="%IsOrdinaryTextInClip%" Condition="\x00" Value="True" IgnoreCase="FALSE"/>
<AND/>
<IF VARIABLE Variable="%IsWebAddressInSelection%" Condition="\x00" Value="True" IgnoreCase="FALSE"/>
<TEXT TYPE Action="0" Text="<a href=\"%Selection%\">%Clip%</a>"/>
<MACRO STOP/>
<END IF/>
<COMMENT/>
<COMMENT Value="Nothing in clipboard, nothing selected" _BACK="0080FFFF"/>
<IF VARIABLE Variable="%IsClipPopulated%" Condition="\x00" Value="False" IgnoreCase="FALSE"/>
<AND/>
<IF VARIABLE Variable="%IsSelected%" Condition="\x00" Value="False" IgnoreCase="FALSE"/>
<TEXT TYPE Action="0" Text="<a href=\"\"></a>"/>
<TEXT TYPE Action="0" Text="<ARROW LEFT><ARROW LEFT><ARROW LEFT><ARROW LEFT><ARROW LEFT><ARROW LEFT>"/>
<MACRO STOP/>
<END IF/>
<COMMENT/>
<COMMENT Value="Nothing in clipboard, web address is selected" _BACK="0080FFFF"/>
<IF VARIABLE Variable="%IsClipPopulated%" Condition="\x00" Value="False" IgnoreCase="FALSE"/>
<AND/>
<IF VARIABLE Variable="%IsWebAddressInSelection%" Condition="\x00" Value="True" IgnoreCase="FALSE"/>
<TEXT TYPE Action="0" Text="<a href=\"%Selection%\"></a>"/>
<TEXT TYPE Action="0" Text="<ARROW LEFT><ARROW LEFT><ARROW LEFT><ARROW LEFT>"/>
<MACRO STOP/>
<END IF/>
<COMMENT/>
<COMMENT Value="Nothing in clipboard, ordinary text is selected" _BACK="0080FFFF"/>
<IF VARIABLE Variable="%IsClipPopulated%" Condition="\x00" Value="False" IgnoreCase="FALSE"/>
<AND/>
<IF VARIABLE Variable="%IsOrdinaryTextInSelection%" Condition="\x00" Value="True" IgnoreCase="FALSE"/>
<TEXT TYPE Action="0" Text="<a href=\"\">%Selection%</a>"/>
<VARIABLE SET INTEGER Option="\x0D" Destination="%TextLength%" Text_Variable="%Selection%" _COMMENT="Move cursor to URL location"/>
<VARIABLE MODIFY INTEGER Option="\x00" Destination="%TextLength%" Value1="%TextLength%" Value2="6"/>
<REPEAT START Start="1" Step="1" Count="%TextLength%" Save="FALSE"/>
<TEXT TYPE Action="0" Text="<ARROW LEFT>"/>
<END REPEAT/>
<MACRO STOP/>
<END IF/>
<COMMENT/>
<COMMENT Value="Web address in clipboard, nothing selected" _BACK="0080FFFF"/>
<IF VARIABLE Variable="%IsWebAddressInClip%" Condition="\x00" Value="True" IgnoreCase="FALSE"/>
<AND/>
<IF VARIABLE Variable="%IsSelected%" Condition="\x00" Value="False" IgnoreCase="FALSE"/>
<TEXT TYPE Action="0" Text="<a href=\"%Clip%\"></a>"/>
<TEXT TYPE Action="0" Text="<ARROW LEFT><ARROW LEFT><ARROW LEFT><ARROW LEFT>"/>
<MACRO STOP/>
<END IF/>
<COMMENT/>
<COMMENT Value="Ordinary text in clipboard, nothing selected" _BACK="0080FFFF"/>
<IF VARIABLE Variable="%IsOrdinaryTextInClip%" Condition="\x00" Value="True" IgnoreCase="FALSE"/>
<AND/>
<IF VARIABLE Variable="%IsSelected%" Condition="\x00" Value="False" IgnoreCase="FALSE"/>
<TEXT TYPE Action="0" Text="<a href=\"\">%Clip%</a>"/>
<VARIABLE SET INTEGER Option="\x0D" Destination="%TextLength%" Text_Variable="%Clip%" _COMMENT="Move cursor to URL location"/>
<VARIABLE MODIFY INTEGER Option="\x00" Destination="%TextLength%" Value1="%TextLength%" Value2="6"/>
<REPEAT START Start="1" Step="1" Count="%TextLength%" Save="FALSE"/>
<TEXT TYPE Action="0" Text="<ARROW LEFT>"/>
<END REPEAT/>
<MACRO STOP/>
<END IF/>
<COMMENT/>
<COMMENT Value="Ordinary text in clipboard, ordinary text in selection (result may not be meaningful)" _BACK="0080FFFF"/>
<IF VARIABLE Variable="%IsOrdinaryTextInClip%" Condition="\x00" Value="True" IgnoreCase="FALSE"/>
<AND/>
<IF VARIABLE Variable="%IsOrdinaryTextInSelection%" Condition="\x00" Value="True" IgnoreCase="FALSE"/>
<TEXT TYPE Action="0" Text="<a href=\"%Selection%\">%Clip%</a>"/>
<VARIABLE SET INTEGER Option="\x0D" Destination="%TextLength%" Text_Variable="%Clip%"/>
<VARIABLE MODIFY INTEGER Option="\x00" Destination="%TextLength%" Value1="%TextLength%" Value2="6" _COMMENT="Move cursor to URL location"/>
<REPEAT START Start="1" Step="1" Count="%TextLength%" Save="FALSE"/>
<TEXT TYPE Action="0" Text="<ARROW LEFT>"/>
<END REPEAT/>
<MACRO STOP/>
<END IF/>
<COMMENT/>
<COMMENT Value="Web address in clipboard, web address in selection (result may not be meaningful)" _BACK="0080FFFF"/>
<IF VARIABLE Variable="%IsWebAddressInClip%" Condition="\x00" Value="True" IgnoreCase="FALSE"/>
<AND/>
<IF VARIABLE Variable="%IsWebAddressInSelection%" Condition="\x00" Value="True" IgnoreCase="FALSE"/>
<TEXT TYPE Action="0" Text="<a href=\"%Clip%\">%Selection%</a>"/>
<VARIABLE SET INTEGER Option="\x0D" Destination="%TextLength%" Text_Variable="%Selection%"/>
<VARIABLE MODIFY INTEGER Option="\x00" Destination="%TextLength%" Value1="%TextLength%" Value2="6" _COMMENT="Move cursor to URL location"/>
<REPEAT START Start="1" Step="1" Count="%TextLength%" Save="FALSE"/>
<TEXT TYPE Action="0" Text="<ARROW LEFT>"/>
<END REPEAT/>
<MACRO STOP/>
<END IF/>

 

Link to comment
Share on other sites

3 hours ago, acantor said:

 

I don't remember whether Macro Express 3 and 5 support Boolean variables.

 

Yes, that naming convention certainly makes your macro more readable!

 

I don't think ME 3 supports Boolean variables, as such.  My last job before I retired involved coding "medical logic modules" which were add-on customizations to a very large electronic patient care system in a hospital.  It used a scripting language that seemed to be halfway between BASIC and COBOL.  I liked its Boolean variables even better than what you are showing, because you didn't have to say IF ... EQUALS TRUE or IF ... EQUALS FALSE.  It was more like, FALSE was equivalent to null; and TRUE was equivalent to anything else.  

 

So instead of
   Variable Set Bool %IsOrdinaryTextInClip% to "True"
   ...
   If Variable %IsOrdinaryTextInSelection% Equals "True"...

 

you would both define the variable and set the condition by
   Clipboard_contains_ordinary_text = 1 (or = 7, or ="yes indeed it does", or any other value you felt like)  
      or
   Clipboard_contains_ordinary_text = "" (null)

 

Then in the logic you would test the condition by
   If Clipboard_contains_ordinary_text ...
      or
   If not Clipboard_contains_ordinary_text ...

 

This did away with the unnecessary verbiage of "equals true" and "equals false".  The variable was Boolean in the sense of having a value or being null; and the IF statement tested for existence or non-existence.  Of course you could also test for specific values, for example
   If Clipboard_contains_ordinary_text equals "yes indeed it does" ... 

 


 

Link to comment
Share on other sites

I suppose other scripting tools yield "prettier" code than Macro Express. My favourite macro language of all time was the back-end of Borland's short-lived, non-WYSIWYG word processor, Sprint, released around 1988. (It competed with WordPerfect 5.0, and lost!)

 

Sprint's Boolean if-then-else expressions were something like this:

 

IsTab ? Break : (f c)

 

(Translation: "If the current character is a tab, stop. Else move the cursor forward one character.")

 

I appreciated the terseness of the language, which to my mind, invited simplicity and encouraged more readable code.

 

I believe that what I learned while messing with macro languages 30+ years ago laid a foundation for what I do now with Macro Express. Yet despite the thousands of hours of experience, I still find the process of scripting mysterious. I kludged together a version of my HTML hypertext link generator a decade ago, and reviewing the code today, I see it's a tangled mess of IF-THEN-ELSE statements. After trying (and failing) to make it right, it occurred to me in the flash that Boolean expressions were the key.

 

OK, who has ready to pose the next challenge?

Link to comment
Share on other sites

Back to rberq's challenge: I added a timer to the script, and tested three proposed solutions: checking the length of the string every loop, calculating the exact number of loops to make, and choosing 25 as the total number of loops.

 

I tested with a string of roughly 100,000 characters in three ways: all spaces, mostly spaces (1 character/13 spaces), and mostly characters (4 characters/1 space).

 

The result: The macro completes the work fastest when the input contains more characters and fewer spaces. That's not a surprise. But I saw no consistent differences between the there method used to parse out extra spaces.

Link to comment
Share on other sites

1 hour ago, acantor said:

Back to rberq's challenge: I added a timer to the script

I did similar testing with a timer also.  Much of the run time was moving the clipboard data into a variable before starting the whole operation; so I started the timer AFTER that move was completed.  I found the same thing that you did -- all varieties of the script ran in about the same time, with one exception.  One of my versions, before the replace-2-with-1 loop, changed all strings of 30 spaces to 1 space, then all strings of 29 spaces to 1, then all 28, then 27, and so on.  When that was done, the loop usually had to run only once or twice and sometimes not at all.  That version ran significantly faster when the entire string was all or mostly-all spaces; but it ran significantly longer with mostly-character text.  

Link to comment
Share on other sites

A thought on optimizing this macro.

 

Sample random characters from the initial string. I have no idea how many, but my initial inclination would be to pick a small percentage, perhaps 1%.

 

Then figure out the proportion of spaces in the sample. If the proportion of spaces is high, then iteratively delete some of the extra spaces before iteratively replacing space-space with space. If the proportion of spaces is low, immediately replace space-space with space.

Link to comment
Share on other sites

3 hours ago, acantor said:

Sample random characters from the initial string.

Well worth while if the macro will run hundreds or thousands of times a day.

Much ado about nothing if it runs rarely.

Must make a note to myself to find out what methods are used to compress text, as in zip files.  I have often wondered, but mostly back before I had Google to assist me. 

Link to comment
Share on other sites

Quote

 


Much ado about nothing if it runs rarely.
 

 

 

I wholeheartedly agree. At this point, continuing to improve this macro is, for me, an intellectual exercise -- a pastime, a way to while away some of the hours the pandemic has opened up.

 

When you first posed this challenge, I thought it was fairly simple, as I came up with a working prototype without too much effort. But over time, your challenge has proven to be an unexpected goldmine of ideas about programming. In other words, it's the best kind of challenge. 

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