record.addFileFromUrlToField() and use of wild cards or Regex in filename

Tap Forms – Organizer Database App for Mac, iPhone, and iPad Forums Script Talk record.addFileFromUrlToField() and use of wild cards or Regex in filename

Viewing 3 reply threads
  • Author
    Posts
  • May 10, 2022 at 4:08 AM #47251

    Victor Warner
    Participant

    I wish to use the record.addFileFromUrlToField function to add an attachment to a File Attachment type field.

    The files are all named in a consistent way: year + job number + “invoice – invoice number” but then the invoice number various.

    An example:

    If the file is called: “2022_046 – Smith Consular Service invoice – invoice number UK144720.pdf”

    I can form the first part of the filename from a Tap Forms field (“2022_046”) plus the text upto the invoice number. It is the end part of the file name which I cannot form (“UK144720” in the above example) as it changes from file to file.

    The script I have come up with at the moment is:

    function Add_Attachment() {
    var file_attachment_id = 'fld-1f7f08ca22de4884b5a7aa6b52a74251';
    var protocol_number_full_file_naming_id = 'fld-cdbcef5028c54e8591313e2bc5295d1a';
    var protocol_number = record.getFieldValue('fld-cdbcef5028c54e8591313e2bc5295d1a');
    
    var url = "file:///Users/victor/Desktop/" + protocol_number + " - Smith Consular Service invoice - invoice number [ ].pdf"
    
    console.log(url);
    
    	record.addFileFromUrlToField(url, file_attachment_id);
    	document.saveAllChanges();
    
    }
    
    Add_Attachment();

    It is the part in “[ ]” in

    var url = "file:///Users/victor/Desktop/" + protocol_number + " - Smith Consular Service invoice - invoice number [ ].pdf"

    with which I would like to known if it is possible to use a wildcard/regular expression.

    Any help would be gratefully received.

    • This topic was modified 2 years, 7 months ago by Brendan.
    May 11, 2022 at 11:23 PM #47282

    Brendan
    Keymaster

    You have to know the filename before you can add it to a File Attachment field with that code. If you don’t know the final filename, Tap Forms won’t be able to read it. It doesn’t support regular expressions for that.

    But if the invoice number varies, why not add an index that starts at the last filename that’s there and then increment it in a loop if you need to add more than one file at a time.

    May 13, 2022 at 8:11 AM #47294

    Victor Warner
    Participant

    Brendan,

    Thank you for the response. It has forced me to find a work around.

    I know it is possible with a File Attachment field to click on the Add File icon (+) and then add as many files as you wish. However this is not reachable by the menu or by the keyboard – only a mouse or trackpad will work.

    I post the following in case it is of use to someone else.

    The workaround provides:

    1. selecting the record in Tap Forms
    2. in Finder, selecting the files to be attached.
    3. running a Keyboard Maestro macro using the For Each action which:

    (a) copies the filename and path of each file selected to the system clipboard
    (b) runs the Tap Forms Form Script (via a short AppleScript script) on each filename with path copied ot the system clipboard

    This is has the benefit that it can be activated via a keystroke and there is no need to worry about the names of files being different.

    The final Tap Forms Form Script is as follows:

    `function Add_Alias() {

    var ClipBoardName = ”;

    var file_attachment_id = ‘fld-1f7f08ca22de4884b5a7aa6b52a74251’;

    var ClipBoardName = Utils.copyTextFromClipboard();

    var url = “file://” + ClipBoardName;

    console.log(url);

    record.addFileFromUrlToField(url, file_attachment_id);
    document.saveAllChanges();

    }

    Add_Alias();`

    The Keyboard Maestro script is attached as a PDF.

    Attachments:
    You must be logged in to view attached files.
    May 13, 2022 at 12:34 PM #47297

    Daniel Leu
    Participant

    Nice! Thank you for sharing!

Viewing 3 reply threads

You must be logged in to reply to this topic.