Tap Forms – Organizer Database App for Mac, iPhone, and iPad › Forums › Script Talk › Prompter not pausing execution
- This topic has 5 replies, 3 voices, and was last updated 2 weeks, 1 day ago by Peter Riley.
-
AuthorPosts
-
October 28, 2024 at 4:05 AM #51231
Peter RileyParticipantHi,
My movie media database takes the value of the IMDB id field (eg tt0433442) and does a lookup via the omdb API to retrieve that movie’s data – actors, director etc – and populate the record. That works fine but some of my older records are missing the id, in which case the lookup fails of course.
So I’ve added some code to my script that checks whether the id field is blank and if so calls a function that uses the standard Prompter script template and that asks for the id and populates the id field so the rest of the code won’t fail.
The prompt and setting of the id field value works OK, but I see from the console that the rest of the script is still running (and failing) even before I’ve input the id into the Prompter dialogue box and clicked ‘Go for it!’. Is that by design or am I missing something?
Thanks
October 28, 2024 at 6:22 PM #51236
BrendanKeymasterCan you please post your script or a template (.tff) that includes the script? Take out any API keys you might have in your code first.
The scripts do run all in one shot, but the Prompter does interrupt it, but it depends on how the script was written.
October 29, 2024 at 9:21 AM #51238
Peter RileyParticipantThanks Brendan,
Template of the Movies form attached.
There are also forms for Actors, Directors and Writers. The way it works is that the IMDB ID is entered into the Movie form, then the script downloads the json from OMDB and populates the form with the movie data. If an Actor (for example) isn’t found in the Actor table then the name is added to that table, then the Actors, Director and Writers linked tables on the main form are also populated.
Hope that helps
P
Attachments:
You must be logged in to view attached files.October 29, 2024 at 10:57 AM #51240
Daniel LeuParticipantThe
Prompter
is not blocking meaning that the flow of the program is not interrupted. But you can wrap thePrompter
in aPromise
structure and then you get proper flow control.Here’s a simple example on how to do it:
var prompterVar; function textPrompter(title, text="") { return new Promise(function(resolve, reject) { let prompter = Prompter.new(); prompter.cancelButtonTitle = 'Cancel'; prompter.continueButtonTitle = 'Continue'; prompter.addParameter(text,'prompterVar') .show(title, ((status) => { if (status == false || prompterVar == undefined) { reject('Cancel'); } else { resolve(prompterVar); } })); }); } async function myFunction() { try { await textPrompter("Title", "Enter text") console.log(prompterVar + " entered"); } catch (errorText) { // user clicked 'No' console.log('cancelled'); return; } // user clicked 'yes' // continue with function // main code here console.log('continue from yes'); } myFunction();
October 29, 2024 at 11:18 AM #51241
Daniel LeuParticipantI don’t have the API, so I can’t verify, but otherwise, this seems to work.
The changes: I wrapped your main code in a function:
async function main(){....}
and in there, I check if the id exists, if not I use thePrompter
to get it.var apiKey = 'xxxxxxx';//var title_id = 'fld-a211cf1facf34470b4723b2b77e3f159';var genre_id = 'fld-f2fe50de9ca44bc6bee8887a6a1d1083';var IMDBrating_id = 'fld-e8c5f6890dce415396e3c5d6aa84a446';var runtime_id = 'fld-a917ded5c581468795cfcce40cb9f43b';var imdbid_id = 'fld-2b3bfd5a43ae46c1a3f059dbda9a3dd4';var year_id = 'fld-7f1cf965a9e04e7dae76e1f380f43367';var imdbsummary_id = 'fld-08a25e880aca4f7f89cf4cc268c55013';var poster_id = 'fld-688c78c4bd5148f0877e9a3f99799d5d';var graphic_id = 'fld-9e2d22d7b5d141678e741f41b2205fb5';var imdb_url_id = 'fld-2253e940bec14e7ca246db78109db488';var last_imdb_check_id = 'fld-d9b429bd674c4b5e8235844abaed9ba3';var checkdate = new Date();var imdb_url = ("https://www.imdb.com/title/" + record.getFieldValue(imdbid_id) + "/");// var imdb;var prompterVar;function textPrompter(title, text="") {return new Promise(function(resolve, reject) {let prompter = Prompter.new();prompter.cancelButtonTitle = 'Cancel';prompter.continueButtonTitle = 'Continue';prompter.addParameter(text,'prompterVar').show(title, ((status) => {if (status == false || prompterVar == undefined) {reject('Cancel');} else {resolve(prompterVar);}}));});}// Note: the following parameters are valid .addParameter(label, field_name, control type, values_list, default_value)function fetchMovieFromURL(){var movieID = imdbid_id;// var movieID = record.getFieldValue(imdbid_id);var fetchURL = 'https://www.omdbapi.com/?i=' + movieID + '&apikey=' + apiKey + '&r=json&plot=full';console.log('Requesting: ' + fetchURL);var movie_info = Utils.getJsonFromUrl(fetchURL);return movie_info;}async function main(){var movieID = record.getFieldValue(imdbid_id);if (!movieID) {try {await textPrompter("Enter the IMDB ID");console.log(prompterVar + " entered");const id = prompterVar;console.log("ID: " + id);record.setFieldValue(imdbid_id, id);form.saveAllChanges();// movieID = newID;// console.log(movieID);imdb_url = ("https://www.imdb.com/title/" + id + "/");} catch (errorText) {// user clicked 'No'console.log('Cancel button clicked');return "aborted by user";}}// var idValue = record.getFieldValue(imdbid_id);var results = fetchMovieFromURL();// if (results.totalItems) {// var movie_data = results[0];var movie_genre = (results["Genre"]);var movie_runtime = (results["Runtime"]);var movie_summary = (results["Plot"]);var movie_poster = (results["Poster"]);var movie_year = (results["Year"]);//var movie_title = (results["Title"]);var movie_IMDBrating = (results["imdbRating"]);var movie_stars = (results["Actors"]);var movie_writers = (results["Writer"]);var movie_director = (results["Director"]);record.setFieldValues({[genre_id]: movie_genre,[runtime_id]: movie_runtime,[imdbsummary_id]: movie_summary,[imdb_url_id]: imdb_url,[poster_id]: movie_poster,[year_id]: movie_year,[IMDBrating_id]: movie_IMDBrating,// [imdbid_id]: imdb,[last_imdb_check_id]: checkdate}, false);var poster = record.getFieldValue(graphic_id);if (poster <= "") {record.addPhotoFromUrlToField(movie_poster, graphic_id);}form.saveAllChanges;// Now process actor and director recordsvar actors_form = document.getFormNamed("Actors");var actor_records = document.getFormNamed("Actors").getRecords();var actor_name_id = 'fld-9e6772cd3ce64be1b0ba9f4bcaaeddd6';var actors_link_id = 'fld-dda27dcbe76447978890b3ad6be470d2';var actor_found = "N";// var writer_found = "N";let stars = movie_stars.split(', ');//loop to parse json results:for (let star of stars) {// console.log("**star** " + star);var candidateRecord = star;actor_found = "N";//now check if in actor form already, add if notfor (var index = 0, count = actor_records.length; index < count; index++) {var actor_name = actor_records[index].getFieldValue(actor_name_id);// console.log("actor_name " + actor_name);var actor_record_id = actor_records[index].getId(actor_name_id);var candidateField = actors_form.getRecordWithId(actor_record_id);// var currentRecord = movie_form.getRecords();if (actor_name == candidateRecord) { //ie, actor is already in actors form// console.log(actor_name === candidateRecord); // true or falseactor_found = "Y";console.log("Actor found - " + actor_name);// console.log("candidate_found should be Y " + candidate_found);// console.log("Yep - bye");// console.log("Existing actor found - adding to link table... "+ actor_name);// record.addRecordToField(candidateField, actors_link_id);// console.log("checking " + star + " in link table");// break;} else {actor_found = "N";} //if actor name} //for var index loopif (actor_found == "N") {console.log("New actor found - adding to main record... " + candidateRecord);var newRecord = actors_form.addNewRecord();newRecord.setFieldValue(actor_name_id, candidateRecord);// console.log("New actor found - adding to link table... " + candidateRecord);// record.addRecordToField(newRecord, actors_link_id);// form.saveAllChanges();} //if candidate foundconsole.log("Saving...");form.saveAllChanges();console.log("Existing actor found - updating link table... " + candidateRecord);record.addRecordToField(candidateField, actors_link_id);} //for let star of stars loopdocument.saveAllChanges();//....directorsvar directors_form = document.getFormNamed("Directors");var director_records = document.getFormNamed("Directors").getRecords();var director_name_id = 'fld-2132ad9eddc4412aaeed59e366946a3b';var directors_link_id = 'fld-e5ea9fce00e84ecd9d342f13840a10b4';var director_found = "N";let directors = movie_director.split(', ');//loop to parse json results:for (let director of directors) {var directorRecord = director;director_found = "N";for (var index = 0, count = director_records.length; index < count; index++) {var director_name = director_records[index].getFieldValue(director_name_id);var director_record_id = director_records[index].getId(director_name_id);var directorField = directors_form.getRecordWithId(director_record_id);if (director_name == directorRecord) { //ie, director is already in directors formdirector_found = "Y";console.log("Director found - " + director_name);// console.log("candidate_found should be Y " + candidate_found);// console.log("Yep - bye");// console.log("Existing director found - adding to link table... "+ director_name);// record.addRecordToField(directorField, directors_link_id);// console.log("checking " + star + " in link table");// break;} else {director_found = "N";} //if actor name} //for var index loopif (director_found == "N") {console.log("New director found - adding to main record... " + directorRecord);var newRecord = directors_form.addNewRecord();newRecord.setFieldValue(director_name_id, directorRecord);// console.log("New director found - adding to link table... " + directorRecord);// record.addRecordToField(newRecord, directors_link_id);// form.saveAllChanges();} //if director foundconsole.log("Saving...");form.saveAllChanges();console.log("Existing director found - adding to link table... " + directorRecord);record.addRecordToField(directorField, directors_link_id);} //for let star of stars loopdocument.saveAllChanges();}main();- This reply was modified 2 weeks, 2 days ago by Daniel Leu.
- This reply was modified 2 weeks, 2 days ago by Daniel Leu.
October 30, 2024 at 12:00 PM #51245
Peter RileyParticipantThanks Daniel – that works well. Much appreciated.
Before this I’ve been fiddling with this script for some time in this and another database document for movies I have watched (it is based on one demonstrated by Sam) but I have had to run it more than once to get the link tables updated, ie the first run gets the json from omdb.com and the console shows that the actors etc are found and suggests they are being added to the link table with this:
console.log(“Existing actor found – updating link table… ” + candidateRecord);
record.addRecordToField(candidateField, actors_link_id);
but the link tables remain blank and I have to run it again to actually populate the table.
Cheers
-
AuthorPosts
You must be logged in to reply to this topic.