LiveCode for FM Manual FunctionsHow do I modify a built in function?

How do I modify a built in function?

Adding a new function

In this lesson we will create a new function that will check the file type of a file, by checking the file extension.

We will use the 'lcfileexists' function as the basis for our new function.

To get started open the LiveCode for FM Solution and go to the Functions pane of the Workspace.

Clone a built in function

The built in functions are read only so we need to make a copy first and edit the copy.

Select the function you want to clone(1) and click the 'Clone' button(2).

You will be asked you give your new function a name, call it "fileIsText" and click "OK".

The new function is created and highlighted in the list, and you can now edit it.

LiveCode for FM comes with some built in functions that you can use out of the box, but you can also add your own functions to provide tailored features in your solutions.

LiveCode is a fully featured app development tool and now anything you can write in LiveCode can be used in your FileMaker solution.

The easiest way to get started writing your own functions is to modify one of the built in functions to add some additional functionality.

Update the function description.

Update the function description to explain what the new fucntion will do.

Update the function script

Finally we need to add more code to the function to check the file exists and that the file extension is ".txt". The file to be checked is passed in as a parameter, you get the value of the parameter using the fmParam() function.

Firstly we check if the file exists, if not return an error message.

Set the function script to

if there is not a file fmParam(1) then
  return "Error:file does not exist."
else

end if

Then add the check for the extension. We want to check that the last 4 characters of the filename are ".txt". LiveCode allows us to do this using a chunk expression where the last character is -1, the second last character is -2 etc.

Set the function script to

if there is not a file fmParam(1) then
	return "Error: file does not exist."
else
	if char -4 to -1 of fmParam(1) is ".txt" then
		return true
	else
		return false
	end if
end if

When you are done Save the function and click the "Close" button to close the Workspace and retrun to the solution.

Testing the function

To test the function go the the functions layout of the solution.

  1. Enter the function name
  2. Set the first parameter to the path to a text file.
  3. The returned value is "true" if the file has a ".txt" extension.

If you pass a non text file the returned value is "false", if you pass a file that does not exist the returned value is the message "Error: file does not exist."

Other possible extensions

There are many ways you could further customise this function. For example you could generalise the function so that the extension you want to check for is passed in as the second parameter.

Further resources

0 Comments

Add your comment

E-Mail me when someone replies to this comment