Highlight Before and after the characters -- - livecode

I want to highlight the strings before and after '--'. Example good -- bad here i want to highlight good and bad. when ever the -- is comes then before and after the strings are become highlight. Is it possible.

Let's assume you have the following text in a field "mytext":
This text comes before -- this text comes after.
LiveCode (as most applications) does not allow discontinuous selections, so the 'select' command only works on continuous runs of text.
select word 1 to 3 of fld "mytext"
But you can simulate selection highlighting by setting the backgroundColor property of separate text runs:
put wordOffset("--",fld "mytext") into tWordIndex
set the backgroundColor of word 1 to tWordIndex - 1 of fld "mytext" to the hiliteColor
set the backgroundColor of word tWordIndex + 1 to -1 of fld "mytext" to the hiliteColor
Of course you can use any valid text chunk expression in the two 'set' statements, depending on what part of the text preceding and following the " -- " you want to "highlight".
To clear the backgroundColor from the field do this:
set the backgroundColor of char 1 to -1 of fld "mytext" to empty

Related

AppleScript: Breaking up 40 paragraphs into 10 paragraphs of 4 paragraphs

Essentially, I am taking a column of data from an excel file and breaking it up into small groups. So:
10
20
30
40
50
60
etc...
broken up into:
"10, 20, 30, 40"
"50, 60, 70, 80"
etc
Using AppleScript, I assume you would nest loops, something along the lines of:
tell application "TextEdit"
set theText to text of front document as string
set myParas to every paragraph of theText
set myNum to the number of paragraphs of theText
repeat myNum times
repeat 4 times
end repeat
end repeat
end tell
I'm going to be updating data once a month that comes through as a column of numbers and text. I can strip out all the text easy enough, just would like to know the principle of how to break up or merge the paragraphs into smaller blocks.
For many complicated reasons, I am stuck with AppleScript and textEdit, so other alternates (such as massaging with javascript or textWrangler or whatever) is not an option.
Also, maybe textEdit can do this on it's own but the script I will be using will have lots of other operations based on the above result, so AppleScript has to do all the heavy lifting.
You can specify the step size in a repeat loop, so you could do something like:
tell application "TextEdit" to set theText to text of front document
set paras to paragraphs of theText
set step to 4 -- number of items in a group
repeat with i from 1 to (count paras) by step
try
buildString(items i thru (i + step - 1) of paras)
on error errmess number errnum -- index out of bounds
log errmess
if errnum is -128 then error number -128 -- quit
buildString(items i thru -1 of paras) -- just to the end
end try
end repeat
to buildString(someList)
set tempTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to ", "
set output to someList as text
set AppleScript's text item delimiters to tempTID
display dialog output -- show it
return output
end buildString

How to replace certain pattern before and after particular strings in livecode?

I need to do certain replacement after and before particular strings. Example: I need to "replace bad with good in field "MytextField"". But this replacement should be done after begin{document} and before end{document}. None of the word replace before begin {document} and after end{document}. How is this possible?.
if I use this code "replace bad with good in field "MytextField"" all the instants of bad should be replaced with good. I don't need to change the entire field.
This should work if your begin and end markers only appear once in the field...
put wordOffset("begin{document}",fld "MytextField") into tBegin
put wordOffset("end{document}",fld "MytextField") into tEnd
put replaceText(word tBegin to tEnd of fld "MytextField","bad","good") into word tBegin to tEnd of fld "MytextField"
If the markers appear several times you will need a repeat loop to step through each one.
Paul
Put this into a field: aa red aa red aa red aa red
Put this into a button
local latest
on mouseUp
put 0 into latest
startFinding fld 1,0
end mouseUp
on startFinding tText,tOffset
if the optionKey is down then exit to top --just in case...
put wordOffset("red",fld 1,tOffset) into latest
answer "Change word" && (latest + tOffset) && "?" with "Change" or "Continue"
If it = "change" then put "green" into word (latest + tOffset) of fld 1
add latest to tOffset
if tOffset < the number of words of fld 1 then startFinding fld 1,tOffset
end startFinding

How to set the text style property of a field content in LiveCode

I have a scrolling field in my LiveCode app that looks like this:
A: John Rogers 234 9th
B: Henry Ford 555 1st
C: Jane Fonda 224 5th
I want to make the last words (the positions i.e 9th, 1st, 5th) in each line have a specific text styling. For example, make it red in color.
I tried several things including iterating over the lines with this code:
repeat for each line lC in field "fcontent"
set the foregroundColor of lC to "red"
end repeat
...but kept getting an error. Please someone put me on the right track?
Your solution using the foreGroundColor is almost right. You need to use the textColor instead and you can't use the repeat for each control structure in this case.
repeat with x = 1 to the number of lines of fld "fcontent"
set the textColor of the last word of line x of fld "fcontent" to red
end repeat
Red doesn't need quotes, because it is defines as a constant in LiveCode.
Marks solution is the right one. Looking at your question, you mentioned wanting to set the colour of the last word of each line. You can add "the last word of" to Mark's script and you should get exactly what you're looking for:
repeat with x = 1 to the number of lines of fld "fcontent"
set the textColor of the last word of line x of fld "fcontent" to "255,0,100"
end repeat

How do I copy a chunk of Unicode text?

I have a field "data" with Unicode text in it which displays properly. I want to copy a chunk of it and put it into another field called "someData".
I tried the following script in a button
on mouseUp
put word 2 of line 1 of the unicodeText of field "data" into t
set the unicodeText of field "someData" to t
end mouseUp
Non Unicode text displays fine in the field "someData" but Unicode text does not.
You could probably get away with UTF8 encoding then parsing then re-encoding
on mouseUp
put word 2 of line 1 of uniDecode(the unicodeText of field "data","UTF8") into t
set the unicodeText of field "someData" to uniEncode(t,"UTF8")
end mouseUp
on mouseUp
put unicode the unicodeText of word 2 of field "data" into field "someData"
end mouseUp
should work.
Marek
Here is another one-liner you can test:
set the unicodeText of field 2 to the unicodeText of word 2 of field 1
I am no expert in unicode, but there may be a clue in that LC treats most unicode stuff as properties. Because of this, one may set, say, the uniCodeText of a field:
set the unicodeText of fld 1 to "U+400"
But one cannot set that property, or any property, in a variable. Consider the following two handlers. It is assumed there exist two fields, "fld 1" and "fld 2".
on mouseUp
set the useUnicode to "true"
set the unicodeText of fld 1 to "U+400" -- an example
set the unicodeText of fld "f2" to the uniCodeText of fld 1
end mouseUp
on mouseUp
set the useUnicode to "true"
set the unicodeText of fld 1 to "U+400"
put fld 1 into temp
set the unicodeText of fld "f2" to temp
end mouseUp
The first works, the second does not. In your example, you try to put displayed uniCode into a variable. I don't think you can "put" that sort of thing. You have to set a property.
Now that said, check out the "put uniCode" command. This might get around the property thing. Write back if it does.
Craig Newman

Align at longest word

I have the following code:
a = 123
p.value 0.123
p.long.name = "abc"
How can I align each line like shown below in vim?
a = 123
p.value = 0.123
p.long.name = "abc"
Thanks for any hints.
Without plugin:
:%s/=/ &/
:%s/\%13c\s\+=/=
First command will insert spaces before first equal signs on all lines, second one will remove all spaces before an equal sign at 13th column. You could also use Visual block selection and <..... to shift left as many times as necessary.
However this is really unclean. With the tabular plugin you just type :Tab /=/ and this will do the work and the range will be calculated automatically (greatest range around the cursor in which all lines match the pattern).

Resources