You use Script Parameter. When you attach a script to a button, you can insert a different value (for each button) into the Optional Script Parameter and use it in the same script. Example ... Three buttons attached to same script named Update. Each button holds a different script parameter of 1, 2 or 3.
Set Field [ anyField ; Get ( ScriptParameter ) ]
However, you haven't provided enough information to know what you are really looking for, Ted, so if this isn't what you want, please explain more. :^)
Thanks for your reply LaRetta.
So I looked up ScriptParameters and it seems to be the way to go. I can't quite work it out though. The following is a simplified explanation of what I want to do.
Let's say I have two buttons, ButtonA and ButtonB. I want each of the buttons to call on the same script. The script modifies only the clicked button so I need to tell the script what button has been clicked.
That's the nub of the problem. How do I do that.
I've tried putting the button name in the scriptparameter but I couldn't get it to work.
Thanks for your help.
How are you trying to "modify the button"? You can't directly modify any button with a script, but there may be ways to produce the same result if you let us know what kind of change the user should see when they click the button.
Wont setting a Global Field with an input value and then use the OnObjectModify script trigger work as well?
Yes, if it's what the user wants to see on their layout. The "look and feel" can be slightly different.
Well, you can name the text on a button to <<$$button>> and then script:
Set Variable [ $$button ; Get ( ScriptParameter ) ]
... but again, we are missing what you want to do, Ted. Can you please be specific? If you want to turn a field into a button, and tell FM to grab that field name so you can perform a Set Field By Name then we can do that as well. If you want to know which button was clicked, you can put that value within it's script parameter as I explained originally and then the script refers to THAT parameter.
So we clearly need more specific description of what you are doing ... step by step please ... describe the User experience and what should happen. :^)
I want to change the image on the button that has been clicked. The following script changes the image on TickButton depending on the existing image. But I need to put a "variable" in place of TickButton so the script can apply to any clicked button.
If [ButtonImage::TickButton = ButtonImage::BlankContainer]
I know there's neater ways of doing this (Phil showed me one) but at my level of understanding of databases the above script is more in my comfort level.
Here's a trick that will serve the immediate need:
Your "buttons" are of course, container fields.
In layout mode, click your first container field, select Button setup...., select Perform Script and enter this expression as the script parameter:
GetFieldName ( YourTable::ContainerFieldButton1 )
Do the same for the second container field, but use: GetFieldName ( YourTable::ContainerFieldButton2 )
Now your script can be written as:
Set Variable [$FieldName ; value: Get ( ScriptParameter ) ]
If [ getField ( $FieldName ) = ButtonImage::BlankContainer ]
Set Field By Name [ $FieldName ; ButtonImage::TickContainer ]
Set Field By Name [ $FieldName ; ButtonImage::BlankContainer ]
Using GetFieldName to pass the table and field name as a parameter is a trick that keeps your script working correctly even if you later use Manage | database to rename the field or the table occurrence to be something different.
That worked perfectly. Thanks all.
One small problem. I've created a new table (ImageTable) and placed the Tick and Blank fields there. I've changed the script to reflect this but now the images don't alternate on the buttons, i.e. nothing happens.
The revised script is:
Set Variable [$varTemp ; value: Get ( ScriptParameter ) ]
If [ getField ( $varTemp) = ImageTable::Blank ]
Set Field By Name [ $varTemp; ImageTable::Tick ]
Set Field By Name [ $varTemp; ImageTable::Blank ]
The script that works, when I refer to the images in the same table is:
Set Variable [$varTemp ; value: Get ( ScriptParameter ) ]
If [ getField ( $varTemp) = DataInput::Blank ]
Set Field By Name [ $varTemp; DataInput::Tick ]
Set Field By Name [ $varTemp; DataInput::Blank ]
What am I doing wrong?
Since you've added a new table, did you also define a relationship linking it to the table occurrence of the layout where you have these buttons?
Yes, that was the problem. It works OK now.
However, the only reason I created the second table was because I didn't want the image containers to appear in my main table layout. I now realize that you can remove a field from the layout but still refer to it as it is still in the table. Voila!
So, I've removed the image table completely and removed the image fields from the layout, revised my script and everything's cool.
I think I've progressed from total confusion to a foggy understanding.
One small thing. How do you copy script so it can be pasted into, say the forum. I can select all the script and copy it, but it doesn't get copied to my clipboard to allow me to paste it elsewhere.
It would be nice if the script editor were text based. Many have requested that feature: http://www.filemaker.com/company/feature_request.html
In the meantime:
- You can upload a screen shot by clicking the edit link to your original message and uploading it there
- You can upload a screen shot to a file share site and post the download link to a new response you post here.
- You can print a script to a PDF, open the PDF and then select and copy the script as text from the opened PDF.
- If You have FileMaker advanced, you can generate a database design report and copy the script as text from there.