Skip to content

how do i...

mikey edited this page Sep 18, 2022 · 38 revisions

it's not LiveCode Script. the syntax is different. in some ways, those differences are subtle.

answer

Here is a handler that will give you similar functionality to LCS answer. NOTE: This code cannot be executed while the OnCreate(), OnLoad(), or OnPaint() events are active.

  • Don't forget to add a use com.livecode.engine at the top of your .lcb file
private handler Answer (in pPrompt as String, in pOKLabel as String, in pCancelLabel as String) returns String
	variable tCancelClause as String
	variable tOKClause as String

	-- <manage button labels>
		if ( pCancelLabel is empty and pOKLabel is empty ) then
			-- assume ok and cancel are both to be assigned
			put "\qOK\q" into tOKClause
			put "\qCancel\q" into tCancelClause
		else if pCancelLabel is empty then
			-- ok defined, but cancel is not, so assume only ok used
			put the empty string into tCancelClause
			put "\q" & pOKLabel & "\q" into tOKClause
		else if pOKLabel is empty then
			put "\qOK\q" into tOKClause
			put "\q" & pCancelLabel & "\q" into tCancelClause
		else 
			-- both defined
			put "\q" & pOKLabel & "\q" into tOKClause
			put "\q" & pCancelLabel & "\q" into tCancelClause
		end if --pCancelLabel is empty and pOKLabel is empty
	-- </manage button labels>

   execute script "answer \q" & pPrompt & "\q with " & tCancelClause && "and" && tOKClause &"; return it"
   return the result
end handler

Examples

Answer ("Test" , "" , "" ) -- answer dialog with "OK" and "Cancel" buttons
Answer ("Test" , "notOK" , "notCancel" ) -- answer dialog with "notOK" on the OK button and "notCancel" on the Cancel button
Answer ("Test" , "OK!" , "" ) -- answer dialog with just an OK button labeled "OK!"
Answer ("Test" , "" , "No" ) -- answer dialog with "OK" on the OK button and "No" on the Cancel button

exit a handler

  • You don't. You branch around to the bottom.
  • You can use next to skip to the end of a loop, e.g. next repeat

is not a number

is not is not a thing. not() is a thing

not ( myVar is a number )

item n of someVar

you have to split the text into an array, first.

split someVar by "," into someVarAsAnArray
put element n of someVarAsAnArray into whatever

library: respond to the object calling the library

post <message> to the caller

If you are calling a native library, you should first grab the caller, because there may be a delay or other events that interrupt the library handler.

variable tCaller as String
put the caller into tCaller

and then you can post the message to tCaller, if it exists.

message box

here is a handler that will give you similar functionality. NOTE: This code cannot be executed while the OnCreate(), OnLoad(), or OnPaint() events are active.

  • Don't forget to add a use com.livecode.engine at the top of your .lcb file
  • You cannot embed a "\r" when throwing your message over the wall, for some reason, so you have to use beforeLine or afterLine if you want your message to be on a separate line before or after the contents of the message box.
private handler Message ( in pPrompt as String, in pBeforeAfterInto as String ) returns nothing
	-- can't embed a "\r" when sending over the wall into LCS, so have to have extra options like these
	if pBeforeAfterInto is "beforeLine" then
		execute script "put cr before msg"
		put "before" into pBeforeAfterInto
	else if pBeforeAfterInto is "afterLine" then
		execute script "put cr after msg"
		put "after" into pBeforeAfterInto
	end if

	execute script "put \q" & pPrompt & "\q" && pBeforeAfterInto && "msg"
end handler

Examples

Message ( "test" , "into" )
Message ( "test" , "beforeLine" )
Message ( "test" , "afterLine" )

post

Per Bug 23945 , the docs don't specify that Argument should be a List. I think I would add emphasis to the datatype for Arguments, because that's going to bite someone for an hour if we don't call it out for them.

Parameters:

Name Type Description
Message String The message to dispatch.
[ to Object ] String The script object to dispatch the message to.
[ with Arguments ] List A list of arguments for the message.

Examples

post "mouseUp"

post "menuPick" with [ pItemName ] --coercing a string into a list, the fancy way...

put empty into myVar

Depending on what you intend to use myVar for, you can

variable myVar as array
put the empty array into myVar
variable myVar as data
put the empty data into myVar
variable myVar as list
put the empty list into myVar
variable myVar as string
put the empty string into myVar

repeat with i = 1 to n

you have to specify up or down and declare the loop counter variable as an integer

  1. declare the loop counter variable i as integer
  2. tell LCB which direction to count
  • going up
repeat with i from 1 up to n
  • going down
repeat with i from 1 down to n