How do I listen to a specific text string on Mac OS X in a livecode application - livecode

I want to create a Mac app similar to Textexpander or Atext. Both these applications allow the user to define snippets along with their respective trigger words. Typing the trigger words in any app, replaces that trigger word with the actual snippet defined.
I presume that the app listens to all strings being typed in any app and when it detects a string matching one of the trigger words defined, it replaces it with the snippet.
Is that how it actually works, or is there some other way?

Make two fields. In field 2 put something like:
time xyz
come ABC
In the script of field 1:
on textChanged
if the last char of me = space then
put the last word of me into temp
if temp is in fld 2 then
repeat for each word tWord in fld 2
put the last word of line lineOffset(temp,fld 2) of fld 2 into the last word of me
exit repeat
end repeat
end if
select after text of me
end if
end textChanged
Now type into fld 1, you know, "Now is the time for all good men to come to the aid of their country". This can be better done with an array, but the concept may be more accessible here.
This is a better handler, since it will not react to the trigger word:
on textChanged
if the last char of me = space then
put the last word of me into stringOfInterest
put fld 2 into dataToSearch
if stringOfInterest is in dataToSearch then
repeat for each line tLine in dataToSearch
if word 1 of tLine = stringOfInterest then
put word 2 of tLine into the last word of me
exit repeat
end if
end repeat
end if
select after text of me
end if
end textChanged

Related

Splitting a string of a multiline textbox by lines

