Looking through the forums there are not really proper descriptions for the use of AppleScript. Several of the returns from a search are linked to threads which are blocked to non-beta participants too.
I have worked out how to open a Document with AppleScript, but would like to know the correct syntax:
1. For selecting a particular form in a document
2. Selecting a particular advanced search in a form; and
3. Running a form script in a particular form of a document
Hi Larry,
Are you remembering to call form.saveAllChanges(); after you return from your script? I’m sure you are. That should generate an update notification to update the record display.
Maybe if you email me an archive of your parent form then I could take a look at it and run it through the debugger to find out why it’s not updating the child record.
Thanks,
Brendan
Hello!
I’m working on a script to maintain a linked field via the value of another field both on the same form. The field that will be getting modified is a simple text field that will contain ; delimited string tags. The linked field that is based on those tags references a separate form with entries that are keywords. My goals is to parse the tags and have the linked field’s values set based on those tag values.
I have the script to do this pretty much working with a couple of caveats. The script removes all of the entries in the linked field via removeRecordFromField(), parses the tags, looks them up in the linked table, and adds them to the linked field using addRecordToField(). The script executes as expected but the linked field doesn’t seem to be updated. If I open the keyword form and go back to the main form the linked field the shows the updates. Also, if I add a new keyword into the tag field along with an existing one, the new keyword is created in the linked form, but the new one is the only value in the linked field.
Has anyone seen similar behavior in this type of scenario that might have some advice?
Thanks!
Hi Mike,
You could do this all in one script.
function hasAttachments() {
var attachments_field_id = 'fld-......';
var attachments = record.getFieldValue(attachments_field_id);
if (attachments.length > 0) {
return "YES";
} else {
return "NO";
}
}
hasAttachments();
When you call record.getFieldValue(attachments_field_id); Tap Forms will return an array of items. Then you just check to see if the length of that array is greater than 0 then you do have attachments.
Hope that makes sense.
Thanks,
Brendan
I have a form for keeping track of purchases that have warranties so when something breaks I can go look in one place instead of needing to go round up old receipts.
I do most work on iPad. There is a second form for log entries, and the log can include attachments.
When using the primary form, and then accessing the secondary form — it is useful to know if there are attachments in the log. You can see on mac but not on iPad.
Usually the brief info in log is enough but if there were an attachment, it might bear opening the log to see and a flag there is an attachment to know to open the log becomes useful.
If there is a way to make a script to notice an attachment exists and then yield “Yes” that would be a one step process. But not sure how.
Currently I use a script that yields a number 1 if an attachment exists in field it looks at. Then a second calculation field looks at the script field and if the script field is 1, it gives a textual “Yes”.
The script shows errors but does not seem to impede the function.
Maybe someone knows how to improve?
Eliminate errors in script; perhaps consolidate all functions in the script to get “Yes” instead of running two fields to get the “Yes”.
Thanks, Mike
Attachments:
You must be
logged in to view attached files.
To get the sort of autonumber feature you mention you need to have a shared generator to issue the numbers. How does the first device “know” how many records have been created on any other device?
You could set up a web service somewhere that all of your devices can refer to that sets the number. You’d need to create a web service somewhere that all of your devices can access and then a script field to retrieve the number from the service to save in your document. I’d make the script and it’s target field two so that you can handle if the number service is unavailable to give you a number.
This would give you a monotonically incrementing source of numbers though there is the risk that you will still have holes from time to time (numbers that are allocated but not used or used in deleted docs) however completely avoiding that in a fully distributed system like Tap Forms is impossible.
Hi Vera,
Ok, so your script is good, except the only thing you’re not doing is returning your invoice number value.
Where you’ve said record.getFieldValue(invoice_number_id), you could just prefix that with return. As in return record.getFieldValue(invoice_number_id);.
But better would be to just return the first invoice number from the first record in the array.
To do that, use this code after the var link_invoice line (line 5).
if (link_invoice.length > 0) {
var first_invoice_record = link_invoice[0];
return first_invoice_record = first_invoice_record.getFieldValue(invoice_number_id);
}
The above code just checks to see if there is at least one link_invoice record. If so, it then gets the first link_invoice record from the list of linked records. Then it gets the value from the invoice_number field.
And that’s it!
Hope that works for you.
Thanks!
Brendan
I’m sorry for being so dense, but I’m afraid I don’t completely follow. And I also think I neglected to specify something…
When you say I haven’t defined link_invoice, that’s the name of the link-field I had set up to link the two forms, but it wasn’t in my screen shot. I assumed a link field is more like a reference field (like hidden instructions) and wouldn’t display any content on my forms. If it should display content, can you tell me what and how? Or do you mean it needs to be defined as a variable at the top? But I’m still at a loss for what to define.
I followed your instructions for the Child Records Loop script and got my hopes up when it didn’t return an error in the script editor, but I still did something wrong. It puts “Hello World” in all the invoice_number fields when that should either be one of two different invoice numbers or blank — to coincide with the month-year field contents. Where the “do something” placeholder comment appears, I tried doing what made sense to me. The result is the same with or without that. And I don’t know where it got “Hello World” from. It doesn’t appear in any record of either file — except where it put it.
My new attached image shows all this.
Attachments:
You must be
logged in to view attached files.
Hi Vera,
There are a couple of problems with your script.
First off, you haven’t defined link_invoice anywhere, yet you’re referencing it.
Also, you haven’t defined index, yet you’re using it as an array reference.
Take a look at the Child Records Loop snippet. If you select your invoice number field from your Linked form on the Fields list on the left, then double-click on the Child Records Loop snippet at the bottom-left of the Script Editor, Tap Forms will write a function for you to loop through all of the records in your link_invoice Link to Form field.
Since it’s a many-to-many, there can be multiple values for Invoice Number.
But if all you want is the first one, then you could do:
function get_invoice_number() {
var invoice_number = '';
var link_invoices_id = 'fld-.....';
var link_invoices = record.getFieldValue(link_invoices_id);
if (link_invoices.length > 0) {
var first_invoice = link_invoices[0];
var invoice_number_id = 'fld.....';
invoice_number = first_invoice.getFieldValue(invoice_number_id);
}
return invoice_number;
}
get_invoice_number();
You of course have to replace ‘fld….’ with your own field IDs, which you can get from the Fields list on the left.
I have two linked forms (from different sources): TRANSACTIONS and INVOICES
Both of them have fields named month-year and and invoice-number
In TRANSACTIONS, month-year is a calculation field (based on a date field in the same form).
In INVOICES, month-year and invoice-number are text fields (not number or date) and I type in the info when I create an invoice. Both are unique values across all records. Everything is fine up to this point, but…
I want the invoice-number field in TRANSACTIONS to display the invoice-number from INVOICES for all TRANSACTION records where month-year match. I tried both a calculation and a script. The calculation inserts a count formula, which is not what I want. The script gives me an error when I test it inside the script editor. I attached a screen shot of the script & error.
The forms are linked by month-year — one to many — but I tried the other linking options and they were no better.
I’m sure my logic is not what it should be due to my JS illiteracy. Can you tell me what I’m doing wrong?
Attachments:
You must be
logged in to view attached files.
If it’s the way I have it set up, you don’t even need to use a script field any more. A regular calculation field will work and all you’ll have to do is select the field from the field list on the left. Easy peasy!
Technically you could also do something similar with scripting and a little bit of chaining:
record.getFieldValue('fld-lnkfromform').getFieldValue('fld-targetfield');
If there isn’t a parent record set, it’ll internally error out but that should be mostly harmless as a single line.
The code above is Objective-C. But the if statement looks pretty much identical to how it would in JavaScript. Dots are just used to get the value of an object. For example, you’re Vera. That’s your first_name. Vera is a Person. So to get your name I can say person.first_name and I would be given the first name of you :)
Square brackets aren’t too scary either. They just help you to index into an array. You’ve seen an Excel spreadsheet I’m sure. They have row numbers on the left. In fact, the multi-column list view shows row numbers in Tap Forms. To get a value from an array you would just say people[10]. That would give you the 10th person from the people array :). Really not too scary if you think about it.
Is that algorithm javascript? If so, THAT’S the kind of scripting I understand — except maybe the first 3 lines. Whenever it gets into arrays or uses “.” or square brackets, I get lost. I’m not a programmer.
Yes, send PDFs now (with my FM solution), but I would like him to be able to see the info as up-to-date as I have it. Mine may be a few weeks old as I only import records at the start of the month (due to FM reboot nuisance). But if TF import and syncing from my iPad works well (that’s where I do most of my record-keeping), I might import more frequently. So thank you for that answer.
Hi Oliver,
Although you can colour fields and values in Tap Forms, they are global settings on the Field object itself. So any change made to them will apply to every record, not just for a particular record.
But there is a labelColour and valueColour property on the Field object that can be accessed from a Script.
Thanks,
Brendan
Hi Brendan,
I had no idea TF has been around for so long. I never heard of it until a few weeks ago when I started trying out alternatives. And I’m sorry if I gave the impression that you should t be using JavaScript. It wasn’t what I meant at all. I fully understand if not for JS, this would be a far more expensive program. I also know JS is everywhere, and I’ve heard it was easy to learn, but it hasn’t been so for me. That’s what I was getting at. I bought a few books about 15 years ago and tried learning but finally gave up. As close as I get to programming now is HTML, CSS, and spreadsheet formulas, and I know these aren’t programming. But I’m willing to take small bites of JS.
BTW: May I suggest you set up a separate forum board for feature requests? It might be a good way for people to let you know what they’d most like and perhaps vote on what others like. Likewise, if it’s something that already exists, it might be a good way to get the word out.
Thanks again!