Programatically or automatically import

Tap Forms – Organizer Database App for Mac, iPhone, and iPad Forums Script Talk Programatically or automatically import

Tagged: ,

Viewing 5 reply threads
  • Author
    Posts
  • August 8, 2021 at 4:18 AM #44934

    Tom Kerswill
    Participant

    Hi

    Is it possible to import data programatically? Ideally by using an existing import preset?

    I’d like to have something like Hazel or Keyboard Maestro trigger this when data in a file changes, so that I can reimport it into Tap Forms.

    I suspect I could probably do a form script that might do this; but ideally it would be able to trigger Tap Forms’ existing import system, as it can then do things like automatically setting links to other forms, etc., whereas I suspect this would be quite complex to do if building an import script from scratch.

    Thanks in advance,

    Tom

    August 9, 2021 at 8:49 AM #44938

    Daniel Leu
    Participant

    I don’t see an API for that. In one of my applications, I use the clipboard to import data into TF. It took a bit of time to get the data parsing right, but now it works well.

    If your data is already in CSV format, the parsing should be easy. I don’t think that such a script is that difficult since you already know where the data should go. You then call the script with tapformz://script/[document ID]/[form ID]/[form script name]?key1=value1&key2=value2...

    August 14, 2021 at 1:00 AM #44967

    Brendan
    Keymaster

    And with setting the Script Folder on the Preferences screen, you can put your data in there and then use var data = Utils.getTextFromUrl(url); command to read in the file. Then parse it. The split() function could be used to split the lines into an array of lines. Then for each line, use split again to split by the column delimiter. But it really depends on how your data is structured. If you have delimiters within your content and quoted content, then that would be much more difficult to deal with. I should add a JavaScript API for parsing delimited content into two dimensional arrays of data, basically giving you access to the code I already use for parsing CSV files.

    August 14, 2021 at 9:44 AM #44968

    Sam Moffatt
    Participant

    An API to be able to do what the import system does would be good, perhaps not just a CSV parser (though that would be cool) but access to the same import logic would also enable image import and automatic linking to happen as well.

    August 17, 2021 at 11:29 PM #45006

    Tom Kerswill
    Participant

    Thanks so much for the help. I ended up writing something based on your great Script Manager, Sam, and then building out something to automatically add the fields (if they’re not already there).

    It works really well. Hooking into the existing CSV / Json import would be lovely; but doing it with a script isn’t too bad in the end.

    Here’s the snippet:

    		let targetRecord = 	getRecordFromFormWithKey(form.name,indexKeyId, rowId);
    		if (!targetRecord) {
    			console.log("Adding new record...");
    			targetRecord = form.addNewRecord();
    		}
    		console.log("Record: " + targetRecord.getId());
    		for (const [key, value] of Object.entries(row)) {  			
    let keyField = form.getFieldNamed(key);
    			if (!keyField) {				
    				console.log("Adding field: " + key + "Type: " + typeof(value));
    				if (typeof(value) == "number") {
    				fieldType = "number";
    				}
    				else {
    				fieldType = "text"
    				}
    				keyField = form.addNewFieldNamedWithType(key,fieldType);
    				form.saveAllChanges();
    			}
    			keyId = keyField.getId();
      			console.log("Key: " + key + " keyId: " + keyId + " Value: " + value);
      			targetRecord.setFieldValue(keyId, value);
    		}
    

    Tom

    August 18, 2021 at 3:18 PM #45012

    Sam Moffatt
    Participant

    Great to hear you got it to work and that the Script Manager stuff was helpful for you as well :)

Viewing 5 reply threads

You must be logged in to reply to this topic.