I want to split the text of a textbox after a specific amount of visible lines.
I've found some codes that "allows that", but all of them consider the lines by the "vbCrLf" parameter, but i want to split using the visible lines of a multiline textbox.
To make it more clear to understand, consider a multiline textbox with the following text:
"The history of textbooks dates back to civilizations of ancient history. For example, Ancient Greeks wrote texts intended for education. The modern textbook has its roots in the standardization made possible by the printing press. Johannes Gutenberg himself may have printed editions of Ars Minor, a schoolbook on Latin grammar by Aelius Donatus. Early textbooks were used by tutors and teachers, who used the books as instructional aids (e.g., alphabet books), as well as individuals who taught themselves."
When i use the Textbox.Linecount function it returns the number 6 because the textbox shows six lines (which depends on the size of the control), but if i use a function like strText = Split(TextBox.Text, vbCrLf) it will return 1, because there is only one vbCrLf. But i need to split the text into two textbox considering the visible lines of the control, something like what happens in page breaks of MS Word.
For a better visual explanation, please look at the attached image.
Example
Firstly, I'm not convinced there is a robust and elegant way to do this, but it was fun to experiment and it might be useful to you.
The following will split the contents of TextBoxInput into TextBoxPage1 and TextBoxPage2 breaking on the line number specified by PAGED_TEXT_BOX_LINES.
It uses the textbox itself to detect natural line breaks and thus implicitly caters for the size of the textbox, the font, etc.
The desired line count is hard coded as a constant - not doing this would require an alternative of calculating the line height of the textbox (requiring calculations based on the font metrics and the textbox's internal line-leading size).
It only handles two "pages". But the concept could be extended simply by repeating the process based on the remainder of text that ends up in TextBoxPage2.
Private Sub CommandButton1_Click()
Const PAGED_TEXT_BOX_LINES As Integer = 5
Dim text As String
Dim i As Long
Dim textLength As Long
Dim curLine As Integer
text = TextBoxInput.text
textLength = Len(text)
TextBoxPage1.SetFocus
'add characters of the input string until the first page textbox
' exceeds maximum line count
For i = 1 To textLength
TextBoxPage1.text = Mid$(text, 1, i)
If TextBoxPage1.LineCount > PAGED_TEXT_BOX_LINES Then
'retreat cursor until we reach previous line, so we can
' detect the word that wrapped
curLine = TextBoxPage1.curLine
Do While TextBoxPage1.curLine = curLine
TextBoxPage1.SelStart = TextBoxPage1.SelStart - 1
Loop
'the remaining text after the SelStart is what
' wrapped, so stop page 1 after SelStart
TextBoxPage1.text = Mid$(text, 1, TextBoxPage1.SelStart)
TextBoxPage2.text = Trim$(Mid$(text, TextBoxPage1.SelStart + 1))
Exit For
End If
Next i
End Sub

How do I remove a control from a group?

So, I have a setup script that runs every time I open the program. I have 10 or so different fld variables that I want to add or remove from a group based on whether the number at the end of their id is less than or equal to the length of a randomly generated word, in this case known as the variable randomword. In case the letters are not in the group and therefore cannot be removed, I want to only try to remove it but if unsuccessful just move on to the next value of x. I know how to add something to a group, but not remove a single control from that group. How can I run a script so that:
for x = 1 to 10
put length(randomword) into tLen
if x <= tLen
copy fld ("letter"&x) to group "letters"
else
try:
remove fld ("letter"&x) from group "letters"
end if
end for
Everything below the else statement above is pseudocode as I don't actually know how to do that. Anyone with a working solution to this would make my day.
To delete a control from a group, use the delete command:
delete fld ("letter" & x) of group "letters"

How to search hyphenated words

How to search hyphenated words eg(Alexander-great) if this word present in search list then the output should be like Alexander-great:1 here 1 indicate that how many time the particular word present in main Scrolling field I am using the following code.
on mouseUp
put fld"MytextField"into Mytext
put fld "SRText" into myData
split myData by cr and colon
put the keys of myData into myData
repeat for each words myWord in Mytext
if myWord is among the words of myData then
if myCounts[myWord] is empty then
put 1 into myCounts[myWord]
--answer "Haii"
else
add 1 to myCounts[myWord]
end if
end if
end repeat
combine myCounts by cr and colon
put myCounts
Please use this code
if myWord contains the words of myData then
instead of
if myWord is among the words of myData then
Your code should work fine as is, though you might check out a slightly more compact version:
...
repeat for each words myWord in Mytext
if myWord is among the words of myData then add 1 to myCounts[myWord]
end repeat
...
Local variables in LC, including array variables, may be created and loaded on the fly. It is not necessary to either declare them or to create them with a default value. Please step through the handler, and watch the contents of the variable watcher.
Note that your method is not forbidden, but is unnecessarily verbose. You will become more efficient as you become more experienced.
Craig Newman

How to select a word with special characters in LIvecode

How to select a word with with special characters (eg: usa-uk). the following code will select text but it's doesn't select the words like usa-uk. How I change my code
select the mouseText
If you don't have a huge text in your field you might use:
on mouseMove
if the mouseLoc is within the rect of me then
put the mouseChunk into tChunk
# Check if chars before or after is a "spacing char"
put word 2 of tChunk into tstart
put word 4 of tChunk into tEnd
repeat with i = tStart down to 1 step -1
if matchText(char i of me, "\s") then exit repeat
end repeat
put i+1 into tStart
repeat with i = tEnd to the number of chars of me
if matchText(char i of me, "\s") then exit repeat
end repeat
put i-1 into tEnd
select char tstart to tEnd of me
end if
end mouseMove
How are you selecting? For example, if this was in the field script:
on mousemove
select the mouseText
end mouse move
You would be selecting the text under the cursor. In order to select compound text such as "usa-uk" you would have to group that text fragment. This sets the textStyle of that fragment to "link".
Craig Newman

AppleScript and .txt file

I need to have an AppleScript that will edit the contents of a given text file (generic structure) and delete the 5th character through 8th character, leave characters 9-20, and delete characters 21-32. For example:
Say this is my text file:
"
Qt&:$yp$shshshahahah$jsjsjajssjh
"
(Single line)
I would need to delete starting from the first $ to the next $, and then delete everything after (including) the last $. In this example, the end result would be this:
Qt&:shshshahahah
Thanks,
Isaac D'Keefe
You could run a command like this in Terminal:
sed -Ei '' 's/(.{4}).{4}(.{12}).*/\1\2/' ~/Documents/file
Or if you need to use AppleScript:
set p to "/Users/username/Documents/file"
set input to read p as «class utf8»
set input to text 1 thru 4 of input & text 9 thru 20 of input
set fd to open for access p with write permission
set eof fd to 0
write input to fd as «class utf8»
close access fd
I would need to delete starting from the first $ to the next $, and
then delete everything after (including) the last $. In this example,
the end result would be this:
Based on these conditions you can split an string up into chunks using a delimiter/separator. Then to my understanding you only want to keep the odd items and remove the even ones (by index). So the following script will work as you described rather than working on the index of the characters.
set theString to " Qt&:$yp$shshshahahah$jsjsjajssjh "
set {oldTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "$"}
set textItems to every text item of theString
set AppleScript's text item delimiters to oldTID
set filteredTextItems to {}
repeat with x from 1 to count textItems by 2
set end of filteredTextItems to item x of textItems
end repeat
return filteredTextItems as string

Resources