This is not a new topic here .. but I really need some insight here


	set myScriptFile to "someLocation:whatever_title.scpt"
	try
		set filesScriptRef to alias myScriptFile
	on error -- file doesn't exist, so create it
		set theContent to "set d to someNumber" -- doesn't matter what I put in this string
		
		set tempRef to open for access file myScriptFile with write permission
		write theContent to tempRef
		close access tempRef
		
		set filesScriptRef to alias myScriptFile
	end try

Given that this file does not exist, the on error code kicks in as it should. But, even if I just open for access, followed immediately by close access, with zip inbetween, I get the following error:
“script doesn’t seem to belong to AppleScript”.

I’m guessing that the “.scpt” suffix is bugging AppleScript … but what?

Hi John,

you’re going to create a plain text file (.txt), not a compiled script.
If you want a script (.scpt), compile it.


set myScriptFile to "someLocation:whatever_title"
try
	set filesScriptRef to alias (myScriptFile & ".scpt")
on error -- file doesn't exist, so create it
	set theContent to "set d to someNumber" -- doesn't matter what I put in this string
	
	set tempRef to open for access file (myScriptFile & ".txt") with write permission
	write theContent to tempRef
	close access tempRef
	do shell script "/usr/bin/osacompile -o " & quoted form of POSIX path of (myScriptFile & ".scpt") & " " & quoted form of POSIX path of (myScriptFile & ".txt")
	do shell script "/bin/rm " & quoted form of POSIX path of (myScriptFile & ".txt") -- remove text file
	set filesScriptRef to alias (myScriptFile & ".scpt")
end try

Hi.

This can also be done fairly simply with StandardAdditions Scripting Commands:

set myScriptFile to "someLocation:whatever_title.scpt"
try
	set filesScriptRef to alias myScriptFile
on error -- file doesn't exist, so create it
	set theContent to "set d to someNumber
	set e to \"some string\""
	
	run script ("script" & return & theContent & return & "end script")
	store script result in file myScriptFile
	
	set filesScriptRef to alias myScriptFile
end try

Hi Nigel,

Thank you very much for posting this code snippet!

I am currently working on an AppleScript system, where one script generates job tickets from incoming eMails which are then read by another server-like script to create certain documents.

Until now the job tickets were plain text files with UTF-8 text encoding, which had to be parsed and checked by the server-like script in order to retrieve the saved values. But now I will rewrite the code to generate AppleScript files as shown in your code example, which will make the whole system quicker and a lot easier (no parsing step, just «load script»).

If I just had thought about this solution a little bit earlier :smiley:

I ditto Martin’s atta-boys …

Curious, when I used:

script storeFiles
	set d to someNumber
end script
 -- followed by
store script storeFiles in file myScriptFile

the resulting new file “whatever.scpt” was a biggee, about 430K, and would go down to what it should have been, 4K, only after I went into Script Editor to save it.

Nigel’s

run script ("script" & return & theContent & return & "end script")

effectively creates and saves/compiles in one step, so I only see the 4K size.

Thank you, both. :slight_smile:

I suspect ” though am not totally sure ” that it’s something to do with inheritance. Your ‘script storeFiles’ is instantiated (brought into being) directly by ” and in the context of ” the main script and inherits its variables and their values. I think this information’s stored in the script file, even though it’s not used and may not be usable within the variables’ scope anyway.

run script compiles and executes script text without reference to the calling script, so my code instantiates a script object that has no knowledge of the main script’s environment and thus no extra information to be stored.