Different result if code is run directly using "run script"

This code:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "Script Debugger"
	set activeDoc to make new document with properties {name:"Test Save Script"}
	set source text of activeDoc to "display dialog \"I ran!\""
	set docName to the name of activeDoc
	set savePath to ((POSIX path of (path to downloads folder)) as text) & "Test Save.app"
	set exportFormat to enhanced application
	save activeDoc in POSIX file savePath as exportFormat with run only
end tell
tell application "System Events"
	tell application process "Script Debugger"
		tell window docName
			if exists sheet 1 then
				tell sheet 1
					repeat 200 times
						set buttonProps to (properties of button "OK")
						if enabled of buttonProps is true then
							click button "OK"
							exit repeat
						else
							delay 0.1
						end if
					end repeat
				end tell
			end if
		end tell
	end tell
end tell
tell application "Script Debugger" to close activeDoc saving no

Has the result of saving an app in the user’s downloads folder that, if run, displays a dialog “I Ran.”

This script, which is identical to the above script, except run inside a “run script:”

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set scriptText to "use AppleScript version \"2.4\" -- Yosemite (10.10) or later
use scripting additions

tell application \"Script Debugger\"
	set activeDoc to make new document with properties {name:\"Test Save Script\"}
	set source text of activeDoc to \"display dialog \\\"I ran!\\\"\"
	set docName to the name of activeDoc
	set savePath to ((POSIX path of (path to downloads folder)) as text) & \"TestSave.app\"
	set exportFormat to enhanced application
	save activeDoc in POSIX file savePath as exportFormat with run only
end tell
tell application \"System Events\"
	tell application process \"Script Debugger\"
		tell window docName
			if exists sheet 1 then
				tell sheet 1
					repeat 200 times
						set buttonProps to (properties of button \"OK\")
						if enabled of buttonProps is true then
							click button \"OK\"
							exit repeat
						else
							delay 0.1
						end if
					end repeat
				end tell
			end if
		end tell
	end tell
end tell
tell application \"Script Debugger\" to close activeDoc saving no"

run script scriptText

immediately returns the error:

I discovered this when trying to get Script Debugger to batch save scripts… trying to avoid the UI dialog sheet following the save, which I’m bypassing with UI scripting above. So I’d still be interested in bypassing that, if possible. There’s a thread on that at Late Night Software’s Script Debugger forum here: https://forum.latenightsw.com/t/cant-get-my-batch-deployment-script-to-work-with-code-signing-on/3678/2

I wasn’t aiming to cross-post that, but it’s turned into two questions:

  1. The very Script-Debugger-centric question “how can I avoid the dialog on save/export?”
  2. The question I assume doesn’t relate to script debugger in particular, "Under what circumstances does the same code behave differently when run as a “do script,” as opposed to being run directly?

I thought it was better to put question #2 here.

hello,

Without the first 2 lines of code, which are redundant, your 2nd script runs successfully for me.

As for the answer to your first question, simply close front document saving no:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "Script Debugger"
	set activeDoc to make new document with properties {name:"Test Save Script"}
	set source text of activeDoc to "display dialog \"I ran!\""
	set docName to the name of activeDoc
	set savePath to ((POSIX path of (path to downloads folder)) as text) & "Test Save.app"
	set exportFormat to enhanced application
	save activeDoc in POSIX file savePath as exportFormat with run only
	close document 1 saving no -- ADDED
end tell

That’s interesting… so without the first two lines of code, you mean:

set scriptText to "use AppleScript version \"2.4\" -- Yosemite (10.10) or later
use scripting additions

tell application \"Script Debugger\"
	set activeDoc to make new document with properties {name:\"Test Save Script\"}
	set source text of activeDoc to \"display dialog \\\"I ran!\\\"\"
	set docName to the name of activeDoc
	set savePath to ((POSIX path of (path to downloads folder)) as text) & \"TestSave.app\"
	set exportFormat to enhanced application
	save activeDoc in POSIX file savePath as exportFormat with run only
end tell
tell application \"System Events\"
	tell application process \"Script Debugger\"
		tell window docName
			if exists sheet 1 then
				tell sheet 1
					repeat 200 times
						set buttonProps to (properties of button \"OK\")
						if enabled of buttonProps is true then
							click button \"OK\"
							exit repeat
						else
							delay 0.1
						end if
					end repeat
				end tell
			end if
		end tell
	end tell
end tell
tell application \"Script Debugger\" to close activeDoc saving no"

run script scriptText

This still fails for me, with the “Error: Script Debugger got an error: AppleEvent timed out. (-1712)” error.

This may be because I’m on the Beta version of Script Debugger - Shane said a bug had been introduced that he’ll fix in the next beta.

However, your suggestion about “close saving no” right after the “save” does work… I didn’t actually have to use UI scripting to dismiss the dialog, the document does still respond to the "close’ command even though it has an attached sheet/dialog that needs to be dismissed. So that is a better solution there.

I’m still curious what behaves differently between “run script” and running the same code directly, that I get different results. It’s not critical, I’ve got a good work around. I’d just like to understand what’s going on with Applescript. In what other circumstances might I see different behavior running the same code in different ways?