Converting script to text, modifying it, then making it script again?

I’m trying to make a way for scripts to modify other scripts in ways similar to what scripted text-editors can be made to do on text files. Unfortunately, my programming instincts tell me the best way to do that would be to make some new AppleScript commands, which too far beyond my limited grasp of AppleScript for me to do, just yet.

So, until someone far brighter than myself gives AppleScript a bunch more commands to use, I intend to make do with the next best thing: scripts that can be called on to modify other scripts.

My understanding of AppleScript, AppleScript resources, and programming in general tells me that scripts that work other scripts like they were text aren’t at all impossible to make, or at least they shouldn’t be. Given that .scpt files can be reduced to text editor friendly .applescript Unicode text files, Unicode .txt files and .applescript files can be turned into .scpt files by Script Editor, and a there is a Script Editor command to compile a not yet compiled script.

I’m trying to (for a start) make a script that can accept a target script, a target string, a replacement string, a boolean variable stating whether or not the target is to be replaced, and (if the original is not being replaced) an address (or location) and string name for the new script. Basically, a search and replace function that scripts can use on other scripts

Here’s what I have so far:
Edit: Forgot to apply what I recently learned about properties.

--This script needs to:
--1. Get the info it needs
--2. Convert the target script into a .applescript text file, probably
--3. Use a search and replace script on the .applescript text file
--4. Convert the adjusted .applescript back into a .scpt and either replace the original or save it as a new script
--5. Compile the modified script if asked to
--6. Delete failures if asked to
--7. Do anything else that might make this script more useful

--This script ought to require some information when called:
--1. What is the targeted script?
--2. What is the target of replacement?
--3. What is the replacement to be?
--4. Is the new script to replace the original or be saved as a new script?
--5. If this script is not replacing the original, what is the new script's name and where is it located?
--6. Should this script compile the modified script after the modifications are made?
--7. If this script is to compile the modified script, should it delete the modified script if it produces an error when compiled?

--The target script should be stored in targetedscript if possible.
--temporaryfile should hold the name and/or address of the file that holds the-
--code as unicode for the text working processes to work on.
--adjustedlocation should be the location of the new script if one is wanted.
--newscript should store the new script's name if a new one is being made.
--TrashtheOldOne ought to be a boolean variable that is true if the original should be replaced and false if it isn't.

property targetedscript : "Example:Script.scpt"
--Note: The target script must not be Read-Only.
property temporaryfile : "Example:Script.applescript"
property TrashtheOldOne : True
property newLocation : "Example:NewLocation:"
property newName : "New Name"
property compilenow : True
property trashthefailures : False

--Third, temporarily convert the script into a Unicode text (.txt) document.
tell application "Script Editor"
	save targetedscript as "text" in temporaryfile
end tell

--Fourth, use Unicode text manipulator of choice
--to manipulate the code stored as Unicode Text.
--Except I haven't found one I can get working properly.
--Wide open to recommendations for this part.

--Fifth, save the adjusted .applescript file as a .scpt
if TrashtheOldOne then
	tell application "Finder"
		delete targetedscript
	end tell
	tell application "Script Editor"
		save temporaryfile as "script" in targetedscript
	end tell
	tell application "Script Editor"
		save temporaryfile as "script" in newLocation & newname
	end tell
end if

--Last, (if desired) compile the new script and check for errors
--If there are any errors, then report the errors and also (if desired) reverse the changes and delete the new script if there is one.
--I don't now how to do all that just yet so I'll just try to give someone smarter than me a running start at it.
if compilenow then
	tell application "Script Editor"
		if TrashtheOldOne then
			compile targetedscript
			compile newscript
		end if
	end tell
end try
end if

There are probably hundreds of things wrong with that mess, but all I know for certain myself is that it won’t run properly. Of course, having unfinished sections in there doesn’t help it any, but some if not all of the finished sections don’t work either. I could really use some help with this. The biggest problem I’m having right now is getting the conversions done with a script. I know that Script Editor can do it, but I can’t get a script to get it to. I’d like to not have to have Script Editor open to use this script. The idea is that this script is like a utility for other scripts to use.

If there’s a faster and simpler way to let a script work another script like a quality text-editor could I’d be glad to hear and see it.

Model: eMac
AppleScript: 1.10.7
Browser: Firefox 3.0.10
Operating System: Mac OS X (10.4)

Open Terminal. At the entry prompt, type “man osacompile” (without the quotes). After you’ve read that, get out of the “man” program by hitting “Q”. Now type “man osascript”. (use “Q” to get out).

Apple provided the way to compile and run applescripts from the command line. The only caveat is that you can’t directly solicit user input. That means you can’t use “display dialog” or other input/output-related commands UNLESS (you knew there was an exception, right?) you use a Tell block and address the commands to an application.

osacompile, especially, should help you convert scripts to compiled scripts without having to use Script Editor. By using “do shell script” you can run script commands without a visible sign to the user, also.