Odd stuff I don't understand about scripts within scripts

I’ve been working in Applescript for 2 or 3 years now and have developed a set of AppleScripts to enhance Phase One’s “Capture One” application.

I’m now starting to think of using scripts within a script and I see a couple of things that puzzle me about a script that is part of a script file. I have included an example script “Script 1” at the end of the post.

  1. What’s the right terminology to describe this relationship?

  2. Logging from within the script doesn’t generate any output in Script Editor’s log window

  3. I can pass global variable values into the sub-script, but I can’t pass them back.

Why does this happen?

Is there any way of logging into Script Editor’s log window from a sub-script? I’ve tried telling various entities to log, but without success. Not being able to log is problem in debugging, and it also eliminates a useful way to return interim results.

Is there anyway of using a script to change the global variables of the top level script?

My apologies for the long post. Thanks for any insight that you can give.

Script 1


use AppleScript version "2.5"
use scripting additions
global testv1

set testv1 to "kkk"

display notification "Starting testScript1"
log "Starting testScript1"
run script testScript1
log result
log testv1

return
######### the end

script testScript1
	set theStartString to "" & (get name of me) & " started from " & (get name of (get properties of my parent))
	
	display notification theStartString
	log theStartString
	## This log message never shows up
	try
		testv1
		display notification ("Found testv1 value = " & testv1)
	on error
		display notification ("Did not find testv1")
	end try
	
	set testv1 to "aaa"
	## This new value never makes it back to the main script

	display notification "testScript1 stopped"
	log "testScript1 stopped"
	## This log message never shows up

	return "testScript1 ran"
end script

Background
The context for my question is that in the GUI that I have developed to set script settings, I would like to be able to change other dependent variables after the user makes changes.

I have removed all the complex (and useful logic) from the script and reduced it to barebones that demonstrates what I’m trying to accomplish.


use AppleScript version "2.5"
use scripting additions

global testv1, testv2, testv3, MainScriptRef
set testv1 to "kkk"
set testv3 to "mmm"
set testv4 to testv1 & testv3

log {"testv1", testv1, "testv3", testv3, "testv4", testv4}
GUIsetup()
GUIhandler(result)
log {"testv1", testv1, "testv3", testv3, "testv4", testv4}
##!!!!  the value of "testv4" is not making it back to the global variables.

return
############ the end

on GUIsetup() -- GUI setup for this specific Script
	global testScript1
	set setting_list to {}
	set end of setting_list to {sName:"Test1", sValue:(a reference to testv1), sScript:"Display Notification \"Updated testv1\""}
	set end of setting_list to {sName:"Test3", sValue:(a reference to testv3), sScript:testScript1}
	return setting_list
end GUIsetup

on GUIhandler(theSettingList) -- general purpose GUI handler
	log theSettingList
	repeat with theRow in theSettingList
		try
			set dialog_result to display dialog ("New Value for " & (get theRow's sName)) default answer (get theRow's sValue)
			set contents of theRow's sValue to get (text returned of dialog_result)
			run script theRow's sScript
		end try
	end repeat
end GUIhandler

script testScript1
	property mytestv : a reference to testv4
	display notification "Updated testv3"
	set mytestv to testv1 & testv3 
	##!!!!  the value of "mytestv" is not making it back to the global variables.
	display notification "Updated testv4 " & mytestv
end script

Model: Late 2015 27" iMac
AppleScript: 2.5
Browser: Firefox 64.0
Operating System: macOS 10.12

Hi.

Leaving aside whether or not you actually need to use a script within a script, the reason you’re not seeing what you’re expecting is that you’re using the StandardAdditions’s run script command to run the inner script instead of the AppleScript language’s run command. run script uses its own instance of AppleScript, which isn’t the instance being used by Script Editor to run the main script. I don’t want to go too deeply into the technicalities because I’m not sure of them myself :rolleyes:, but if you use run instead, I think you’ll get what you want:


use AppleScript version "2.5"
use scripting additions
global testv1

set testv1 to "kkk"

display notification "Starting testScript1"
log "Starting testScript1"
run testScript1 -- Not run script testScript1
log result
log testv1

return
######### the end

script testScript1
	set theStartString to "" & (get name of me) & " started from " & (get name of my parent)
	
	display notification theStartString
	log theStartString
	## This log message never shows up
	try
		testv1
		display notification ("Found testv1 value = " & testv1)
	on error
		display notification ("Did not find testv1")
	end try
	
	set testv1 to "aaa"
	## This new value never makes it back to the main script
	
	display notification "testScript1 stopped"
	log "testScript1 stopped"
	## This log message never shows up
	
	return "testScript1 ran"
end script

Thanks Nigel, that makes sense and is exactly the information I was seeking.

I agree that running a script might not be the best choice, I’ve realized that another choice is to call a handler within a script (as is done in some of your sort handlers), and I’ve just realized there is yet another possible choice, to pass an index value to some other handler which has a code segment for each index.