i am working on my first script and I have bumped into a problem.
I want to be able to choose a text document and then insert text - based on user input.

ex before:
this is the first block of text.
this is the last block of text.

ex after user input:
this is the first block of text.
user input text.
this is the last block of text.

ex after more user input:
this is the first block of text.
more user input added after.
user input text.
this is the last block of text.

I assume there is a way to insert based on the line…
insert text after line 1 would always insert input on line 2 after line 1.

thanks for any help you can offer.


Here is kind of a simplistic approach that will work well if
the log doesn’t ever get incredibly huge. There are other ways
of doing this, especially if you consider using a scripting addition
or two. This method is uses nothing but commands from the
Standard Dictionary.

Here’s the concept:
I’m treating the beginning and ending line as a header and a
footer. In the solution I tested my header looked something
like “========HEADER=======>>” I picked the end of the
header to be something the user will probably never enter into
the log.

Then, we get the location of “=>>” --this is where we’ll start writing
Also, get whatever comes after the header, we need to put that back in the log.

Here’s the code.

set logFile to "Mac HD:Desktop Folder:InsertText.log" --path to the log file
set logText to read logFile as text --read the text
set logEOF to (get eof file logFile) --get the end of the file

set startPoint to (offset of "=>>" in logText) + 3 --this is the position of your header ending
set logEnd to characters startPoint thru logEOF of logText as string --get all characters after your header ending

set userInput to display dialog "Enter your text here" default answer "" with icon note --capture user input
set userInput to return & the text returned of the result --coerce to text and prepend a return

write userInput & logEnd to logFile starting at startPoint --write the user input and the rest of the log just after the header ending

Again, if your log gets too big this might not be the best approach as it reads the entire contents of the text file every time. Maybe someone has something a little more efficient?

This works and has been tested under OS 9.2.1

Hope it helps,