How do I copy a chunk of Unicode text? - string

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

Related

LiveCode: How to change the background color of a cell in a Data Grid

In a Data Grid table, I would like to change the backgroud color of a "selected" cell.
In the table, the user can select a cell in each row (I used a custom property that I increment each time the user click on the same row). I would like to highlight the selected cell, for instance by changing the bg color of it.
How can I do that ?
Thanks a lot.
I am fond of saying that I use data grids, but do not understand them.
A dataGrid is just a complex LC object, comprised of other LC objects, groups and behaviors. Each field is designated as, say: fld "Col 1 0002" (first column, second line)
So you could:
on mouseUp
set the opaque of fld "col 1 0002" to "true"
set the backColor of fld "col 1 0002" to "red"
end mouseUp
Now there may well be a "native" way of doing this, But I do not know it.
If I make a new DG and fill it with some tab and return delimited text, my handler turns the designated "cell" red.
I do notice that there was a spurious character after "end mouseUp" in my earlier post. I edited it out, but might this have caused your problem?
Do you have data in the "cell" you are interested in? This method will not work if it is empty. "No such object"? It may be that you need this as well:
on mouseUp
set the opaque of fld "col 2 0003" of grp "yourDG to "true"
set the backColor of fld "col 2 0003" of grp "yourDG" to "green"
end mouseUp
I found a way, thanks to the answer of dunbarx, and with adding the use of "the target".
on mouseUp
set the opaque of the target to "true"
set the backColor of the target to "green"
end mouseUp

Scorlling filed is empty but it display some string why?

I have two Scrolling field and one containing some text and another containing some words. eg. (s1:s2) separated by colon my objective is to find how many words in the second Scrolling are appearing in first Scrolling field. (eg: if s1 is appearing in first scrolling field then it shows s1,1 it means s1 appearing one time). While executing my code it's work but if my first scrolling field is empty it shows some text also some times it takes some words in the first field. (eg:\documentclass)this word is not in 2'nd field.
I am using the following code.
global ar
global sam
on mouseUp
--set the caseSensitive to true
put 0 into tmp
put empty into sam
put the field SRText into myArrayT
split myArrayT by CR
put the number of lines of (the keys of myArrayT) into myArrayl
repeat for each key j in myArrayT
put myArrayT[j] into k
split k by colon
put k[1] into searchStr1
put the field "MytextField" into sss
repeat for each word ass in sss
if ass contains searchStr1 then
add 1 to tmp
put ass & CR & CR after sam
put sam into ar
end if
end repeat
end repeat
answer sam
answer tmp
--answer sa
end mouseUP
on mouseDown
answer ar
put ar into ss
get ss
repeat for each word tWord in it
add 1 to wordCount[tword]
end repeat
combine wordCount by return and comma
answer wordCount
--put empty into sam
end mouseDown
You really should use quotes around field names. I promise you'll realise some day, that you have been shooting yourself in the foot all the time.
Don't use the in front of object references,
field "Field Name"
button "Field name"
but do use the in front of property references:
the number of lines of fld "Field Name"
the text of fld "Field Name"
the name of this stack
There really use no reason for your obsession with arrays. Stop using arrays when you don't need them! It just makes things overly complicated and slow.
I have modifed your mouseUp handler. I didn't look at your mouseDown handler. I believe your mouseUp handler should look like this:
on mouseUp
--set the caseSensitive to true
put 0 into tmp
put empty into sam
repeat for each line k in fld "SRText"
set the itemDel to colon
put item 1 of k into searchStr1
repeat for each word ass in field "MytextField"
if ass contains searchStr1 then
add 1 to tmp
put ass & colon & tmp & cr after sam
end if
end repeat
put 0 into tmp
end repeat
put sam
end mouseUp
The script above is based on your approach and I really don't understand why you would do it this way and what you would do with the result. The script below is probably much more efficient and it seems more useful to me.
on mouseUp
// I assume fld SRText contains a list similar to
// "abc:1" & cr & "bcd:2" & cr & "cde:1" & cr & "def:4"
put fld "SRText" into myData
split myData by cr and colon
put the keys of myData into myData
// myData is not an array!
repeat for each word myWord in fld "MytextField"
if myWord is among the lines of myData then
// use an array to store data, not to loop through
if myCounts[myWord] is empty then
put 1 into myCounts[myWord]
else
add 1 to myCounts[myWord]
end if
end if
end repeat
combine myCounts by cr and colon
put myCounts
end mouseUp

Highlight Before and after the characters --

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

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

populate the data grid form with selected categories

The stack I made is displaying categorized lines of text. I made a card to select the categories put them into a variable and populate a data grid form but the code for populating the data grid for My Categories does not work:
Here is the code for populating the form:
global gAllLines,gSelectedCategories,gMyCategories
on mouseUp
put empty into gAllLines
put fld "alllines" of cd "settings_files" of stack "settingsandfiles" into gAllLines
put empty into gMyCategories
repeat for each line i in gAllLines
if item 2 of i is among the items of gSelectedCategories then put i & cr after gMyCategories
end repeat
set the dgText of group "mycategories_compact" to gMyCategories
end mouseUp
The download link for the stack (best working so far, but still not 100% OK) is:
https://dl.dropboxusercontent.com/u/99863601/Data%20grid%20Form_All%20Lines%20Categories%20Selections3.zip
Please let me know how to fix it.
Thanks in advance.
keram
The problem is you have two different itemDelimiters. Your field "alllines" has tab delimited data while your gSelectedCategories has comma delimited. Try:
global gAllLines,gSelectedCategories,gMyCategories
on mouseUp
put empty into gAllLines
put fld "alllines" of cd "settings_files" of stack "settingsandfiles" into gAllLines
put empty into gMyCategories
replace comma with tab in gSelectedCategories
set the itemDelimiter to tab
repeat for each line i in gAllLines
if item 2 of i is among the items of gSelectedCategories then put i & cr after gMyCategories
end repeat
set the dgText of group "mycategories_compact" to gMyCategories
end mouseUp
Edit
I never use dgText so I'm not sure why buy this datagrid seems to not accept dgText["firstLineContainsColumnNames"] any more. So to me the logical solution is to use dgData:
global gAllLines,gSelectedCategories,gMyCategories
on mouseUp
set the dgData of group "mycategories_compact" to empty
put empty into gMyCategories
replace comma with tab in gSelectedCategories
set the itemDelimiter to tab
local tIndex = 1,tDataA
repeat for each line i in gAllLines
if item 2 of i is among the items of gSelectedCategories then
put item 1 of i into tDataA[tIndex]["Text"]
put item 2 of i into tDataA[tIndex]["Category"]
add 1 to tIndex
end if
end repeat
set the dgData of group "mycategories_compact" to tDataA
end mouseUp
I have not looked at your stack, but the handler works fine as far as it goes. That is, the global variable "gSelectedCategories" is loaded if one of the checkbox buttons has one of the words you are checking for in its name, What are you not seeing?
Craig Newman

Resources