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 - 1,321 through 1,335 (of 2,952 total)
  • Author
    Search Results
  • MiB
    Participant

    Thank you, I’ve changed that.
    Of course you can use whatever you wish from this conversation. I have learned a lot, and it was fun. I think, I should learn some java script, just for fun. :-)
    I will attach the latest files. You can use them in your video if you like.
    – Or everybody else, who is reading here. You just have to change the username “mib” to your own in the Script and in the command file.

    Attachments:
    You must be logged in to view attached files.
    #44227
    Dirk Soenen
    Participant

    Hi Daniel,

    Thanks for your response. I’ve never used scripts, this is definitely the moment to explore. I’m starting with the tap forms javascript scripting 101 manual today. Never too old to learn.

    #44226
    Sam Moffatt
    Participant

    Indeed! Good news you got it sorted and you’ve begun your Tap Forms scripting journey!

    Sam Moffatt
    Participant

    Sounds good!

    One other thing I noticed you created the script as field script, I think you should recreate it as a form script. Form scripts are intended to be run on demand which fits this case more. Script fields update when a referenced field changes and are more intended to work with the individual records.

    Once Brenden’s done the public release, I’ll do a video with this. Do you mind if I use your use case and sample data as an example?

    #44223
    Daniel Leu
    Participant

    Your contact field returns just a regular javascript object. To get the name, you can use contact.name or contact["name"].

    Example:

    function Get_Contact_Name() {
    	var contact_id = 'fld-xxx';
    	var contact = record.getFieldValue(contact_id);
    	return contact["name"];
    }
    
    Get_Contact_Name();

    Cheers, Daniel

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

    #44222
    Fern
    Participant

    Is there a way to grab only the name of the linked Contact? (I’d love to not have to manually enter a contact’s name; I’ve just been linking to a record in iOS’s Contacts.)

    With my almost nonexistent knowledge of scripting, I’ve only gotten it to return the entirety of the iOS record, i.e.{ id = “blablabla”; name = “blablabla”; phoneNumbers = {home = blablabla; mobile = blablabla;};} etc

    #44221
    Fern
    Participant

    This is my first time using scripts in TapForms and I figured out how to adapt Daniel’s version to my forms and where to put it! (Funnily enough I figured out the former first…) This is run-of-the-mill for people like you and Daniel, but it’s a total achievement for me. Thank you so much for your help!

    #44220
    Daniel Leu
    Participant

    Yes, you can check this in a script:

    function Has_Photo() {
    	var photo_id = 'fld-xxx';
    	var photo = record.getFieldValue(photo_id);
    	
    	if (photo.length == 0){
    		return "has no photos";
    	} else {
    		return "has " + photo.length + " photos";
    	}
    
    }
    
    Has_Photo();

    Cheers, Daniel

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

    Sam Moffatt
    Participant

    Thanks a lot for your effort!
    I fiddled around with this (again: without knowing any java script, so excuse me for asking dumb questions).

    We learn by asking questions and making mistakes, if you’ve got the question there is also likely someone else like you that has a similar question. My hope as always is that this helps not only yourself but maybe someone else who finds this with similar questions.

    ls * > files.txt results in a listing of all directories with an “:” after it an everything in it.
    ls > files.txt results in just the directories, but also the files.txt which is created before the txt-file is filled.
    ls -d */ > files.txt eliminates that issue but generates an “/” behind each directory.
    The solution would be: ls -d */ | sed -e ‘s-/$–‘ > files.txt

    Makes sense, I did some playing and this might also work out for you:

    find  * -mindepth 0 -maxdepth 0 -type d
    

    It’s a single command but I’m not sure we’re gaining much at this point. I was thinking about find earlier but it has it’s own quirks but this seems stable from my limited testing. My personal use case involved just JSON files so easy enough to wildcard match on those filenames.

    The colon being appended seems weird though, I wonder if that’s some other sort of flag being introduced. I’ve not seen that before, very interesting. The colon used to be the old MacOS path separator so perhaps it’s that?

    – This line crashes tapforms:

    targetRecord.addPhotoFromUrlToField(prefix + sourceFile + “/folder.jpg”, foto_id);

    That might be a bug for @Brendan to look into then, it’s probably not handling the file:// prefix properly.

    Three hours later reading through the basics of regex and Stackoverflow, I have fixed that:

    let pieces = sourceFile.match(/(.*)\s\[([^\]]*)\]/);
    

    works fine.

    Glad to hear you figured it out, as I said it’s close but obviously not something I was able to directly test and validate so I expected some minor tweaks.

    What doesn’t work:
    – One additional (empty) record is made. I guess from the “files.txt in the same directory. (Not really a problem. I just delete it.)

    This might be that there is an empty line from the \n split, something simple that might catch that is the following:

    	for (let sourceFile of indexFile.split("\n")) {
    		if (sourceFile.length < 3) {
    			console.log("Empty source file, skipping: " + sourceFile);
    			continue;
    		}
    

    Basically if the length of the string is less than three (empty string, “.” or “..”) then it’ll skip that line. Assumption is that your folders will always be three or more characters long.

    – info.txt is nowhere to be seen in the records.

    I’d do a console.log on the return value of the Utils.getTextFromUrl(prefix + sourceFile + "/Info.txt") just to see what you’re getting back as a first step for debugging. I did some testing and it feels like maybe the spaces are being messed up by Tap Forms, perhaps another bug for Brendan.

    – folder.jpg does not appear anywhere in the records. The line of code crashes tapforms.
    If it is changed to:
    targetRecord.addPhotoFromUrlToField(foto_id, (prefix + sourceFile + “/folder.jpg”));
    (foto_id in front of the path and brackets) it doesn’t crash, but isn’t doing anything either. _ And that’s not the order in which it should be, regarding to the manual!

    The swapped order likely doesn’t crash because it can’t find the field and is probably silently failing instead of outright failing. Crashing Tap Forms probably should have generated an alert for @Brendan though in those cases if you have a crash dump I usually also reach out to support@tapforms.com with the crash dump to let him know what I was doing if I can reliably reproduce it to get it fixed. As I said this isn’t something I’ve tested personally but is something at some point I want to work towards.

    Not all jpgs are named “folder.jpg”. Would it be possible zu take just *.jpg and add it? (Of course I have to make sure, there is just one then.)

    Once the above bug gets fixed then you can use a similar sort of index file technique to generate a well known file path to import the JPEG files from. This is a little ugly but is something like what I’d do to get it to work:

    (IFS=$'\n'; rm files.txt; for FOLDER in $(find  * -mindepth 0 -maxdepth 0 -type d ); do echo "$FOLDER" >> files.txt; pushd "$FOLDER"; pwd; ls *.jpg > images.txt;  popd; done )
    

    It’s a bit ugly and intended to be used with bash. The IFS portion tells bash to use newlines to split strings so that spaces don’t mess it up. I’m going to use this one liner to also generate the files.txt as well, so we delete it before the run starts. I used the find syntax from earlier to find all directories in the current directory and the for FOLDER to loop over them. I dump the folder name into the files.txt file and change into that directory with pushd. The pwd is there to just print out the full path for debugging just in case you end up somewhere you’re not expecting. The ls is there to grab the files and then popd to go back ot the parent directory. It’s wrapped in parentheses to prevent the IFS from persisting beyond the command execution. I’d probably put it in a simple shell script for safe keeping as well.

    Then in Tap Forms you can use Utils.getTextFromUrl on that path and loop over it to add it to the photo field.

    Sounds like we’re making progress though! Now it feels like we’re hitting some bona fide TF bugs :D

    #44205
    Sam Moffatt
    Participant

    If People is linked to Communication via one-to-many, then the person will show up in the joined table under Day.

    One to Many fields on the child side of the relationship can only have a single parent so it’s able to show that single person.

    If People is linked to Communication via many-to-many, then the People field is empty in the Communication table under Day.

    In this situation there are many possible links, so it’s not possible to show a singular field value. You can use a script field to join the records together from the linked field so that it can be displayed.

    I want to go into Day and easily see everyone I interacted with. Any hacks or ideas for how I can set this up to be able to have multiple People linked to a Communication AND still have them listed in the Communication table under Day?

    You can use a script field to merge the linked People fields in the Communication record and that will then show up in the Day record with the linked communication.

    I have an example of joining child records using a space and Daniel has some examples of joining child records as a comma separated string. It’s a little bit of scripting to basically grab the name field from your person record and then put a comma in. Daniel’s got a couple of examples with some great comments to help you out.

    MiB
    Participant

    Thanks a lot for your effort!
    I fiddled around with this (again: without knowing any java script, so excuse me for asking dumb questions).

    ls * > files.txt results in a listing of all directories with an “:” after it an everything in it.
    ls > files.txt results in just the directories, but also the files.txt which is created before the txt-file is filled.
    ls -d */ > files.txt eliminates that issue but generates an “/” behind each directory.
    The solution would be: ls -d */ | sed -e 's-/$--' > files.txt

    – This line crashes tapforms:

    targetRecord.addPhotoFromUrlToField(prefix + sourceFile + "/folder.jpg", foto_id);

    But there are other issues I can’t solve:

    This part seems to work fine:

    function Script() {
    var foto_id = 'fld-38a03d032ec544f5a249080c91527ea1';
    var titel_id = 'fld-d85f5b27ea4845bcaf67296e9601d77f';
    var autor_id = 'fld-d69f7da098ab42339650e1cce7a7f2e6';
    var inhalt_id = 'fld-186f3e4f483c446884d11c771292a78f';
    
    function Import_Entries() {
    	// prefix is the path to where the root directory is (also link this via Script Folder Access)
    	let prefix = "file:///Users/mib/Downloads/x/";
    
    	// load up the list of the files 
    		let indexFile = Utils.getTextFromUrl(prefix + "files.txt");
    	
    	// check if we got content or not.
    	if (!indexFile) {
    		console.log("No index file?");
    		return
    	}

    This next one, seems to work too:

    for (let sourceFile of indexFile.split("\n")) {

    But the regxp, how you call it, isn’t.

    This:

    let pieces = sourceFile.match(/(.*)(\[([^\]]*)\])*/);
    		if (pieces) {
    			// if we found two parts, set title/author
    			titel = pieces[1];
    			autor = pieces[2];
    			//debug
    			console.log(titel);
    		}
    

    results in 4 lines (I have filled directory “x” with 4 example directories) with just the directory names unchanged.
    A console.log(autor); instead of console.log(titel); will result in 5 (!?) “undefined”.

    Three hours later reading through the basics of regex and Stackoverflow, I have fixed that:
    let pieces = sourceFile.match(/(.*)\s\[([^\]]*)\]/);
    works fine.

    ________________________________________________

    So for now, creating new records with title and author works.

    What doesn’t work:
    – One additional (empty) record is made. I guess from the “files.txt in the same directory. (Not really a problem. I just delete it.)

    – info.txt is nowhere to be seen in the records.

    – folder.jpg does not appear anywhere in the records. The line of code crashes tapforms.
    If it is changed to:
    targetRecord.addPhotoFromUrlToField(foto_id, (prefix + sourceFile + "/folder.jpg"));
    (foto_id in front of the path and brackets) it doesn’t crash, but isn’t doing anything either. _ And that’s not the order in which it should be, regarding to the manual!

    Not all jpgs are named “folder.jpg”. Would it be possible zu take just *.jpg and add it? (Of course I have to make sure, there is just one then.)

    Do you have an idea what’s wrong? (For a quick test, I have attached my files. – The database backup is under X:\)

    Attachments:
    You must be logged in to view attached files.
    #44199
    Daniel Leu
    Participant

    Both record.setFieldValue and record.setFieldValues require the field id as argument. That’s why your script doesn’t do anything since you provide a variable that doesn’t contain a valid ID.

    var name_id = 'fld-db21a48449d84ef4bfb6f4fb28473fa8';
    var namex = record.getFieldValue('fld-db21a48449d84ef4bfb6f4fb28473fa8');
    
    var deed_id = 'fld-9fe105d0c1a344b988899fafbb1f6e01';
    var deed = record.getFieldValue('fld-9fe105d0c1a344b988899fafbb1f6e01');
    
    var n_as_witness_id = 'fld-20109866d84b4587866be0cb63853547';
    var n_as_witness = record.getFieldValue('fld-20109866d84b4587866be0cb63853547');
    
    var number_of_witnesses_id = 'fld-7038bb074b3f42bca3ca6411c38ebe8b';
    var number_of_witnesses = record.getFieldValue('fld-7038bb074b3f42bca3ca6411c38ebe8b');
    
    var public_form_id = 'fld-e50974efdf284413abafd8aa9ebf36f1';
    var public_form = record.getFieldValue('fld-e50974efdf284413abafd8aa9ebf36f1');
    
    console.log(namex + " " + deed + " " + n_as_witness + " " + number_of_witnesses + " " + public_form);
    
    record.setFieldValues({[deed_id]: 'Yes',[n_as_witness_id]: 'Yes', [number_of_witnesses_id]: '1', [public_form_id]: 'No'});
    
    document.saveAllChanges

    I don’t really know what this script does since you are reading values and then overwriting them. But at least it stores the values now.

    Cheers, Daniel

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

    #44197
    Victor Warner
    Participant

    I have a simple requirement: for an existing record I wish to set certain fields with particular values (they are linked to pick lists).

    According to the function lists I can use record.setFieldValue() or record.setFieldValues() but it is not working. Ideally I would like to use a Form script (and assign to shortcut key) as these default values only apply for some records but not others.

    The script is as follows:

    var name_id = 'fld-db21a48449d84ef4bfb6f4fb28473fa8';
    var namex = record.getFieldValue('fld-db21a48449d84ef4bfb6f4fb28473fa8');
    
    var deed_id = 'fld-9fe105d0c1a344b988899fafbb1f6e01';
    var deed = record.getFieldValue('fld-9fe105d0c1a344b988899fafbb1f6e01');
    
    var n_as_witness_id = 'fld-20109866d84b4587866be0cb63853547';
    var n_as_witness = record.getFieldValue('fld-20109866d84b4587866be0cb63853547');
    
    var number_of_witnesses_id = 'fld-7038bb074b3f42bca3ca6411c38ebe8b';
    var number_of_witnesses = record.getFieldValue('fld-7038bb074b3f42bca3ca6411c38ebe8b');
    
    var public_form_id = 'fld-e50974efdf284413abafd8aa9ebf36f1';
    var public_form = record.getFieldValue('fld-e50974efdf284413abafd8aa9ebf36f1');
    
    console.log(namex + " " + deed + " " + n_as_witness + " " + number_of_witnesses + " " + public_form);
    
    record.setFieldValue(namex,'James Smith');
    record.setFieldValue(deed, 'Yes');
    
    // record.setFieldValues({[deed]: 'Yes',[n_as_witness]: 'Yes', [number_of_witnesses]: '1', [public_form]: 'No'});
    
    document.saveAllChanges

    produces no errors but also does not set the fields to the defined values. I am sure there is something basic I am missing

    Also is the syntax for record.setFieldValues correct?

    The sample database is attached.

    Help would be gratefully received.

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

    In your example you have a list of folders and inside a couple of files. If those files are consistent (e.g. folder.jpg and Info.txt) then you can just assume they exist and work from there. So I’d just do ls * > files.txt to get the list of folders you have. It’ll look like your directory listing and then you can compose the paths inside of it.

    I removed the de-duplication logic to simplify so multiple runs will create duplicate entries. If this works, you can grab the Script Manager and use that to add it back (you might want to add a hidden key field as well).

    // init field ID variables
    var thumbnail_id = 'fld-changeme';
    var title_id = 'fld-changeme';
    var author_id = 'fld-changeme';
    var description_id = 'fld-changeme';
    
    function Import_Entries() {
    	// prefix is the path to where the root directory is (also link this via Script Folder Access)
    	let prefix = "file:///Users/youruser/Documents/x/";
    
    	// load up the list of the files 
    	let indexFile = Utils.getTextFromUrl(prefix + "files.txt");
    	
    	// check if we got content or not.
    	if (!indexFile) {
    		console.log("No index file?");
    		return
    	}
    	
    	// split it up line by line and process it
    	for (let sourceFile of indexFile.split("\n")) {
    		// write out the file we're processing in case something goes wrong
    		console.log(sourceFile);
    		// default the title to be the filename and author empty
    		let title = sourceFile;
    		let author = "";
    
    		// use a regexp to look for square brackets
    		let pieces = sourceFile.match(/(.*)(\[([^\]]*)\])*/);
    		if (pieces) {
    			// if we found two parts, set title/author
    			title = pieces[1];
    			author = pieces[2];
    		}
    		
    		// create a new record in this form 
    		let targetRecord = form.addNewRecord();
    
    		// set the title and author fields
    		targetRecord.setFieldValue(title_id, title);
    		targetRecord.setFieldValue(author_id, author);
    
    		// read the Info.txt file into the description note field
    		targetRecord.setFieldValue(description_id, Utils.getTextFromUrl(prefix + sourceFile + "/Info.txt"));
    
    		// add the folder.jpg file to the thumbnail field
    		targetRecord.addPhotoFromUrlToField(prefix + sourceFile + "/folder.jpg", thumbnail_id);
    	}
    	document.saveAllChanges();
    }
    
    Import_Entries();
    

    That should come close to what you need, you’ll need to change the value of the variables at the top to match your field ID’s. You can use the ID button in the script editor on the fields and it’ll generate a line for you with the field ID in it you can use. I’m not sure if the image import works from disk as I’ve not tried it but I think it should work (and if it doesn’t we ask Brendan nicely to add it). Make sure you use Script Folder Access in the Tap Forms document preferences to link the root directory otherwise you’ll get permission errors. I haven’t run this so it’s not guaranteed to run but it should come close.

    I tested the regexp against this data based on the image, it should work but might need tweaks:

    Die Welt nach der Flut [Kassandra Montag]
    Die Welt ohne Strom - 01 - - One...ond After William R. Forstchen]
    Die Welt ohne Strom - 02 - One Year After [William R. Forstchen]
    Die Welt von Arven - Das Schwert der Ahnen [Raphael Sommer]
    Die Wikinger 01
    Wie Rollo, d...er, Herzog der Normandie wurde
    Die Wikinger - 02 Björn Einars..., der Abenteurer [Hans Paulisch]
    Die widen Waldhelden Kaninchen in Not [Andrea Schütze]
    Die Wolf-Gäng - Das Hörspiel zum Kinofilm [Wolfgang Hohlbein]
    Die Wunderfabrik - 01 - Keiner...wissen! [Stefanie Gerstenberger]
    Die Wunderfabrik - 02 - Nehmt... in Acht! [Stefanie Gerstenberger]
    Die Wunderfrauen - 01 - Alles,...erz begehrt [Stephanie Schuster]
    Die zweite Braut [Sihulle Raillon]
    

    Getting the play time is again a little harder but not impossible. I’d probably use ffmpeg to grab it but that would require some stitching.

    MiB
    Participant

    Thank you very much for your detailed answer!
    As previously mentioned, I can’t code, so I can’t use your knowledge, flown into this script, to use it to build my own script.
    But what I have understood is this: I need a text file with all of my files I want to have in tapforms? With

    ls */ > files.txt

    all files are listed. (But of course ALL files. The ones which should not be in tapforms also.)

    And it seems, the duration is a bigger problem, so I delete this one from the wish list.
    I can easily import the title and author of as many files as I want by hand. So that would be no problem. But I guess it would be much harder to match the content of the info.txt file and the jpgs afterwards, when tapforms is already filled with the titles and the author.
    Therefore all Information should be imported at the same time, directory by directory. Right?

Viewing 15 results - 1,321 through 1,335 (of 2,952 total)