Empathic script [was Re: Evaporating properties]
Empathic script [was Re: Evaporating properties]
- Subject: Empathic script [was Re: Evaporating properties]
- From: Nigel Garvey <email@hidden>
- Date: Thu, 8 Feb 2001 16:31:39 +0000
[This relates to my parallel posting "Haunted script"]
"Marc K. Myers" wrote on Tue, 06 Feb 2001 17:34:24 -0500:
>
I wrote the following script to test how an applet could create another
>
applet and fill it with a script object:
>
>
global thePass, newFile
>
>
on run
>
set thePass to 0
>
end run
>
>
on idle
>
set thePass to thePass + 1
>
if thePass = 1 then
>
set theCnt to 16
>
set newScript to makeScript(theCnt as text)
>
set myFile to (path to me)
>
tell application "Finder"
>
set newFile to (duplicate myFile) as alias
>
set name of newFile to "New App"
>
end tell
>
store script newScript in newFile with replacing
>
return 2
>
else if thePass = 2 then
>
ignoring application responses
>
tell application (newFile as text) to run
>
end ignoring
>
return 2
>
else
>
tell me to quit
>
return
>
end if
>
end idle
>
>
on makeScript(theText)
>
script test
>
property occCnt : theText
>
on idle
>
activate
>
display dialog "It happened " & occCnt & " times."
>
set occCnt to ((occCnt as integer) + 1) as text
>
return 4
>
end idle
>
end script
>
end makeScript
(This script makes a duplicate of its own application file, renames the
duplicate, stores the script produced by the makeScript() handler into
it, tells the newly created app to run, then quits. The new script app
carries on displaying dialogs with an increased value of 'occCnt' on each
idle call. Marc's problem was that the new script ran perfectly when
first created, but immediately threw a type 1 error if it was run again
by itself.)
Having concluded that the strange behaviour of Marc's script was due to
the value of the property 'occCnt' not being stored with the generated
script, I worked out a solution whereby the script would be generated
with a 'run script' command, so that the property's initial value could
be 'hard wired' into it. In the event, it proved necessary to get the
generated script to initialise the property (or more conveniently, a
global) itself on its first run:
on makeScript(theText)
run script ,
"script test
global occCnt
try
occCnt
on error
set occCnt to " & theText & "
end try
on idle
activate
try
display dialog \"It happened \" & occCnt & \" times.\"
on error number -128
quit
end try
set occCnt to occCnt + 1
return 4
end idle
end script
return test"
end makeScript
This worked well and I passed it on to Marc. But then I found that it
didn't work on my other machine (the 'run script' command throwing a 'Bad
script' error) and Marc reported back that it didn't work on his either.
It turned out that it only worked for me if Script Editor was still open
from having loaded the 'mother' script - even if that particular window
was now closed.
Exploring the 'catalyst' or 'empathy' idea, I found that the 'run script'
also behaved if it was enclosed in a 'tell' block addressed to another
application. Even a non-scriptable one like Stickies would do, though the
Finder was obviously more convenient. This works on both of my computers
(Mac OSses 8.6 and 9.0.4) but apparently still errors on Marc's (Mac OS
8.1).
So:
Why does 'run script' balk in the first place?
Why does a 'tell' block or an open Script Editor cure it for me?
Why doesn't a 'tell' block cure it for Marc?
Is any of this related to the peculiarities described in my other post?
NG