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.
-
What’s the right terminology to describe this relationship?
-
Logging from within the script doesn’t generate any output in Script Editor’s log window
-
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