Skip to main content
Adirai Maji
Inspiring
December 26, 2020
Answered

[Scripting] Is Settings Object limited don't take array as a value?

  • December 26, 2020
  • 1 reply
  • 712 views

I just tried to insert an array in setting Object when saving settings. That doesn't make any error. But when I tried get the settings it just returning the string "Array".

 

This is what I inserted,

app.settings.saveSetting("TestSettingsObject", "ArrayOfValues", ["valueOne", "valueTwo",true]);

 

This is what I get when I tried get the values in settings

var values = app.settings.getSetting("TestSettingsObject", "ArrayOfValues");

alert(values);

 

It just alerted "Array" instead of actual array values. What Am I doing wrong? or Settings Object won't just take arrays?

 

This topic has been closed for replies.
Correct answer Paul Tuersley

It just saves a single string or value, and getSettings just returns a string so even if you were saving a value you'd have to parse that string back into a value after reading.

If you did want to save an actual array you could just use myArray.join() which would turn it into a string like "1,2" then after reading the pref you'd have to split it into an array again like myArray = mySetting.split(",") then loop through it with something like myArray[x] = parseFloat(myArray[x]) to convert it back into a value.

 

 

1 reply

Paul TuersleyCorrect answer
Inspiring
December 26, 2020

It just saves a single string or value, and getSettings just returns a string so even if you were saving a value you'd have to parse that string back into a value after reading.

If you did want to save an actual array you could just use myArray.join() which would turn it into a string like "1,2" then after reading the pref you'd have to split it into an array again like myArray = mySetting.split(",") then loop through it with something like myArray[x] = parseFloat(myArray[x]) to convert it back into a value.

 

 

Mathias Moehl
Community Expert
Community Expert
December 28, 2020

Just note that you can run into issues, if the elements of your array are strings containing "," symbols.

So if woul write ["this,is","a test"] to the settings, you will get ["this","is","a test"] as result.

A more robust approach would be to convert your data to json strings.


convert array to string:
JSON.stringify(myArray)

 

convert string back to array:
JSON.parse(myString)

This also works with any other data that can be converted to json (which means effectively anything except object that contain functions).

 

Note that JSON is not available in After Effects scripts out of the box. Here is a tutorial how to use json in Ae scripts

https://aescripts.com/learn/After-Effects-Scripting-JSON-Tutorial/

 

Mathias Möhl - Developer of tools like BeatEdit and Automation Blocks for Premiere Pro and After Effects
Adirai Maji
Inspiring
December 29, 2020

Wonderful...! I knew this is gonna be a trouble If I pass the string which contain comma(,) in it.But I never had an Idea how to come accross this until I see your note on this. This is really brilliant way of handling the situation. Thanks @Mathias Moehl