Find and replace not text but applescript

I have and applescript application (SCRIPT_A) this will be in the login items and run on boot.
Inside SCRIPT_A is SCRIPT_B in the resources,

SCRIPT_A looks like this

set input_text to text returned of (display dialog “Input here what needs to be added to SCRIPT_B:” default answer “”)

SCRIPT_B looks like this
say xxxxx

Get the input_text of SCRIPT_A from the user and change xxxxx of SCRIPT_B to

say “as entered from SCRIPT_A”

but then Save to SCRIPT_C located in the homefolder.

Hope that simplifies what I am asking

I tried this> but I think i only copes with actual TEXT not applescript

set thefile to (path to resource "B.scpt" in directory "Scripts")
set input_text to text returned of (display dialog "Input here what needs to be added to SCRIPT_B:" default answer "")

findAndReplace("XXXXX", input_text, thefile)

on findAndReplace(tofind, toreplace, TheString)
	set ditd to text item delimiters
	set res to missing value
	set text item delimiters to tofind
	repeat with tis in text items of TheString
		if res is missing value then
			set res to tis
		else
			set res to res & toreplace & tis
		end if
	end repeat
	set text item delimiters to ditd
	return res
end findAndReplace

Hello.

Please look at man osacompile and man osadecompile.

It is either that, or grab the text out of a script window/document in some editor.

Hello.

I think I got what you mean now, but still, is it the same item in script B that is to be changed on a regular basis, and the saved as script C? -And if it is like that, what does the piece in script B, that is to be replaced look like?

Here is a little script that will do what you want.
Your find-and-replace should replace the ‘modifying’ block of code
And of course adapt the reading/writing code to use A’s Resources folder.

Hello.

I have another idea, it is fairly dynamic.

In your script, you have a script object, with the varying statements, that you run each time you run the script. The script object you run, is just a text string, that contains the necessary items. Then varying the text string shouldn’t be any problem really.

Very small example:


	set a to "script e
property d:0
set d to 10
end script"
	run script a

I have thought that you’d reference, or set a reference to the script object inside B, you can also have the whole thing happening inside B, and just change text properties of the B script, to compile the script with different values.

You can of course build up the “script object” of as many concatenated variables/properties as you like, but I think you’ll have to run a handler inside the script b, in order for the changed properties inside it to stick, before you save the whole thing into script c.

I hope you understand how I think. :slight_smile:

Edit

Here is an example that works when more happens than just assignments:

script b
	property country : "Norway"
	
	run script "say \"[[emp +]]" & country & "...[[emp +]][[emp +]]10 points\""
	
end script

tell b to run
set b's country to "United Kingdom"
tell b to run

Hello.

I don’t think you’ll have to run it in order to save the new version of a script, but you’ll have to construct it so it forms a closure.

That is, you’ll need to have a handler, that returns a script the same type as script b, that script needs to reference a variable you have in your script a, that must be a global.

You then concatenate the global in script a, to look like the script you want, then you assign a new copy of the script to some variable, which then is bound with the value of the global, before you save it. When you then load and run script a, it should contain the value of the global, for running the script with the bound global text script as the script to run.

Here is an example of what I was trying to say above, as a script may say more than a thousand words. :slight_smile:


global a
to makeScript()
	script aScript
		property b : a
		display dialog b
	end script
end makeScript

set a to "Hi"
set b to makeScript()
set a to "I am"
set c to makeScript()
set a to "A closure"
set d to makeScript()
tell b to run
tell c to run
tell d to run

Edit
The run statements in the code above aren’t necessary, before storing the script objects, they only serve as a proof of concept.