<< Append: Script to append text to one or more lines >> name QSLUtilAppend group "&Utility" command "&Append Text"; << The following subroutine does most of the work for appending text. This subroutine can be used stand-alone as a utility routine by other scripts. This subroutine assumes no user interface. One problem with this implementation is that you cannot "undo" appending to multiple lines in a single operation. It's not clear how to program around this. >> sub appendtext(file, startline, endline, text) repeat for lineno from startline to endline -- Position the caret at the end of the line: file.select(startline: lineno, startcolumn: file.recordlength); -- Insert the new text at the current position file.insert(text); endrepeat endsub sub gettext result = dialog("Enter the text to append", 1, ""); if result.button = 1 then return result.enteredtext; else return "" endif endsub << UI for appendtext. Check that there is a foreground document. If there is not a selection, assume the whole file. Report errors and if everything is okay append the text to the appropriate lines. >> sub appendactivedocument appendfile = qedit.activefile; if not exists(appendfile) then result = dialog("There is no document to append lines to"); else thetext = gettext(); if length(thetext) > 0 then theselection = appendfile.selection; startcolumn = theselection.start.column; if length(theselection) = 1 then -- Append to the entire file startline = 1; endline = appendfile.linecount; else startline = theselection.start.line; if theselection.end.column = 0 then endline = theselection.end.line - 1; else endline = theselection.end.line; endif endif appendtext(appendfile, startline, endline, thetext); -- Restore the selection appendfile.select(range: theselection); endif endif endsub appendactivedocument();