Script to Add File Alias?

Viewing 11 reply threads
  • Author
    Posts
  • March 8, 2022 at 2:52 AM #46959

    Ricard Casals
    Participant

    First of all, thank you for this excellent program.
    I have a form with a lot of records: 10 years, day by day, one photo per day. At the moment I prefer that the images are outside of Tap Forms, and now they are, as an alias, in a “web-website” field.
    I’m new to Javascript and I’m trying – probably wrongly – to copy the link in the “web-website” field to a “File-Attachment” field, with this script, trying in 2 ways:


    function CC1 () {
    // Get image-link-from-web-field
    let image_id = 'fld-8f9c8612cbc04ebfb422576213cebf74';
    let image_link = record.getFieldValue (image_id);

    // way A - Copy to file-attach-field-as an alias
    record.addFileFromUrlToField (image_link, 'fld-05f12a9a7da74079b354a02e7c5bb305');

    // way B - Copy to file-attach-field-as an alias
    //record.setFieldValue('fld-05f12a9a7da74079b354a02e7c5bb305 ', image_link);

    form.saveAllChanges ();
    }
    CC1 ();

    What am I trying to do is possible? Or should it be done manually by clicking “Add File Alias”icon?

    Thanks a lot! Ricard Casals

    March 8, 2022 at 8:36 AM #46960

    Daniel Leu
    Participant

    The first approach is the way to go. But to attach a file as an alias, you need one additional parameter:

    record.addFileFromUrlToField(url, file_field_id, {"alias" : true});

    • This reply was modified 2 years, 1 month ago by Daniel Leu.
    March 8, 2022 at 9:38 AM #46962

    Ricard Casals
    Participant

    Thanks, Daniel, for this quick response. But I tried the proposed code and it doesn’t work. Is it possible that I wrote something wrong?

    record.addFileFromUrlToField (image_link, 'fld-05f12a9a7da74079b354a02e7c5bb305', {"alias": true});

    March 8, 2022 at 11:07 AM #46964

    Daniel Leu
    Participant

    I just tried it myself. I can add a file but as soon as I add the alias parameter, the file alias is not added :(

    Something for Brendan to look at.

    March 8, 2022 at 11:09 AM #46965

    Ricard Casals
    Participant

    Thanks, Daniel!

    March 10, 2022 at 3:57 AM #46975

    Ricard Casals
    Participant

    This is a very simple (and primitiv?) Applescript to attempt to add a single photo alias to a ‘File Attachment’ field on a single record.
    The next step will be an automated ‘repeat with’ from a list of photos to a bunch of records in Tap Forms.
    Waiting for a better and cleaner solution with an script in Tap Forms.
    With this code, it works to assign a single alias to a single field. It’s a small thing, yes, just to keep going and learning!


    -- 220310B - 'TF_AS3' (WIP)
    -- NOTE: the 'POSIX file' depends on your own photo folder path.
    -- NOTE: the value of 'row 4' (in the first 'click button' line) depens on the 'File Attachment' field position.
    -- NOTE: the parameters of 'click text field…' line and 'click button 3 of sheet…' depends on the own photos path, obviously.
    set f to POSIX file "/Users/ricardcasalsalexandri/Desktop/fotos-test-2/19770101.JPG"
    set sourceFile to f
    tell application "Finder"
    activate application "Tap Forms 5"
    tell application "System Events" to tell process "Tap Forms 5"
    click button 4 of UI element 2 of row 4 of table 1 of scroll area 2 of splitter group 1 of splitter group 1 of window 1
    click text field 1 of UI element 1 of row 2 of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of sheet 1 of window 1
    click button 3 of sheet 1 of window 1
    end tell
    end tell

    (I tried to attach the script – also as a zip – but I can’t figure it out)

    March 10, 2022 at 8:46 AM #46976

    Daniel Leu
    Participant

    Wow, this is scary….

    >click button 4 of UI element 2 of row 4 of table 1 of scroll area 2 of splitter group 1 of splitter group 1 of window 1
    >click text field 1 of UI element 1 of row 2 of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of sheet 1 of window 1
    >click button 3 of sheet 1 of window 1

    I would not want to maintain this, at least with your comments it is a bit easier! It is great that you have found a way to automate attaching a file as an alias.

    Once the record.addFileFromUrlToField() issue is fixed, you can call the Tapforms’ script interface using tapformz://script/db-xxxx/frm-xxxx/Test+Script?option1=A&option2=B.

    March 10, 2022 at 8:57 AM #46977

    Ricard Casals
    Participant

    Daniel, yes, you’re right, this script is too inelegant and ineffective to be useful to anyone but me. I’ve shown it almost as a curiosity while we wait for the right way to do it.

    Other than that, thanks again!

    March 18, 2022 at 10:39 PM #46995

    Brendan
    Keymaster

    So Daniel emailed me about this thread. Sorry I missed it before.

    At first I thought, damn, my code isn’t working.

    But then when I dug into it more, it turns out it is working.

    There are a couple of things you need to do though.

    1. Make sure the source files you want to make aliases for are accessible to the Scripting engine by setting the Script Folder on the Preferences window. Without this, Tap Forms won’t be able to read the file for it to make a bookmark/alias for.

    2. Make sure you URL encode the URL string you’re trying to make an alias for. The only problem with this is you’ll get the encoding in the filename on the File Attachments field.

    I’ve just added code to URL encode the provided URL and that avoids that problem. But either will work then.

    Here’s a sample that worked for me in the existing version:

    function Add_Alias() {
    
    	var file_attachment_id = 'fld-36eea6e880d14959b2e002b8f9b060b8';
    	var url = "file:///Users/brendan/Desktop/All%20Wines.csv";
    	record.addFileFromUrlToField(url, file_attachment_id, {"alias" : true});
    	document.saveAllChanges();
    }
    
    Add_Alias();

    and when I release the next update, this will work:

    function Add_Alias() {
    
    	var file_attachment_id = 'fld-36eea6e880d14959b2e002b8f9b060b8';
    	var url = "file:///Users/brendan/Desktop/All Wines.csv";
    	record.addFileFromUrlToField(url, file_attachment_id, {"alias" : true});
    	document.saveAllChanges();
    }
    
    Add_Alias();

    Only the All Wines.csv file is different. But if your files don’t have any spaces or special characters in them, then you should be fine as is.

    Thanks,

    Brendan

    March 19, 2022 at 1:44 AM #47004

    Ricard Casals
    Participant

    Brendan, I reviewed the script I had and the file’s url and now everything works! – Something a small thing that wasn’t ok: a space in the url?, not enabled TF5 script folder preference? I have to be very careful about these little details in the code and the data it carries!
    Thanks again for your patience and dedication!
    By the way, the last line “document.saveAllChanges ();” is equivalent to “form.saveAllChanges ();”?
    Ricard

    March 19, 2022 at 2:44 AM #47005

    Brendan
    Keymaster

    I’m glad you got it working. Yes, sometimes the devil is in the details. But I should write that down somewhere in the manual.

    Yes, the saveAllChanges() function is the same no matter how you call it. I know, it’s redundant.

    March 19, 2022 at 9:54 AM #47007

    Daniel Leu
    Participant

    Ah…. now I found why it didn’t work for me: My filename I used in my test had umlauts (eg, ö).

    Just an observation: the same file can be attached only ones, but there is no limit/check for aliases.

Viewing 11 reply threads

You must be logged in to reply to this topic.