Executing a function - How to get a result

LiveCode Builder is a language for extending LiveCode's capabilities, creating new object types as Widgets, and libraries that access lower-level APIs in OSes, applications, and DLLs.

Moderators: LCMark, LCfraser

Post Reply
Zryip TheSlug
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 163
Joined: Tue Jan 26, 2010 10:15 pm
Contact:

Executing a function - How to get a result

Post by Zryip TheSlug » Sun Apr 05, 2015 10:32 pm

The com.livecode.date library seems actually very limited.

For getting the list of the weekdayNames, I have tried:

Code: Select all

private handler getWeekDaysName() as List
   variable tStringList as String
   variable tDayNamesList as List

   execute script "get the system weekdayNames"
   put the result into tStringList
   split tStringList by return into tDayNamesList
  
  return tDayNamesList
end handler
But the engine is returning an error at the line:
put the result into tStringList

How can I get the result of a function when using "execute script"?
Last edited by Zryip TheSlug on Wed Apr 08, 2015 12:01 am, edited 1 time in total.
TheSlug
http://www.aslugontheroad.com - Tutorials, demo stacks and plugins for LiveCode
Data Grid Helper - An intuitive interface for building LiveCode's Data Grids
Excel Library- Extends the LiveCode language for controlling MS Excel

trevordevore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1005
Joined: Sat Apr 08, 2006 3:06 pm
Location: Overland Park, Kansas
Contact:

Re: Executing a function - How to get a result

Post by trevordevore » Tue Apr 07, 2015 2:01 pm

You need to return the value, not get it.

Code: Select all

execute script "return the system weekdayNames"
Trevor DeVore
ScreenSteps - https://www.screensteps.com

LiveCode Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode
LiveCode Builder Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode-builder

Zryip TheSlug
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 163
Joined: Tue Jan 26, 2010 10:15 pm
Contact:

Re: Executing a function - How to get a result

Post by Zryip TheSlug » Tue Apr 07, 2015 7:40 pm

Trevor,

I should have thought to a simple "return". Thanks!

That's said I'm not sure the syntax for defining property options:

Code: Select all

metadata fontName.options is "execute:get the fontnames; sort it" 
is not confusing. It confused me. 8)
TheSlug
http://www.aslugontheroad.com - Tutorials, demo stacks and plugins for LiveCode
Data Grid Helper - An intuitive interface for building LiveCode's Data Grids
Excel Library- Extends the LiveCode language for controlling MS Excel

Zryip TheSlug
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 163
Joined: Tue Jan 26, 2010 10:15 pm
Contact:

Re: Executing a function - How to get a result

Post by Zryip TheSlug » Wed Apr 08, 2015 12:24 am

trevordevore wrote:You need to return the value, not get it.

Code: Select all

execute script "return the system weekdayNames"
I gave this syntax a try:

Code: Select all

variable tStringList as String
execute script "return the system weekdayNames"
put the result into tStringList
and I got this error from the engine:

"Value is not of correct type for assignment to variable - expected type livecode.lang.string for assigning to variable tStringList".

We can assume, "the result" contains nothing or not the value we are expecting or
the engine is failing to type the result "object" in this case.
Or, on the other hand we can assume I do not know how to get a result.

Anyway, I can't figure out how the engine could give a type to "the result" as the type is totally hidden until the execute command has been interpreted by the engine.

For example, the result could contain:

Code: Select all

execute script "return true" -- a boolean
execute script "return 1" -- an integer
execute script "return test" -- a string
And the developer would expected to get a string in all the cases.

As we can't give a type to "the result" ourself, the syntax for the execute script command would be:

execute script "myScript" as [Type]
TheSlug
http://www.aslugontheroad.com - Tutorials, demo stacks and plugins for LiveCode
Data Grid Helper - An intuitive interface for building LiveCode's Data Grids
Excel Library- Extends the LiveCode language for controlling MS Excel

trevordevore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1005
Joined: Sat Apr 08, 2006 3:06 pm
Location: Overland Park, Kansas
Contact:

Re: Executing a function - How to get a result

Post by trevordevore » Wed Apr 08, 2015 5:16 am

Hmm, this is the code I tested in do-1 and it works for me.

Code: Select all

public handler OnOpen()
		variable tStringList as String
		variable tDayNamesList as List
		
		execute script "return the system weekdayNames"
		put the result into tStringList
		split tStringList by "," into tDayNamesList
		 
		log "%@, %@" with [tStringList,tDayNamesList] 
end handler
Are you sure your updated code is compiling? If you have installed the extension and you try to test new code from the extensionManager then the installed extension is run, not the updated code. You would need to uninstall it, restart, and then try testing again.
Trevor DeVore
ScreenSteps - https://www.screensteps.com

LiveCode Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode
LiveCode Builder Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode-builder

Zryip TheSlug
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 163
Joined: Tue Jan 26, 2010 10:15 pm
Contact:

Re: Executing a function - How to get a result

Post by Zryip TheSlug » Wed Apr 08, 2015 7:06 pm

I found the problem.

You are in the OnOpen message.

The same code executed in the OnPaint or in the OnCreate message is returning the error.

The OnPaint message is probably not the best place for executing this kind of command, but I used it for testing. The OnCreate is more appropriated, though.

I will report this in the RQCC.
TheSlug
http://www.aslugontheroad.com - Tutorials, demo stacks and plugins for LiveCode
Data Grid Helper - An intuitive interface for building LiveCode's Data Grids
Excel Library- Extends the LiveCode language for controlling MS Excel

trevordevore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1005
Joined: Sat Apr 08, 2006 3:06 pm
Location: Overland Park, Kansas
Contact:

Re: Executing a function - How to get a result

Post by trevordevore » Wed Apr 08, 2015 7:14 pm

Actually, that probably isn't a bug. I remember reading that you can't execute code within certain handlers as it has the potential to create problems.
Trevor DeVore
ScreenSteps - https://www.screensteps.com

LiveCode Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode
LiveCode Builder Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode-builder

Zryip TheSlug
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 163
Joined: Tue Jan 26, 2010 10:15 pm
Contact:

Re: Executing a function - How to get a result

Post by Zryip TheSlug » Wed Apr 08, 2015 7:31 pm

You're right. In the documentation itself. For the OnPaint and OnCreate messages we have a big note:
Note: Access to most script object operations is not allowed whilst an OnPaint handler is running.
I will just report a typo in the documentation about the note concerning the OnCreate message, which seen to be a copy paste of the OnPaint note.
TheSlug
http://www.aslugontheroad.com - Tutorials, demo stacks and plugins for LiveCode
Data Grid Helper - An intuitive interface for building LiveCode's Data Grids
Excel Library- Extends the LiveCode language for controlling MS Excel

Post Reply

Return to “LiveCode Builder”