read and execute code from external file?

just as the subject says. i need to read from a text file and execute the code inside it.

Model: iMac 21.5" 2010
AppleScript: 2.2.3
Browser: Safari 6
Operating System: Mac OS X (10.8)

run script theText

thanx. had no idea it was so simple. :smiley:

tried this. does not do what i want. i want it to run the text from the file inside the current script so that variables defined in the file are available to the script.

If the “text” is compiled AppleScript code you can load the script as a script object with the load script command and have access to its handlers and properties/variables

if the compiled script has a different extention to the normal .scpt would it still work?

Can’t you try such questions yourself :wink:

But it’s an instantiated object which means it’s properties aren’t persistent with load only. You need to load an run some code to set it’s variables. That last bit can be stored inside the object of course, like an init handler.

Are you maybe looking for a way to have preferences for your script?
A text file can work, but another method might be easier to maintain.

Help us to help you, and describe your project in plain english.

Its kind of a secret actually. :wink: basically, i need to have a file that will be chosen at startup which contains space seperated values that i want to read and convert all the words of (which will actually be numbers) into a list with the space as a seperator and then do an if statement looking like if variable is a item in list then do som

Ah. You want to read data, not code.

Then why not store them as a list in the first place?

No can do. An AppleScript list uses the comma as separator.

You are already coding here. Much too soon, methinks. First describe the process, then code.
And do what, then?

Your first post did not tell us what you were trying to. Your 2nd post made it clear you wanted to read data, but not much more. I suggest you write out the steps, in short, simple sentences, one per step. No offense, but the typing hands seem to have had a hard time trying to keep up with the racing brain :wink:

Something else I got from your second post: you just started with AppleScript, right?
If so, consider that writing down in plain english what your script should do is a learning device.

My apologies if I have offended you in any way. I just ‘got’ something from your 2nd post, and tried to put that into ‘light’ words.

Finally, some pointers.
First, you should look at the read/write commands of the Standard Additions osax. Go to the Unscripted section if you don’t know what that means.
Second, figure out in what form the data are easiest to manage.

To get every line separated use

every paragraph of theData

theData is a variable that is holding the contents of a file. To separate every item on a line which are separated by space

set AppleScript's text item delimiters to space
set theList to every text item of "hello world! how are you . today?"
set AppleScript's text item delimiters to ""
return theList

Both scripts combined could be look like this:

set theData to "hello world! how are you . today?
I'm fine thank you. Goodbye !"

set theList to {}
set AppleScript's text item delimiters to space
repeat with thisLine in every paragraph of theData
	set end of theList to every text item of thisLine
end repeat
set AppleScript's text item delimiters to ""
return theList

I don’t know which programming language you know but text item delimiters is like IFS in bash.

got it working.