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 - 2,311 through 2,325 (of 2,867 total)
  • Author
    Search Results
  • #36575
    matt coxon
    Participant

    Hi Sam, thanks very much for the response, I understand what you mean but can’t seem to get the syntax correct

    Do I add the command to the main script field after the vlookup or in a new script field I played around with various but can’t seem to get it to work, but when I do I’ll need to apply it to date fields in the same scenario as the web field so will be very useful

    Thanks

    #36555
    Sam Moffatt
    Participant

    You can load the attachment up into your existing document, alternatively you can get this script, create a new form script and update the field ID’s at the top to match your field IDs. On Tap Forms 5.3 or later, form scripts are a third tab next to form and field settings.

    #36532
    namor
    Participant

    dear brendan

    thanks a lot! i could add the fields together easily
    but i could find out yet how to hide the field in the form.

    i have some other fields that i would like to hide as
    an ascending number i use for a calculation that doesn’t
    have to show up.

    is there a simple script?

    best, roman

    #36523
    Sam Moffatt
    Participant

    You can do the same on the iOS version as on the Mac, in the calculation editor you need to use [Field Name] instead of clicking on the fields and also you need to make sure you use the quote button above the keyboard to avoid inserting smart quotes.

    The examples above in the iOS calculation editor would be CONCAT([First Name];" ";[Last Name]) and CONCAT([Street];" ";[City];" ";[State]).

    Doing the script example on iOS is a few extra taps to select the fields but not that bad. I prefer script fields because of the better error reporting and testability (just click the “run” button to see if the field will work; make sure you’re on a record first though or you’ll get an error that record isn’t defined or similar). I wouldn’t want to do a lot of script or calculation field work on iPhone but on the iPad it isn’t too bad if you have a full keyboard. I’ve written some pretty crazy stuff on the phone but doing it on the desktop and using P2P sync or CouchDB sync is much easier.

    Attached is a video of doing a new script field on my iPhone in one of my test databases, reordering it to make sure it’s the first and then going to the list view on the phone.

    Attachments:
    You must be logged in to view attached files.
    #36522
    Sam Moffatt
    Participant

    You can’t make a script field clickable (would be nice, it’s on my wish list*) however as a workaround what you can do is use the script field to set the value of a web site field using record.setFieldValue(field_id, value). Then when the script is run, it’ll update the web site field to be the new value which means you can then click on that field to open the link.

    * I’d actually like a way of being able to define a custom icon for a script field and an action handler distinct from value. I think it’d be cool to have the ability to have the script field display one value but when you click it, it opens up a link or perhaps even runs a form script with a field context set. Doing all of that and getting it right is a lot of work and perhaps a little too powerful.

    #36520
    Sam Moffatt
    Participant

    Try something like this:

    var address_id = 'fld-fb97220d3a4a4df48df4a54030766c54';
    
    record.getFieldValue(address_id).match(/Cupertino/) ? 'Yes' : 'No';
    

    You can use the method match on strings, it takes a regular expression which is why the string is in backslashes not quotes. I use a ternary operator to take the output of the match and do a ‘yes’ or ‘no’ based on if something was found.

    W3C Schools page for match: https://www.w3schools.com/jsref/jsref_match.asp (has links for further details on regular expressions)
    Mozilla page on ternary operator: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator

    #36515
    Sam Moffatt
    Participant

    Create a new calculation field, in the form options you can set it up. Here’s a quick recording of how do that with a calculation field.

    Looks like I hit a bug/quirk that commas, even in the quoted strings, seem to trigger something. That one’s for Brendan to look at.

    To work around that, I also did a similar example using a script field. It’s a little more verbose but I find the script fields easier to work with than the calculation fields because it has better error reporting (easier to know what you did wrong). That’s probably less likely if you don’t learn JavaScript but there are plenty of resources on the web to learn JavaScript.

    As you can see, both of the calculation fields update the list view on the left to display the composite data. Hopefully this helps!

    Edit: Had to trim the calculation field recording to get it to fit under the file size limit. I attached a screenshot of what the final calculation that worked looked like (I ended up doing some experimenting with field separators).

    Attachments:
    You must be logged in to view attached files.
    #36512
    matt coxon
    Participant

    Hi, I’m using the vlookup script by (Sam Moffatt) great script thank you Sam!!!

    Is there a way to make a script field be a clickable URL / Hyperlink (as it would be if you had selected the “Web Site” field

    This is the script,

    Thanks Matt

    form.runScriptNamed(‘vlookup’);

    var trello_cards_id = ‘fld-a6346935a6af40d1833fad4f49958bca’;
    var tsm_rfs_id = ‘fld-b0c9111f87064a23bde75c965374e97c’;
    var short_url_id = ‘fld-7888517ad8204f5aa7a4bf926c92be15’;

    var rfs = record.getFieldValue(‘fld-e13aa1493c6f4b919f30b27cd8bc8ae9’);

    vlookup(rfs, trello_cards_id, tsm_rfs_id, short_url_id);

    matt coxon
    Participant

    Hi, im playing around with scripts, (i’m new to them) is there a way to search a field for a partiular string and the output accordingly

    Example would be

    Field “Labels” has the following text (urgent,storage,emea,network,office moves,colo,new)

    I’d like the script to search for the string “office moves” and return either a “yes” or whatever Id like

    Is that possible
    Thanks

    #36481
    Brendan
    Keymaster

    @John,

    You could simulate that with a Calculation or Script field to concatenate the values.

    #36416
    Sam Moffatt
    Participant

    Ok, I’m going to attach a form template for this as well but the gist is naming a bunch of checkboxes the same way and then managing their state.

    Two sets of checkboxes, one named “CBA” then a number and another set named “CBB” then a number. I created 15 of the CBA and 4 of the CBB. Too easy.

    The biggest challenge in this is managing all of the checkbox ID’s because clicking all of those is a pain. Step 1: Form Script to generate Javascript for later (I called this “Generate Checkbox Script” but you could call it something else):

    var fields  = form.getFields();
    
    console.log(`var cbaList = [];`);
    console.log(`var cbaValues = [];`);
    console.log(`var cbbList = [];`);	
    
    for (var index = 0; index < fields.length; index++)
    {
    	if (fields[index].name.match(/CBA/))
    	{
    		console.log(`cbaList.push('${fields[index].getID()}');`);
    		console.log(`cbaValues.push(record.getFieldValue('${fields[index].getID()}'));`);
    	}
    	
    	if (fields[index].name.match(/CBB/))
    	{
    		console.log(`cbbList.push('${fields[index].getID()}');`);
    	}
    }
    

    This is an abuse of console.log but essentially I’m using it to write Javascript code. What we’re doing is asking the form for all of it’s fields (form.getFields()) and then looking to see which ones match CBA and which ones match CBB. The CBA ones we want to track their state, so we put the field ID into a list and then also grab their value at the same time. For CBB, we don’t necessarily care about it’s values until we know that all of CBA has been ticked, so we don’t bother with the extra lines. That for my template generates this:

    var cbaList = [];
    var cbaValues = [];
    var cbbList = [];
    cbaList.push('fld-955441d1635b4ce399a4d6008c7ee37d');
    cbaValues.push(record.getFieldValue('fld-955441d1635b4ce399a4d6008c7ee37d'));
    cbaList.push('fld-b22653c0a9904dcd976c31e8ba0388fe');
    cbaValues.push(record.getFieldValue('fld-b22653c0a9904dcd976c31e8ba0388fe'));
    cbaList.push('fld-49d779446f6b42a897a11d137e1c2817');
    cbaValues.push(record.getFieldValue('fld-49d779446f6b42a897a11d137e1c2817'));
    cbaList.push('fld-308e12249c64487fae2237f325fbc21d');
    cbaValues.push(record.getFieldValue('fld-308e12249c64487fae2237f325fbc21d'));
    cbaList.push('fld-99ade950b34c4d53bd98c218dce8d8ef');
    cbaValues.push(record.getFieldValue('fld-99ade950b34c4d53bd98c218dce8d8ef'));
    cbaList.push('fld-617fcf4b482e44cf92eac93d4719b2c9');
    cbaValues.push(record.getFieldValue('fld-617fcf4b482e44cf92eac93d4719b2c9'));
    cbaList.push('fld-3c97916b19f049bbb7edf6a2c68d0e3a');
    cbaValues.push(record.getFieldValue('fld-3c97916b19f049bbb7edf6a2c68d0e3a'));
    cbaList.push('fld-fc1d9f241ac745119901b4e1cabaab7b');
    cbaValues.push(record.getFieldValue('fld-fc1d9f241ac745119901b4e1cabaab7b'));
    cbaList.push('fld-2beae294572d40178c5f31b39d9d2f3e');
    cbaValues.push(record.getFieldValue('fld-2beae294572d40178c5f31b39d9d2f3e'));
    cbaList.push('fld-53b3172d013343eaa510169e71cc1a15');
    cbaValues.push(record.getFieldValue('fld-53b3172d013343eaa510169e71cc1a15'));
    cbaList.push('fld-880186ebe46e49cea5e2fa487e0112dc');
    cbaValues.push(record.getFieldValue('fld-880186ebe46e49cea5e2fa487e0112dc'));
    cbaList.push('fld-3c644b46b5b64f5e80575c601ef86938');
    cbaValues.push(record.getFieldValue('fld-3c644b46b5b64f5e80575c601ef86938'));
    cbaList.push('fld-1c85ce9ada1643ac89eb3304fffdb730');
    cbaValues.push(record.getFieldValue('fld-1c85ce9ada1643ac89eb3304fffdb730'));
    cbaList.push('fld-614d0018122d4a8eab464f96a3a4a623');
    cbaValues.push(record.getFieldValue('fld-614d0018122d4a8eab464f96a3a4a623'));
    cbaList.push('fld-16d97ccc160b4d26b572e38c89633908');
    cbaValues.push(record.getFieldValue('fld-16d97ccc160b4d26b572e38c89633908'));
    cbbList.push('fld-2437b06a3d3c4646a11c093e260fe860');
    cbbList.push('fld-b465ee9958c44acdab05ceb042bc582e');
    cbbList.push('fld-5fa2c5b939354661b706ba35a55fabc8');
    cbbList.push('fld-1748eee5b1eb42f19fab7e74431a2710');
    

    The three lists (cbaList, cbaValues and cbbList) end up forming the basis of the next piece. I pasted that into a new field script replacing it’s contents (I called mine “Checkbox Watcher” but really you can name it what you want).

    I then wrote some stuff! Let’s take it apart, piece by piece:

    var setFlag = 0;
    var result = 1;
    

    Setting up our variables: setFlag is used to know if I set a field and result is used to see if all of the checkboxes have been checked.

    for (var cba = 0; cba < cbaValues.length; cba++)
    {
    	result = result & cbaValues[cba];
    }

    This is a simple loop over all of the CBA field values and it does a logical AND operation on each of them against result we created before. If any of them are zero/false, then result will be false and zero out for the rest of the runs. If they’re all set to one, then they’ll all come together. This means that the script will work on an arbitrary number of checkboxes without having to know precisely how many checkboxes there are.

    if (result)
    {
    	for (var cbb = 0; cbb < cbbList.length; cbb++)
    	{
    		console.log(`${cbbList[cbb]}: ${record.getFieldValue(cbbList[cbb])}`);
    		if (!record.getFieldValue(cbbList[cbb]))
    		{
    			setFlag = 1;
    			record.setFieldValue(cbbList[cbb], 1);
    			document.saveAllChanges();
    			break;
    		}
    	}
    }
    

    First bit of fun, if we have all of the checkboxes checked, look for a checkbox from CBB that hasn’t been checked and check it. When it’s checked, setFlag is toggled for the next chunk. document.saveAllChanges() is sprinkled in to make sure to tell TapForms to persist our changes.

    if (setFlag)
    {
    	for (var cba = 0; cba < cbaList.length; cba++)
    	{
    		record.setFieldValue(cbaList[cba], 0, false);
    	}
    	document.saveAllChanges();
    }
    

    If setFlag was set, then this loops through all of the CBA checkboxes and sets them to zero. There’s a third flag to setFieldValue here which is set to false. This is a special flag that tells TapForms to *not* trigger script changes which would call this script again and cause it to loop. More saving of changes too!

    document.saveAllChanges();
    
    console.log(new Date());
    
    console.log(`All checked? ${result}`);
    
    console.log(`Did we set overflow? ${setFlag}`);
    
    `${result}/${setFlag} @ ${new Date()}`;
    

    For good measure I have a final saveAllChanges that really is there from when I first started the script. I log out some debugging messages starting with a date, if all of the checkboxes were checked, if this invocation of the script set the overflow and then it sets the script field value to a slightly formatted form for easy reviewing.

    Gotchas

    I didn’t test this on iOS but it should work fine there as well.

    When you import the template, open up the “field script” and save it. This should force Tap Forms to set up the watching behaviour properly. For me when I did a quick test import, that didn’t work properly however editing the field and saving it worked properly.

    The last gotcha I found with this was that on the Mac, the UI didn’t seem to update properly after the last checkbox was checked. Since I’m uploading this template, hopefully the keymaster can have a look and see what’s up there. If I navigate away from the record to another one and then return, the checkbox state is correct. If I click on “recalculate formulas”, it refreshes the UI properly.

    Why the form script and the field script?

    A fine question is why I even write Javascript to write Javascript at all, couldn’t I just run the same Javascript directly? And the answer to that question is actually yes! I could have a field script that handled the same logic as the form script and it would mostly work. The only requirement would be to press the “recalculate formulas” to trigger the field script to run. For TapForms to trigger updates to a field script automatically, it needs to know which fields to ‘watch’. TapForms has some heuristics it runs to figure that out but the simplest is looking for getFieldValue('fld-id'). The point of the form script is to write the Javascript we need to get TapForms to watch the fields (in this case the 15 checkboxes) we care about. It’s also another reason why we don’t get the values of the CBB checkboxes that way: we don’t want to get updates for these fields, so we avoid triggering that behaviour.

    This particular behaviour is on the edge of magic for me. I’ve made my peace with it because it mostly works and it is easy enough to work with or around once you understand it.

    Attachments:
    You must be logged in to view attached files.
    #36414
    Bert Rietveld
    Participant

    Thanks, Brendan. Waiting then (impatiently) for the next update ;)
    I’ll continue my workaround for now: Copy the URL to the clipboard and running a fastscript or using keyboard maestro.

    #36408
    Sam Moffatt
    Participant

    A straight forward implementation would likely be if/else but I’ve been thinking about the checkbox use case with some sort of array to do a for loop. I think it might be achievable with some introspection but it hasn’t quite come to me yet.

    Everything will work on iOS except for the custom layouts. Running the scripts on iOS is a few more taps.

    #36407
    Sam Moffatt
    Participant

    I don’t think this is a scripting limitation, this is a MacOS App Store sandbox limitation. App Store apps can only access files in its sandbox or files that you have used the Apple file dialog to select.

    Are you using the App Store version? I thought at one point there was a stand-alone version that might not have the same limitation (perhaps try the trial version?).

    #36406
    Bert Rietveld
    Participant

    Javascript support is a great addition to Tap Forms, but has some limitations (for good safety reasons): It cannot open local files. Unless I’m missing something, it cannot trigger an AppleScript (which could open local files). Putting a local file URL in a website field results in “Tap Forms Mac 5” does not have permission to open…”. Dropbox file URLs work but I cannot move everything over to Dropbox (>2TB). Some mechanism to script a button to get to local files would be very good. Does anybody know of a way how to do this?

Viewing 15 results - 2,311 through 2,325 (of 2,867 total)