Search Results for 'script'
Tap Forms Database Pro for Mac, iPhone, iPad and Apple Watch › Forums › Search › Search Results for 'script'
-
AuthorSearch Results
-
October 7, 2020 at 4:37 PM #42201
In reply to: Getting seconds from a date
Stefan de Graaf
ParticipantThanks for the help! Ah that was a copy-paste mistake indeed (older version of the script). I tried it with your version but keep getting the same “TypeError: undefined is not an object (evaluating ‘dataField.getTime’), line:(null)” error. And the date field is a regular date field (only the name has been changed) and has a date set. Will have to play around some more tomorrow.
October 7, 2020 at 2:31 PM #42199Topic: Getting seconds from a date
in forum Script TalkStefan de Graaf
ParticipantHey all,
Hopefully a quick question: I’m trying something along the lines of this in a script field (based on a different date field):
let dateField = record.getFieldValue('fld-060389f539934aa9b7163c7a8b7a4cd1'); let delta = Math.abs(givenDate.getTime() - new Date().getTime()) / 1000;In JS, you can use getTime on a date object, but it seems you can’t do this with a Tap Forms date field’s value: TypeError: undefined is not an object (evaluating ‘givenDate.getTime’), line:(null)
Is there a way to do this? Also tried creating a new Date() from the field’s value but that didn’t seem to work.
Thank in advance!
October 7, 2020 at 5:45 AM #42190In reply to: Calculating age from year
Stefan de Graaf
ParticipantThank you very much Sam! Both suggestions seem to work now (figured out that the first one worked all along but apparantly it doesn’t always recalculate the value when you adjust the calculation. when I changed the Year field, it did show up.. oops..).
And I didn’t even notice the script field (thanks for that suggestion!), otherwise I would’ve tried that as well. Might also be a nice way to generate text like “1 year, 10 months, 3 days ago”. Looks like it uses (some variant) of javascript, so that should be doable. Will try that tonight!
Thanks again for the help and quick reply!
October 7, 2020 at 2:55 AM #42186In reply to: Calculations using TODAY() not updating
Stuart Devlin
ParticipantOk thanks. Am I able to write a script that would do that once a day? Do I take it that the reason Bento could auto update the calculation was because the database was shared from a single computer?
This is not a huge issue at all, I’m just converting a Bento database that we use at work and it will end up being used by 3 or 4 people and the less anyone has to remember to do something the better.
BTW, I LOVE how I can make this look so similar to my Bento solution, it’s really going to help with an easy transition for us.
October 7, 2020 at 1:06 AM #42171In reply to: Calculating age from year
Sam Moffatt
ParticipantIn Try 1 you have Year as a number (good) and Age as a date (incorrect). You need the calculation field to be a number because the ages is going to be a number. That said date should have still shown up but still been wrong. I copied what you put on the forum, replace “Year” with the field placeholder for my form and it works when set to number.
In Try 2 you have an extra colon at the end of your calculation field. I created a date field and calc field also set to number which seemed to work properly for my tests.
Calculation fields are a little touchy at times and can give you an empty result without any feedback. I generally prefer script fields because it’s a little more obvious what the error is, that said it’s not all that friendly:
var year_date_field = record.getFieldValue('fld-060389f539934aa9b7163c7a8b7a4cd1'); parseInt((new Date() - year_date_field) / 1000 / 60 / 60 / 24 / 365.25)Essentially we take the current date from the year_date_field (it’s a date field, a number field would need to be converted from a year to a date like the calc field does) and then that gives us the difference in milliseconds. We need to convert that to years which is the division operations there. The
parseIntpiece is to convert it to a whole number, alternatively you could replaceparseIntwithMath.flooras well.Hopefully that helps!
October 5, 2020 at 1:46 AM #42133In reply to: Script stopped working
Sam Moffatt
ParticipantI don’t think Utils.alertWithMessage returns anything, it should block the script and continue executing on the next line, I don’t think it’s an async call but I could be misremembering. I have some prompter functions of my own (really borrowed/inspired by @daniel_leu) but I need to do some documentation work on them.
I don’t think you can suppress the message, I believe it’s intended to be a security measure which is relatively common best practices these days.
October 5, 2020 at 1:35 AM #42130In reply to: Script stopped working
Sam Moffatt
ParticipantGood to hear it’s working!
I can’t claim credit for the Javascript guide though, that’s T. L. Ford’s (or @cattailnu) hard work not mine. It’s a great contribution though because whilst Javascript in Tap Forms is similar enough to Javascript at large, a lot of existing resources focus on the web (which has it’s own features added by browsers) or projects like node.js (also with it’s own quirks). Hopefully some Tap Forms specific resources will help the community at large.
October 5, 2020 at 1:25 AM #42128In reply to: Script stopped working
David Gold
ParticipantI had one other question. I’ve changed the script to show the Comments field using alertWithMessage() (instead of copying to clipboard) and it’s working fine. If I wanted to then wanted to do something else after I’ve viewed the comments and pressed the “OK” button how do I do that? I can get it to work before I’ve pressed the OK but that doesn’t allow me to read the alert.
Also one other question – if one wants to open an external URL is there anyway to stop the message coming up that you are leaving Tap Forms (on iOS) or is that built in as a security measure to the Tap Forms app?
October 5, 2020 at 12:10 AM #42125In reply to: Script stopped working
David Gold
ParticipantIt’s working with your changes. Really appreciate it. Not coming up with any errors now. I’m going to try and expand what I want it so do and will come back if I have any queries (am just starting 201 of your Javascript/Tap Forms guide so getting there slowly…
October 4, 2020 at 4:05 PM #42123In reply to: Script stopped working
Sam Moffatt
ParticipantOne extra feature is that the console should make the link clickable so if you run it outside of the script editor (e.g. in the normal view with the console open) then it should take you to the record that it doesn’t think is setup properly.
October 4, 2020 at 4:03 PM #42122In reply to: Script stopped working
Sam Moffatt
ParticipantOh that’s an annoyance of the forum, it turns the backtick character into
<code>and</code>respectively even though it’s inside a script context already. In Javascript the backticks are used to create template literals which allow you to embed variables inside the string. Not particularly necessary in this case but something I’m in the habit of doing.If you’re looking for the record that is problematic, you can use
record.getUrl()for the record that fails the test andconsole.logit to find it.Here’s my copy of the script, I took out the template string so hopefully the forum doesn’t munge it and it’s been updated to point to different fields and field names (title and subtitle from the Books form in the document I use for the YouTube videos):
var myForm = document.getFormNamed('Book'); var records = myForm.getRecords(); var search_term = Utils.copyTextFromClipboard(); var result_count = 0; var results = []; var results_comments = []; var selected; function copy_comments( comments ) { Utils.copyTextToClipboard( comments ); } function copy_result_multiple( comments ) { if ( comments == true ) { console.log( 'Index:' + results.indexOf( selected ) ); console.log( results_comments[ results.indexOf( selected ) ] ); copy_comments( results_comments[ results.indexOf( selected ) ] ); } else { console.log( 'Cancelled' ); } } function multiple_results( all_results ) { let prompter = Prompter.new(); prompter.cancelButtonTitle = 'cancel'; prompter.continueButtonTitle = 'Copy Comment'; prompter.addParameter('Select Result ', 'selected', 'popup', all_results) .show('Multiple Results Found', copy_result_multiple ); } function search_records( haystack , needle ) { var title_id = 'fld-59b7b2dfa5c843afb969e74df4ad111c'; var subtitle_id = 'fld-b7d482d0db5d4554ad0948f19394f441'; var rec; for (rec of haystack) { var title = rec.getFieldValue( title_id ); if (title && title.includes( needle ) ) { results.push( rec.getFieldValue( title_id ) ); results_comments.push( rec.getFieldValue( subtitle_id ) ); result_count++; } else { if (!title) { console.log("Empty field: " + rec.getUrl()); } } } if( result_count == 0 ){ Utils.alertWithMessage("No Results", "No results were found for the search term: " + needle); }else if( result_count > 1 ){ //multiple results multiple_results( results ); }else{ //single result copy_comments( results[0] ); } } search_records( records , search_term );October 4, 2020 at 2:44 PM #42120In reply to: Script stopped working
Sam Moffatt
ParticipantOk, that’s actually a data problem, one of your records doesn’t have location_id set on it and you’re getting
undefinedback from thegetFieldValuecall. When you then chain that to add the.includes, you’re getting theundefined is not an objecterror becausegetFieldValuereturned undefined for the empty record. Chaining sometimes makes it have to figure out where something goes wrong, especially for contexts that can legitimately return a mix of return types.A simple fix would be to do something like:
var location = rec.getFieldValue(location_id); if (location && location.includes ...An extra variable but Javascript will test if it’s “truthy” and continue. For your purposes that shouldn’t hurt anything because you want to see if it has a search value anyway so presumably any field that is empty can be ignored (undefined and an empty string are both falsy).
Good to hear you enjoy the videos! Stuff on the forum is often the inspiration for videos so in a sense I’m putting my money where my mouth is because it’s sometimes easier to see something rather than read it.
October 3, 2020 at 11:53 PM #42114In reply to: Script stopped working
David Gold
ParticipantBrendan – I’ve uploaded the file.
Sam – I have the iOS version working (without the prompter – I didn’t realise the prompter was now supported). The issue I’m having is with the second script (the Mac one). The console is showing: “TypeError: undefined is not an object (evaluating ‘rec.getFieldValue( location_id ).includes’), line:(null)”
Let me know if you know what is causing it.
By the way love your videos!Attachments:
You must be logged in to view attached files.October 3, 2020 at 11:39 PM #42113In reply to: Script stopped working
Sam Moffatt
ParticipantDo you mind being a little more specific about which part isn’t working?
My first inclination would be to pepper the thing with
console.logstatements everywhere to see where it stops.I just copied the Mac version (the last post you did) and tried it out with a test document and it worked fine. I had to change the field names but when I ran it in the script editor it worked.
One minor change is where you have
if (result_count == 0 ){instead of just logging the error message, use an alert box:if( result_count == 0 ){ Utils.alertWithMessage("No Results", <code>No results were found for the search term: ${needle}</code>);Makes it a little more obvious there is no results available.
I didn’t look at this on iOS though but generally my scripts work identically on iOS and MacOS. I think there was a time when the prompter wasn’t on iOS but it should be there now but perhaps there is still something funky there.
October 3, 2020 at 11:17 PM #42112In reply to: Script stopped working
Brendan
KeymasterCan you upload your form (.tff file) with these scripts? I just fixed the attachment upload function.
-
AuthorSearch Results