Linux application in livecode - livecode

Spell Check is a default application in Linux. With the help of that application, can we check the spelling of a text field while users enter data?

Some (or many?) Linux distributions contain a command line utility that is called spell. If you run this with words as parameters, you need to press return a second time, but if you use a file as a paramater, you don't need to press return again. This means that a solution could be:
write the text of a field to a file
run the command line utility from LiveCode's shell function with the file as parameter
parse the result returned by the shell function
Before you try this, open your terminal on Linux and type spell. Press enter to see if the command is recognised. If yes, then the script below should work.
This script writes the text of a field to a file, does a spell check on the file and returns the incorrect words to LiveCode. I haven't tested the script and you may have to tweak it a little.
function spellCheck theText
// works on Linux only
if the platform is "Linux" then
// remove everything that isn't a word
put replaceText(theText,"[^\w]","") into myWords
// write clean data to a temporary file
put the tempName into myTempFile
put myWords into url ("file:" & myTempFile)
// call spell with shell
put "spell" && myTempFile into myShell
// only return the incorrect words
put line 2 to -1 of shell(myShell) into myCorrections
// return the incorrect words to calling handler
return myCorrections
else
// this isn't Linux
return "error"
end if
end spellCheck
//theField is the short name of a field
on checkField theField
// call above function
put spellCheck(the text of fld theField) into myWords
// myWords should now contain the incorrect words
if myWords is not "error" then
lock screen
// parse incorrect words and mark them in the field
repeat with x = 1 to number of words of field theField
if myWord is among the lines of myWords then
// an incorrect word has been found and is marked red
set the textColor of word x of fld theField to red
end if
end repeat
unlock screen
end if
end checkField
Usage: checkField shortNameOfTheField

Related

AppleScript, can't replace character in a text

I'm working on an AppleScript that is calling a Python script with the name of a file as an argument, something like :
set descriptionFiles to (every file of current_folder whose name extension is "txt")
repeat with textFile in descriptionFiles
-- run a Python script that clears the xml tags and reformat the text of the file
do shell script "python3 '/Users/MBP/Documents/Python/cleanDescription.py' '" & textFile & "'"
end repeat
Now the AppleScript does fine as long as I'm not encountering a file with a single quote in it's name at which point it stops and throws an error.
To correct that, I've been trying to escape the single quote in the files name before passing it to the Python script but that's where I'm stuck. I'm using this routine :
on searchReplace(thisText, searchTerm, replacement)
set AppleScript's text item delimiters to searchTerm
set thisText to thisText's text items
set AppleScript's text item delimiters to replacement
set thisText to "" & thisText
set AppleScript's text item delimiters to ""
return thisText
end searchReplace
with a call like this :
tell application "Finder"
set search_T to "'"
set rep to "\\'"
set selected to selection as alias
set textName to selected as text
set res to searchReplace(textName, search_T, rep)
end tell
Using the code above on a single file throws an error on the searchReplace(textName, search_T, rep) part, with a number of -1708
Any ideas ?
The most reliable way to escape special characters in AppleScript is quoted form of. It handles all forms of quotation smoothly. Never do it yourself. It's also a good practice to quote paths always in do shell script lines even if they don't contain spaces.
Another issue is that textFile is supposed to be a POSIX path rather than a Finder specifier. And get the path to the python script once
set pythonScriptPath to POSIX path of (path to documents folder) & "Python/cleanDescription.py"
set descriptionFiles to (every file of current_folder whose name extension is "txt")
repeat with textFile in descriptionFiles
-- run a Python script that clears the xml tags and reformat the text of the file
do shell script "python3" & space & quoted form of pythonScriptPath & space & quoted form of POSIX path of (textFile as text)
end repeat

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

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

Append text to the end of a line

I'm working with AutoIt, and I was wondering in there is a method I can use to append a string of text to the end of a line inside a text file. I've been browsing all over autoit forums and there are lots of answers that are really close, but I have not found a solution that has actually worked for me.
The function:
FileWriteLine($LOG, "FText")
just adds a whole new line at the bottom, while the function:
_FileWriteToLine($LOG, 1, "FText", 0)
adds the letters "FText" to the beginning of the first line in the log file.
Is there any way I can add this text to the end of the first line, instead of the beginning?
I have never come up with this problem but just thinking of it, how about reading the whole line, storing it in a variable, add the extra test you want in the end of the line and then write the new line as it is modified replacing the old line???
You can use the FileWrite function: FileWrite documentation
You can create your own file writing function to include the opening, writing, and closing of the file like this:
Func WriteToLog($FileName, $Value)
$FileHandle = FileOpen($FileName, 1) ; 1 = append mode
If $FileHandle <> -1 Then
FileWrite($FileHandle, $Value)
EndIf
FileClose($FileHandle)
EndFunc
Then using your example and assuming $LOG is the file name for your log file, you can simply call your function whenever you need to log something:
WriteToLog($LOG, "FText")

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

vim how to create a debug message fast

I am using ruby on rails but that does not matter much for this question. Let's say that i have a statement like this
error = 'this is an error message'
I have noticed that I end up doing this a lot
error = 'this is an error message'
puts "error = #{error.inspect}"
I am sure a macro can be written which would take the work on the left hand side of left most = and then create another line along with template shown above.
I am using mvim on mac. Any pointer in terms of where I should start to look for developing what I want.
Try snipmate:
http://www.vim.org/scripts/script.php?script_id=2540
I recorded a simple macro that does your sample. To record a macro type q followed by what register you want the macro to be put in (convention calls for qq). To play the macro type # then the macro register. You can view this at :help recording
To write the macro, use the following commands (and here is how is should look in the register)
^yEoputs "error = #{^Op.inspect}"^[
^ moves to the first non whitespace character of the line
yE yanks to the end of the space separated word.
o Puts you in insert mode on the next line
puts "error = #{ is the text that you type out
^O is ctrl+O (capital letter o) - this allows the next, and only the next command to be run in command mode, which is...
p Puts the yanked word, after this command is run you're still in insert mode
.inspect}" is the text that you type and finally...
^[ is Esc
I would go for:
nnoremap µ :s/^\s*\(\k\+\)\s*=.*/&\rputs "\1 = #{\1.inspect}"/<cr>
:s presents the advantage of doing the job plus matching the assigned variable if any. Doing the same thing with classical commands like yw, p, etc would be more cumbersome.
If the template become more complex, we can rely on template-file expanders as long as they easily permit to call viml function like matchstr(). Of course, in that case I would use mu-template with the following template-file:
VimL:" $Id: {rtp}/template/ruby/inspect.template
VimL: let s:value_start = '¡'
VimL: let s:value_end = '¡'
VimL: let s:reindent = 1
VimL: let s:marker_open = '<+'
VimL: let s:marker_close = '+>'
VimL: let s:varname = matchstr(getline(line('.')-1), '^\s*\zs\k\+\ze\s*=')
VimL: if empty(s:varname) |throw "the previous line don't assign any variable" |endif
puts "¡s:varname¡ = #{¡s:varname¡.inspect}"<++>
VimL:"vim: encoding=utf-8
If you're doing these on the fly, a snipmate snippet could look like this:
${1:error} = '${2:error message here}'
puts "error = #{$1.inspect}"
If, on the other hand you're just wanting to output pre-existing variables for debugging purposes. Nick-Canzoneri's macro may be more useful.

Resources