Navigator Convert to Behaviors Details

The “Convert to Behaviors…” command on the Scripts menu can be somewhat involved, depending on the existing structure of behaviors you have. This is a description of using the command on a stack with multiple scripted objects with behaviors, and chained behaviors.

Overview

convert

Select the controls in Navigator and select “Convert to Behaviors” on the Scripts menu.

To convert to script-only stack behaviors, select the controls you want to convert and select “Convert to Behaviors…” on the Script menu. Select all the controls that will be affected, not just the behavior controls. Note that in this example I selected No Script 1, 2, and 3 for example. They aren’t used as behaviors, but they have behaviors that will be converted.

For each control you select:

  1. The control’s script will be moved to a script-only stack. If the control has no script, this is optional (see below).
  2. If the control already has a behavior, the script-only stack’s behavior will be set to the control’s behavior.
  3. The control’s behavior will be set to the script-only stack.
  4. Any other controls being converted that reference the control as their behavior will be updated to reference the script-only stack.

Only controls selected for conversion will be converted as described above. See below for how to add enclosed controls and behavior chains.

If you want to convert controls on multiple cards or in multiple stacks, bookmark them in Navigator and select the bookmarks. Once you have opened the Behavior Conversion dialog you can easily add enclosed controls and behavior chains.

The Conversion Dialog

When you select “Convert to Behaviors…”, Navigator will display the Behavior Conversion dialog:

Screen Shot 2018-02-24 at 2.43.08 PM

The Filename Template

The first section of the dialog allows you to set the file name template to use. The template uses the Value() function, so “me” refers to the control being converted to use the script-only stack. You can use almost any legitimate LiveCode script reference, so for example, “the short name of stack (word -2 to -1 of the long name of me)” will give the name of the stack that contains the behavior control. The Add: dropdown includes several simple recipes you can add to your template. Whenever you update the template, Navigator tests the template on the entry field and displays the results in the Example field. If the evaluation fails, the example will turn red.

The “Replace space with:” character will be used to replace any spaces in the file name. It can be any string of characters you like, including nothing, in which case spaces will simply be removed; and space, in which case spaces will be unchanged.

Conversion Options

The next section includes two checkboxes for options for the conversion. The first is Convert Unscripted objects. This is not checked by default. If you check it, objects with no script will still be converted to (empty) script-only stacks. This can be useful if you have set up a user interface but haven’t coded anything. Otherwise it’s likely to create a bunch of script-only stacks you don’t need.

The second checkbox is Create Script-only Stack Behaviors for existing Script-only Stacks. It is not checked by default, and you probably don’t want to check it.

Behavior Stack Folder

The next section allows you to set the folder to create the behavior script files in. You can type in a path, or click the button to select a folder in a dialog. Navigator will create the appropriate relative path to use when setting the stackfiles property for however many stacks are included in the conversion.

The ID List

The ID List field contains the list of controls to convert. It starts out with whatever controls you selected before you used “Convert to Behaviors…”, but it can be modified to include whatever long IDs you like. The two buttons above the list allow you to expand the list, by adding all controls contained in any cards, groups, or stacks in the list, or by adding all behavior controls in any behavior chains starting with any controls in the list.

Set as Template

Finally, the “Set as template for this/these stack(s)” button sets properties on the stacks involved so that you can convert behaviors without using the dialog. Once those properties are set, right-clicking on a control in those stacks will include “Export to Behaviors” on the popup menu, and selecting it will perform the conversion to script-only stack behaviors immediately.

The Conversion Process

When you click “Convert” Navigator will offer to perform a test run, which won’t change anything, but will take you to the Log display and show the log output with the current settings. You can see what script-only stacks will be created, and how the behaviors for various controls will be changed. If everything looks right, you can switch back to the Convert display to perform the actual conversion, which also populates the Log display.

In performing the conversion, Navigator converts scripted controls to behaviors, creates the necessary stack files, and adds entries to the stackfiles property of the stacks containing the converted controls so the new stacks will be available.

If the conversion process resulted in any script-only stacks that themselves have behaviors, you should refer to the log file. At present, script-only stacks don’t support storing a behavior, so if they need one, it has to be set in your startup code. The log file includes the lines of code you need to execute during initialization to get the behavior stacks to work properly.

Example

Suppose you have the following setup:

Screenshot 2018-01-30 22.37.18

The arrows represent behavior links:

  • Button “No Script 1” has no script, but it has button “Behavior 1” set as its behavior. The same is true of buttons “No Script 2” and “No Script 3”.
  • Button “Behavior 1” has a script, and button “Behavior 2” set as its behavior.
  • Button “Behavior 2” has a script and button “Behavior 3” set as its behavior.
  • Button “Script” has a script, and no behavior.
  • Button “Has Script 1” has a script, and also button “Behavior 1” set as its behavior.
  • Button “No Script A” has no script and button “Behavior 2” set as its behavior.
  • Button “Has Script A” has a script, and button “Behavior 2” set as its behavior.

