Script That Edits Scripts?

How do I write a script that writes or edits other scripts? I know that scripts can be written to order Script Editor to compile scripts, but I can’t find any way to, for example, replace a file address in a script with a different one. I’m new to Applescript (and scripting in general) but not terribly new to programming. I’ve searched for the answer for a while now, and I’d bet the answer is something incredibly obvious but I just can’t find it.

set the targetscript to "This:Is:An:Example:Script.scpt"
set the targetfile to "This:Is:An:Example:File.rtf"
set the replacementfile to "This:Is:Another:Example:File.rtf"

--script editing lines go here?

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

Let’s assume you have an AppleScript file on your desktop named mylib.scpt. This script just contains the following line:

property myname : "TMBG"

Now you can alter this script file by another AppleScript file with code as follows:

set mylib to load script ("Macintosh HD:Users:martin:Desktop:mylib.scpt" as alias)
display dialog mylib's myname -- will display TMBG
set mylib's myname to "They Might Be Giants"
-- saving the loaded script
store script mylib in ("Macintosh HD:Users:martin:Desktop:mylib.scpt" as alias) with replacing
-- loading it again
set mylib to load script ("Macintosh HD:Users:martin:Desktop:mylib.scpt" as alias)
display dialog mylib's myname -- will now display "They Might Be Giants"

Happy Scripting!

Edit: Thanks Martin! Didn’t notice your reply 'til after I’d posted mine though.

After pondering scripts that adjust other scripts for a while, I decided that it could be useful. In fact I’ve come up with a use. Let’s say we have a script template like this:

tell application "Finder"
	set theTarget to "TargetFile"
	set theWords to "TheWordsYouWant"
	repeat with anItem in theTarget
		set the comment of anItem to theWords
	end repeat
end tell

A script that uses the template could use the script-adjusting script to change “TargetFile” and “TheWordsYouWant” to whatever is wanted.

I freely admit that this whole idea may be useless or unnecessary, but I still think it’s a little odd that applescript can do so many things except write or modify applescripts.

I’m now trying to make a script that can both temporarily and permanently adjust other scripts. The main goal is to have a script that can be called by other scripts, applications, etc., have some information passed to it (the target script, the part of the target to replace, the replacement, replacing the original or not, and, if not replacing the original, the name of the new script and the new script’s location), and make the desired adjustments.

This is what I have so far:

--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?

--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

--The target script should be stored in targetedscript if possible
--temporaryfile is the name and address of the file that hold 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
--trashit ought to be a boolean variable that is true if the original should be replaced and false if it isn't

--Somehow convert the target script to unicode text
--the next three lines don't do that right for some reason
tell application "Script Editor"
	save targetedscript as "text"
end tell

--Tell Finder to replace the original if that was what is wanted
if trashit then
	tell application "Finder" to delete targetedscript
	tell application "Script Editor" to save temporaryfile as "script" in targetedscript
	save temporaryfile as newscript
end if

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

Edit: Just realized this should be in a new topic.
Edit: Just realized that the question had already been answered. Feel rather dumb for not realizing it sooner.

Another question, how do I get a script to pass values to another script?

I have this script here (thank you Martin), but how do I get it to receive values and targets from another script?

--when run, this script wants to know the targeted script, the variable to change, and the new value for the variable. Or it would if I knew how to get it to, that is.

set targetedscript to load script (scriptaddress as alias)
set targetedscript's target to newvalue

store script targetedscript in (scriptaddress as alias) with replacing

Edit: Scripts can modify properties of other scripts, so setup targetedscript, scriptaddress, and newvalue as properties.
Of course, since scripts can modify other scripts’ properties, that renders the above script unnecessary. Silly me for not realizing it sooner.

It’d be nice to be able to use scripts to write, rewrite, and manipulate scripts, but I’m beginning to think that might be a little advanced too for me right now. Because the way I see it now, the best way to have scripts altering or making scripts is to make a scripting addition for that and I don’t have the slightest idea how to do that.