A User, can, of course, just click the check box a second time to clear it. How will a script know which value to "uncheck"?
I'll use a literal constant so that this script only removes "Orange" from the list and only adds "Black"....
#First check to see if "Orange" is selected in the current record's checkbox field
If [ Not IsEmpty ( FilterValues ( Table::checkboxfield ; "Orange" ) ) ]
Filter values compares two lists of values and returns a list of all values in both lists. In this case, it will return "orange" only if it is a value selected in the check box field. IsEmpty will return True only if no value is returned, which will be the case only if Orange is NOT selected.
Set Variable [$I ; Value: $I + 1 //$I will be our counting variable to keep track of which variable we are checking.]
Exit Loop If [$I > valueCount ( Table::Checkboxfield ) // exit loop after all values have been checked ]
If [GetValue ( Table::Checkboxfield ; $I ) ≠ "Orange" ]
#Since value at Position number $I is not "orange", add it to list of values stored in the variable named $List
Set Variable [$List ; Value: List ( $List ; GetValue ( Table::Checkboxfield ) ; $I ) ]
End If // if value is "Orange", skip it.
#Copy list of all values but "Orange" back into the check box field
Set Field [table::Checkboxfield ; $List ]
#Now add "Black" to list only if it is not already in the list...
If [ IsEmpty ( FilterValues ( Table::checkboxfield ; "Black" ) ) ]
Set Field [table::checkBoxfield ; List ( table::checkboxfield ; "Black" ]
If you replace "Orange" and "Black" with two variables or two fields, this script can be modified to remove any selected value and replace it with any selected value.
Thank you for your post Phil. I'm a major novice, so excuse me if this is stupid, but...when I try to try out your script, I am unable to input it in ScriptMaker. I can't find, for example, a command (or whatever the term may be) for "Not," or "IsEmpty," etc....
I'm using FileMaker Pro 8 if that makes a difference...
it won't just let me type in the script...
It's really, really important to let people know what version of FileMaker you are using. In this case, you are lucky, both Not and IsEmpty are available in your version. They are not, however, script steps so you won't see them listed in the script editor. You will be able to add them to calculations by typing them into the specify calculation dialog box.
I don't see where I used Not in any part of this script. (text after # or // are comments that can be left out.)
Here's an example:
In the script editor, double click the If script step listed in the right hand section of the editor to add it to your script. Then double click the newly added step or click the specify button at the bottom right to open up the specify calculaiton dialog. Then add the expression shown between the brackets in this dialog. You can add fields by double clicking them in the list of fields at top left. You can add functions by doubleclicking them in the list of functions at top right.
I finally figured this out, too.
I didn't know that you could Calculated Result and Substitute in the Replace function to replace just part of a field, but that alone can be a little problematic for big automation jobs: I realized I still needed a script to check to see if there was a return carriage or not. Pesky things!
Script to REMOVE a checkbox
(Replace the TABLE and FIELD in 6 places and VALUE in 4 places each time you need to use this)
If [ Pattern Count ( TABLE::FIELD ; "VALUE" & ¶ ) ]
Replace Field Contents [ TABLE::FIELD ; Replace with calculation: Substitute ( TABLE::FIELD ; "VALUE" & ¶ ; "" ) ]
Go to Record/Request/Page [ Next ; Exit after last ]
If [ Pattern Count ( TABLE::FIELD ; "VALUE" ) ]
Replace Field Contents [ TABLE::FIELD ; Replace with calculation: Substitute ( TABLE::FIELD ; "VALUE" ; "" ) ]
Go to Record/Request/Page [ Next ; Exit after last ]Else
Go to Record/Request/Page [ Next ; Exit after last ]End IfEnd IfEnd Loop