The Log File

For this conversion, I’m going to use the very simple template “B”, and rely on Navigator to serialize the stack names. Here is part of the log file from the above conversion:

6 script-only stacks were created.
Set 10 behaviors for controls and script-only stacks.
12 controls skipped because of no script.
No errors were detected.

Creating script-only behavior stacks for objects in the following stacks:
SoS Test Thing 1

Wrote the script of:
button id 1026 of card id 1002 of stack “/Users/gcanyon/Desktop/Desktop Archive/SoS Test Thing 1.livecode”
to script-only stack:
/Users/gcanyon/Desktop/SoS Scripts test repository/script-only demo/B4.livecodescript
Deleted the script of:
button id 1026 of card id 1002 of stack “/Users/gcanyon/Desktop/Desktop Archive/SoS Test Thing 1.livecode”
Added to the stackFiles of stack “SoS Test Thing 1”:
B4,../SoS Scripts test repository/script-only demo/B4.livecodescript

Wrote the script of:
button id 1006 of card id 1002 of stack “/Users/gcanyon/Desktop/Desktop Archive/SoS Test Thing 1.livecode”
to script-only stack:
/Users/gcanyon/Desktop/SoS Scripts test repository/script-only demo/B2.livecodescript
Deleted the script of:
button id 1006 of card id 1002 of stack “/Users/gcanyon/Desktop/Desktop Archive/SoS Test Thing 1.livecode”
Added to the stackFiles of stack “SoS Test Thing 1”:
B2,../SoS Scripts test repository/script-only demo/B2.livecodescript

Use this script to set behaviors for script-only stacks:

set the behavior of stack “B4” to the long id of stack “B5”
set the behavior of stack “B3” to the long id of stack “B5”
set the behavior of stack “B5” to the long id of stack “B6”
set the behavior of stack “B2” to the long id of stack “B3”

 

That last section is the part needed for setting the behaviors of script-only stacks. Navigator sets any behaviors necessary on the script-only stacks it creates, but script-only stacks don’t retain behaviors, so copy that code to your startup routine to make the behaviors work properly.

Screenshot 2018-01-30 22.43.27

Script-only Stackfiles

Here is a screen shot of the window where the script-only stacks were created.

 

 

What Navigator did

So here is a revised diagram showing what Navigator has done:

Screenshot 2018-01-31 08.21.59

The LC logos are the new script-only stacks Navigator created to contain the objects’ scripts:

  • B1 contains the script of button “Script”
  • B2 contains the script of button “Has Script 1”
  • B3 contains the script of button “Behavior 1”
  • B4 contains the script of button “Has Script A”
  • B5 contains the script of button “Behavior 2”
  • B6 contains the script of button “Behavior 3”

The scripts of all the objects have been set to empty.

The arrows represent the new behavior settings Navigator created. Everything works as it did before.

For example, button “No Script A” originally had button “Behavior 2” as its behavior, and button “Behavior 2” had button “Behavior 3” as its behavior. Now button “No Script A”‘s behavior is set to stack “B5”, which contains the stack of button “Behavior 2”; and stack “B5″‘s behavior is set to stack “B6”, which contains the script of button “Behavior 3”.

As another example, button “Has Script A” had a script of its own. It also had button “Behavior 2” as its behavior, which had button “Behavior 3” as its behavior. Button “Has Script A” now has no script. Its script is in stack “B4”, which is set as its behavior. Stack “B4” then has the same behavior setup that button “Has Script A” had, maintaining the original setup.

Note button “Orphan”, which was missed in the conversion. If its behavior was set to button “Behavior 1”, that will still be the case. button “Behavior 1” now has no script, but it has its behavior set to stack “B3”, which contains button “Behavior 1″‘s script, and has its behavior set to stack “B5”, maintaining the functionality of the original chain of behaviors. Of course it’s better to convert everything, but as long as button “Behavior 1” isn’t deleted, button “Orphan” will continue to work exactly as it did before the conversion, even though it was missed. TBD is a routine to optimize behavior chains, which would see that button “Behavior 1” has no script and is unnecessary, and would optimize it out of the chain.

Finally, note the behavior arrows drawn in red. Those are behaviors set on script-only stacks, which do not maintain behaviors themselves. Navigator has set the behaviors so that everything will work, but restart LiveCode and those behaviors will disappear. That’s where the last section of the log file comes into play: it provides the code necessary to create those behaviors so everything works properly. Put this somewhere in your startup code and you’re all set:

set the behavior of stack “B4” to the long id of stack “B5”
set the behavior of stack “B3” to the long id of stack “B5”
set the behavior of stack “B5” to the long id of stack “B6”
set the behavior of stack “B2” to the long id of stack “B3”

And that’s it! ALWAYS WORK ON A COPY.

 

 

 

 

Advertisements