Test whether script is run-only, and change variable accordingly?

I have a script that I distribute in run-only form. For retesting purposes, I change some variables while running it in Script Debugger, and then change them back to export in run-only mode. Is there a way to make a script do something like this:

if run-only then
   set myVar to false
else
   set myVar to true
end if

Probably this is something everyone knows, but I can’t find anything about it.

Something like this?

try
	do shell script "osadecompile " & quoted form of POSIX path of (path to me)
	set run_only to false
on error errMsg number errNum from offendingObject partial result resultList to explectedType
	if (errMsg contains "errOSASourceNotAvailable") then
		set run_only to true
	else
		error errMsg number errNum from offendingObject partial result resultList to explectedType
	end if
end try

set myVar to (not run_only)

Nigel,

That is brilliant. Thank you!

Script Debugger saves the file Script Debugger.plist to an applet’s /Contents folder only when the script is not run-only, so you could test for the presence of that:

try
	((path to me as string) & "Contents:Script Debugger.plist") as alias
on error
	-- must be run only
end try

Shane, that’s equally effective, and I suppose a microsecond or two faster than Nigel’s test. It’s always gratifying to see two or more AppleScript solutions to the same problem. Thank you!

But if the script’s not an applet or wasn’t created with Script Debugger …. (OK. We can assume it was created with Script Debugger in the “Script Debugger” forum. :grin: )

Nigel’s method is the most universally applicable, but for anyone who insists on working in Script Debugger, Shane’s solution gets the job done…! I’m grateful for both solutions.

In @emendelson’s case I know he’s code-signing, so he could look for the codesign folder instead. But he did say “while running it in Script Debuggger”, so I’m not sure I was making a great leap of faith.

I guess I’m just a bit wary of how long osadecompile can take when very large dictionaries are involved.

Actually, I’ve started notarizing using the amazingly valuable and beautifully-designed SD Notary instead of code-signing using Script Debugger. I hope that’s a sensible thing to do, but I should probably shut up to avoid thread drift (which I can’t stand to see when someone does it in a forum that I moderate).

It is. Traditional code-signing is next to useless these days.

An alternative, although I imagine it’s just osadecompile without the shell script:

use AppleScript version "2.5" -- El Capitan (10.11) or later
use framework "Foundation"
use framework "OSAKit"
use scripting additions

set run_only to ((current application's class "OSAScript"'s alloc()'s initWithContentsOfURL:(path to me) |error|:(missing value))'s source() is missing value)

Yes, it still has to load the dictionary to decompile.

But thinking about it, in the case of a run-only script, it’s just going to return straight away. Whereas if it’s open in an editor, the dictionary is loaded anyway. Classic premature optimization.