NSAppleScript, executeAndReturnError...

With little help from this site I was able to make this working. So thanks I guess its was Shane again.

use AppleScript version "2.5"
use framework "Foundation"
use scripting additions

set theScript to "display dialog \"Hello World\""

set aString to current application's NSString's stringWithFormat:theScript
set initWSource to ((current application's NSAppleScript's alloc()'s init())'s initWithSource:aString)
set {returnValue, errorDict} to (initWSource's executeAndReturnError:(reference)) as list

-- reference https://www.macscripter.net/viewtopic.php?pid=198631#p198631
if (errorDict = missing value) then
	return item 1 of ((current application's NSArray's arrayWithObject:returnValue) as list)
else
	return item 1 of ((current application's NSArray's arrayWithObject:errorDict) as list)
end if

So I made a handler…

use AppleScript version "2.5"
use framework "Foundation"
use scripting additions

set theScript to "display dialog \"Hello World\""

my stringWithScript:theScript withReturnMessage:"its working"

on stringWithScript:aString withReturnMessage:isMessage
	set aString to current application's NSString's stringWithFormat:aString
	set initWSource to ((current application's NSAppleScript's alloc()'s init())'s initWithSource:aString)
	set {returnValue, errorDict} to (initWSource's executeAndReturnError:(reference)) as list
	
	-- reference https://www.macscripter.net/viewtopic.php?pid=198631#p198631
	if (errorDict = missing value) then
		if (isMessage = "") then
			return item 1 of ((current application's NSArray's arrayWithObject:returnValue) as list)
		else
			return isMessage
		end if
	else
		return item 1 of ((current application's NSArray's arrayWithObject:errorDict) as list)
	end if
end stringWithScript:withReturnMessage:

You don’t need the extra step to create a Cocoa string and as you coerce the result to an AppleScript list the conversion to NSArray and back to list is pointless.

use AppleScript version "2.5"
use framework "Foundation"
use scripting additions

set theScript to "display dialog \"Hello World\""

my stringWithScript:theScript withReturnMessage:"it's working"

on stringWithScript:aString withReturnMessage:isMessage
	set initWSource to current application's NSAppleScript's alloc()'s init()'s initWithSource:aString
	set {returnValue, errorDict} to (initWSource's executeAndReturnError:(reference)) as list
	
	-- reference https://www.macscripter.net/viewtopic.php?pid=198631#p198631
	if (errorDict = missing value) then
		if (isMessage = "") then
			return returnValue
		else
			return isMessage
		end if
	else
		return errorDict
	end if
end stringWithScript:withReturnMessage:

Hi StefanK.

I get your point if I have return of isMessage but if I have not… then your code will return ASObjC
value and not AS…

So I used Shane version with NSArray to return its value or error message.

ex.

set theScript to "display dialog \"Hello World\""

my stringWithScript:theScript withReturnMessage:""

This will return ASOC value… with your code.