0

How can a make a .txt file to save user input data after each use? I would like to save or store the "width" and "height" var until the next input.

        var doc = app.activeDocument;
        var panel = new Window("dialog", "test", undefined, { borderless: false });

        var row1 = panel.add ("group");
        row1.add ('statictext {text: "width", characters: 8, justify: "left"}');
        var width = row1.add("edittext",undefined, "100");
        width.characters = 10;
       
        var row2 = panel.add ("group");
        row2.add ('statictext {text: "height", characters: 8, justify: "left"}');
        var height = row2.add("edittext",undefined,"200");
        height.characters = 10;
        

        var row3 = panel.add ("group");
        row3.add("button", undefined, "OK");
        row3.add("button", undefined, "Cancel");
        panel.show();


        x = Number(width.text + " "); 
        i = Number(height.text + " ");
        
        var layer = doc.layers[0];
        var rectangle;
        rectangle = layer.pathItems.rectangle(0, 0, x, i); 

3
  • I don't know if there are better ways to store persistent data these days, but a text file certainly works. Here's some example code for reading and writing a text file. If you want to write more complex data than just 2 values, you might want convert a javascript object to JSON using JSON.stringify( myVar ) and then JSON.parse( text ). JSON extension is .json. I think you still need to manually add JSON to your project to use the mentioned JSON methods, but I'm not sure if that's changed in the newer CC versions.
    – Joonas
    Oct 11, 2021 at 8:08
  • You can include this in your script to use the JSON methods. Or you can write comma separated text and parse it yourself, like in this case you could save the text file contents as: width,height122,300 and read text.split(','). — This might not be necessary looking at your dialog, but perhaps it's useful in the future... I have made a ScriptUI dialog builder that basically tries to solve the issue of designing the dialog and it also exports working javascript code for SUI.
    – Joonas
    Oct 11, 2021 at 8:24
  • @Joonas you can save the variables in illustrator settings Like app.preferences.setStringPreference('yourscript/subvalue',"data");
    – joojaa
    Oct 11, 2021 at 9:12

1 Answer 1

1

You can store and retrieve stuff from illustrator settings with:

app.preferences.setStringPreference('yournamespace/subvalue/subvalue', "text");  

And read it with

app.preferences.getStringPreference('yournamespace/subvalue/subvalue')

The database is a key value storage. You can also read settings for other things like snapping or gridspacing.

A working example script that does storage and updating of the data can be found here, yes I am the script author.

1
  • Thanks for reply the "jooGraphFunction,", is too advanced for me can you exemplify on my script to better understand?
    – Martha
    Oct 11, 2021 at 10:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.