The size of scripts

What’s with the size of script files?

This simple little number:

tell application "TextEdit"
	set dialogString to ""
	set textBlob to ""
	repeat with n from 1 to (count of documents)
		set textBlob to text of document n
		set dialogString to ((dialogString & (name of window n) as string) & ": " & (count of textBlob) as string) & return
	end repeat
	display dialog dialogString
end tell

is something like 1.8 megabytes.

Granted, we live in an age of affordable terabyte drives, but I’m an old dinosaur who “splurged” on a second 400K drive on my first Mac. 1.8 megabytes seems awfully large for a 322-character script. Is AS saving variables? Is it saving previous versions? Is there a trick to keeping script files slim and trim? Should be reset variables on exit?

doug

On my machine, that’s about 2.6KB as a script file and 73.6KB as an application bundle.

Properties are saved, but other variables are not.

To augment Bruce’s reply, properties and their current values are saved back to the script file after each run of the script (except when the script’s run in Script Editor). Not only properties, in fact, but also globals and what are know as “top level variables”. Top level variables are those that are used at the “top level” of the script ” ie. in the (implicit or explicit) run handler.

If the above is your entire script, it’s all within an implicit run handler, so dialogString, textBlob, and n are all top level variables. The value of textBlob at the end of the run is the whole of the text of the TextEdit document. This is saved back to the script file, so it’s the likely cause of your bloat.

The values of local variables are not saved back to the file, so you could explicitly declare the variables as local:

local dialogString, textBlob, n

tell application "TextEdit"
	set dialogString to ""
	set textBlob to ""
	repeat with n from 1 to (count of documents)
		set textBlob to text of document n
		set dialogString to ((dialogString & (name of window n) as string) & ": " & (count of textBlob) as string) & return
	end repeat
	display dialog dialogString
end tell

I personally find it more convenient to put the “top level” action into an ordinary handler, where variables are local anyway unless declared otherwise. The handler call is then the only thing in the implicit run handler. (I use the name main() by analogy with the C programming language, but it has no significance in AppleScript):

on main()
	tell application "TextEdit"
		set dialogString to ""
		set textBlob to ""
		repeat with n from 1 to (count of documents)
			set textBlob to text of document n
			set dialogString to ((dialogString & (name of window n) as string) & ": " & (count of textBlob) as string) & return
		end repeat
		display dialog dialogString
	end tell
end main

main()

If you find you have to use a property or a global to hold a value that’s likely to be very long, a third trick (assuming you don’t need the value next time the script’s run) is to set the affected variable(s) to something very short immediately before the script’s exit point:

-- Blah blah blah

set myBloatedTopLevel to ""
set myBloatedGlobal to ""
set myBloatedProperty to ""

-- End of script.

I figured it had to be something like this. What I shared was the simplest example. I have another script that 7.5 megs! I’ll add a closing routing that resets the larger variables to “”

thanks