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: