Tap Forms app icon half
Tap Forms Forum text image
Blue gradient background

Exchange tips and ideas with the Tap Forms community

Search Results for 'script'

Viewing 15 results - 781 through 795 (of 2,951 total)
  • Author
    Search Results
  • #47778

    In reply to: Speed Tips?

    Daniel Leu
    Participant

    5s seems indeed a bit long. I added some code to measure the execution time:

    function speedtest() {
       var l1_id = 'fld-xxx';
       var l1b_id = 'fld-xxx';
       var l2_id = 'fld-xxx';
       var l2b_id = 'fld-xxx';
    
       record.setFieldValue(l1b_id, record.getFieldValue(l1_id));
       record.setFieldValue(l2b_id, record.getFieldValue(l2_id));
    
       document.saveAllChanges();
    }
    
    var start = Date.now();
    speedtest();
    var end = Date.now();
    console.log(<code>Execution time: ${end - start} ms</code>);

    The first time running the script, I get an execution time of 5ms, successive runs are all between 1ms and 2ms.

    Cheers, Daniel

    ---
    See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricks

    #47777

    Topic: Speed Tips?

    in forum Script Talk
    Stephen Meehan
    Participant

    Hi, the script below takes about 5 seconds to execute, which seems inordinately slow for something so simple.

    Anyone notice any obvious bottlenecks?

    Thanks!

    function New_Script() {

    var l1_id = ‘fld-1150255ff604464392abee526dd2592b’;
    var l1b_id = ‘fld-5afc66a3718e4ac69e5121ae0182b483’;
    var l2_id = ‘fld-b9080a9632014c118b8a4f1790d7363e’;
    var l2b_id = ‘fld-144e23e549f4475b8d384de1b6b367e3’;

    record.setFieldValue(l1b_id, record.getFieldValue(l1_id));
    record.setFieldValue(l2b_id, record.getFieldValue(l2_id));

    document.saveAllChanges();

    }

    New_Script();

    #47754

    In reply to: Table quirkiness

    Daniel Leu
    Participant

    I do something very similar. In my custom layout, I have two buttons that execute each a separate script, one to update the summary field and one to clear it. This way, the content of the table is only evaluated for the current record when I ask TF to do it.

    Cheers, Daniel

    ---
    See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricks

    #47753

    In reply to: Table quirkiness

    Bob Williams
    Participant

    Any suggestions for how I might set up my forms and/or scripts to ameliorate the saveAllChanges() selection issue caused by the refresh? When I’m trying to edit data in a table, it makes for a lot of extra moving around, and worse, I keep misstepping and accidentally entering data into the wrong cell.

    For context, I have a table with rows for notes, the fields of which include a short-notes field and a long-notes field. I have a separate script field (not in the table) that builds a summary of all the notes in the table by doing some fancy string concatenation. It’s the summary script that’s calling saveAllChanges(). Maybe there’s some way to delay the script run until focus leaves the table? Or some way I’m missing from within the script itself to grab the selection right before the save call and then restore it right after?

    #47746
    Bernie McGuire
    Participant

    Brenday
    Yes its a classic delemia :) One possibility, which I think is similar to you first approach, would be to allow one to disble the ADD function on the Employee Form in this case, and force added to be don from the Department Form.

    What I have implemented as a tempoary solution, which doesnt solve the problem , but alerts to the issue, is scripting as follows.

    On the Employee Form:
    1. a hidden auto increment
    2. the link from form for Departments field
    3. a hidden script field that does the following:
    It is triggered by a chane in the auto increment,
    it checkes if the link from field for Department is = undefined, and if it is,
    executes a Utils.alertWithMessage(“Warning”,”A an employee must have a departemnt so totals will be correct. Use the employee add from the Department Form”.

    This doesnt prevent the error but alerts the operator that additional stepes are necessary to fix the situation. It would be ideal if I could close the open Employee FOrm, deleteing the record to force the correct operation.

    If there were some way to say, pop up a Department List and let the operator assign a Department. This might be the most user friendly solution. If possible of course.

    Any time you want to knock around ideas Brendan, let me know, Im happy to engage.
    Thanks for all you hard work.
    Bernie

    #47737
    Gregory
    Participant

    Also, a possible related bug: when I first wrote the script and ran it, I was getting syntax errors about one of my opening vars being defined more than once (“Can’t create duplicate variable”).

    the reason you’re getting this error is because your variables are being declared in the global space. if you’ve declared one or more of these variables in other scripts that have already run, you’ll get this error.

    any variable declared outside of a function block will be global. to minimise these situations, I would reformat the script so that none of the variables are global:

    function areThereOpenTodos() {
    
       var todosTableId = 'fld-cacbf';
       var todosTable_doneCbFieldId = 'fld-b94f8';
       var todosTable_ofFieldId     = 'fld-4f82e';
    
        var countDoneChecked = record.getTotalOfLinkedFieldForField(todosTableId, todosTable_doneCbFieldId);
       var countOfChecked = record.getTotalOfLinkedFieldForField(todosTableId, todosTable_ofFieldId);
    
       return (countDoneChecked + countOfChecked) > 0;
    }
    
    function run() {
    
       var openTodosFieldId = 'fld-f1892';
    
       if (areThereOpenTodos()) {
          record.setFieldValue(openTodosFieldId, 1);
          console.log('set to 1');
       } else {
          record.setFieldValue(openTodosFieldId, 0);
          console.log('set to 0');
       }
    }
    
    run();
    #47726

    In reply to: Table quirkiness

    Bob Williams
    Participant

    If I haven’t right-clicked on a table column and selected either of the sort options, the table visually shows that no sort order is being applied: the column header is gray, and the sort direction triangle is hollow. As soon as I choose a sort option from the context menu, both of those things change, showing that a sort order has been applied and what that order is.

    From a user POV, this suggests that there simply is no sort being applied before selecting one. In such case, I’d expect the rows to remain in the order I entered them, and it’s this order that I’m referring to as the “natural order”.

    I get that deep under the hood, the database itself my treat sort order in the absence of an explicit sort as undefined, but that feels like an implementation detail that shouldn’t leak all the way up to the user level of an app like this. As a general rule, things should stay where the user put them and not move around on their own. In this case, it’s akin to unsorted rows in a spreadsheet arbitrarily reordering themselves — that would be a maddening experience, and no spreadsheet has ever worked that way.

    A date field (or an auto-increment field, which I also played with as a solution) is a decent workaround, but it’s more noise in the user’s system that they don’t directly need, and it’s more hassle to set up and maintain. As a suggestion, I think TF should track natural order internally and maintain that order unless the user chooses otherwise. This will also enable an “insert row here” feature, which doesn’t logically make sense in the current model.

    As to the changing selection issue, some more testing suggests that it’s related to a script field on the form, specifically, the document.saveAllChanges(); line. Note that the script doesn’t change the table in any way; it simply iterates over the table and outputs a summary. If you need more, let me know and I’ll send you my file.

    #47725
    Bernie McGuire
    Participant

    Is there a way to prevent orphans from being created in a 1-M or M-M relationship in Tap Forms.
    Here is the situation A company has Departments, Departments have employees, every employee must be in a department.
    The Design !. Department Form, 2. Employee Form. The Department form has a ‘Line to Form’ pointing to Employee as 1 to Many, with show inverse relationship checked.
    Work Flow that works: Open Department Form, Click + on Employee (link to form) and add an employee. This works fine.
    Work flow as a problem: Operatore opens the Employee form list, Clicks + to add and a Employee form opens but NO department is assigned (obviously) and if the operator does not assign a department this will be an orphan employee (until some intervention is made to assign the Department)

    Is there a way to keep work flow 2 from happening. A way to force a department always be assigned to a new employee? I rather not do this after the fact, ie , with a script that checks for orphans etc.
    Rather either prevent it or enforce it.
    I rather not hide the employee form because it is useful to pull up the list of employees and go directly to that individual form.
    Is it Possible?
    Thanks

    #47673
    Daniel Leu
    Participant

    Is it possible? Yes! Is it easy? Not exactly!

    This is something I have been looking at in the past, but never spent time on implementing it.

    You need to create a custom URL scheme that runs your AppleScript. Here is one simple example on how to do this: https://github.com/chrisliuqq/mac-custom-url-scheme-to-script.

    Then in TapForms, you can create a form script that calls your script using the registered custom URL with something like Utils.openUrl("custom://something-I-dont-need?param=Hello");. My example uses the custom URL shown in the example above. As you see, you can use parameters to provide an argument to your AppleScript. Another option is to use the clipboard (eg, Utils.copyTextToClipboard("Some text").

    All that is left is assigning the form script to a script button in your custom layout. That’s it. Good luck!

    Cheers, Daniel

    ---
    See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricks

    #47672
    derek
    Participant

    Hi Brendan,

    Have been looking for a solution but don’t even know if its possible to add a script button to trigger an “Automator” workflow.

    What I would like in my “Job Sheet” Form is to have a button to trigger an auto folder creation for the job number that was generated with a new Job Sheet.

    Thanks!

    derek

    #47662
    Brendan
    Keymaster

    That option just executes the script for every record in the form. That’s why you see it running multiple times.

    And I responded to your email separately.

    #47661
    Bob Williams
    Participant

    Ah, I hadn’t seen the form.saveAllChanges() method. With that added, however, the script still doesn’t seem to be running. I’ll send it to you.

    On the multiple runs front, I noticed that if I turn off the “Update records when saving” option in the editor and hit run, the output is exactly what I’d expect.

    With the option turned on:

    set to 0
    set to 0
    set to 0
    7/24/22, 13:20:19 / Recurring meetings / Are there open todos?
    set to 0
    

    With it turned off:

    7/24/22, 13:20:26 / Recurring meetings / Are there open todos?
    set to 0
    

    So it got the TF identifier line moved to the top and made it run just once.

    #47660
    Brendan
    Keymaster

    You don’t really need to set the totals into another field. Just return the total from the field script itself.

    But if you want to set the value on a field, you need to call form.saveAllChanges(); at the end of your script.

    If you’d like to email me a Tap Forms Archive of your form, I can take a look at it for you. support@tapforms.com

    Thanks,

    Brendan

    #47659
    Bob Williams
    Participant

    You basically just gave me the code solution to my backup plan of having a separate checkbox field that’s toggled by script. Thanks! Particularly since I’m not sure I’d have very quickly made the connection from the description in the docs between the record.getTotalOfLinkedFieldForField() method and the sub-fields in tables.

    That said, it’s only kind of working. Here’s my code:

    var todosTableId             = 'fld-cacbfd1e4cac47b085ef2c1e9e076ef2';
    var todosTable_doneCbFieldId = 'fld-b94f8f9b9e8e48168f844c03b0f5810d';
    var todosTable_ofFieldId     = 'fld-4f82e1aeef90484582e9d8cce49e3369';
    var openTodosFieldId         = 'fld-f1892a93ce12498abbced166e497256d';
    
    
    function areThereOpenTodos() {
     	var countDoneChecked = record.getTotalOfLinkedFieldForField(todosTableId, todosTable_doneCbFieldId);
    	var countOfChecked = record.getTotalOfLinkedFieldForField(todosTableId, todosTable_ofFieldId);
    
    	return (countDoneChecked + countOfChecked) > 0;
    }
    
    // record.setFieldValue(openTodosFieldId, areThereOpenTodos());
    
    if (areThereOpenTodos()) {
    	record.setFieldValue(openTodosFieldId, 1);
    	console.log('set to 1');
    } else {
    	record.setFieldValue(openTodosFieldId, 0);
    	console.log('set to 0');
    }
    

    The docs say a script is executed when a field it references with record.getFieldValue() is changed. Logical enough. But when I click the run button in the editor, this is the output:

    set to 0
    set to 1
    set to 0
    7/24/22, 11:50:13 / Recurring meetings / Are there open todos?
    set to 1
    

    One thing of note there is that TF’s own message about the script being run isn’t the first line of output.

    More notable is that the script appears to be run four times, and I can’t figure out why. Experimentally, I can’t connect it to anything I’m doing in the script. In fact, if I simplify the script to just this:

    console.log('set to 1');
    

    The output still suggests four runs:

    set to 1
    set to 1
    set to 1
    7/24/22, 12:06:30 / Recurring meetings / Are there open todos?
    set to 1

    Outside the script, four runs also doesn’t match the number of records the form has, or really anything else about my tiny database that I can see. So what’s happening here? And while it’s a secondary issue, the changing 0/1 value in the first output is weird since the fields being tallied aren’t changing during the script’s run.

    Also, a possible related bug: when I first wrote the script and ran it, I was getting syntax errors about one of my opening vars being defined more than once (“Can’t create duplicate variable”). Strangely, the given var changed from run to run (wasn’t consistently the first one), so I really don’t know what to make of it. But it didn’t happen every time, and after a dozen or so runs, it simply stopped happening.

    Finally, while the script ultimately seems to do the right thing in the editor when I either click the run button or hit the Save button, it doesn’t seem to run at all outside the editor. Adding/removing rows from the table, toggling the checkboxes in the table off/on, switching records, and even closing and reopening the file all fail to trigger a run. Maybe it’s running but producing an incorrect result in those contexts, which is certainly possible, though I don’t think so since Scripts > Show Console Log shows no output in those cases. It appears the only way for me to get it to run is through the editor. (And just to be clear, I am using a script field, not the form scripts feature.)

    #47658
    Brendan
    Keymaster

    Oh, the script wouldn’t be that complicated. Just use this function to get the total of the checked Checkmark fields in your Table field:

    var total_checked = record.getTotalOfLinkedFieldForField(table_field_id, checkmark_field_id);

    A Checkmark field is stored in the database as either 1 or 0. So getting the total would add up all the 1’s to give you the total and that would tell you how many todo’s are done.

Viewing 15 results - 781 through 795 (of 2,951 total)