Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 08/06/2025 in Posts

  1. Absolutely. Please keep in mind that I am a complete AutoIt rookie. The examples show JSON code already in the example scripts as Global variables and parse/manipulate from there. The thing that would help a rookie like me the most would be to have an example script and an example .json file. The script would load the .json file from disk, parse json, modify some data and save those changes back to .json file on disk. That would have helped my rookie situation a lot. However, I also understand that some of that is outside of the responsibility for the UDF. But from an example perspective still would be beneficial. By the way, thank you all for your help. Cheers!
    3 points
  2. If you then want to write to the theme with _JSON_addChangeDelete() (which could make sense due to the nested structure), then you basically only need to determine the first free array index. Since you have an AutoIt data structure after _JSON_Parse(), you basically only need to make a Ubound to the themes array: #include "JSON.au3" ; your input JSON string Global $sJSONRAW = '{"workbench.startupEditor":"none","breadcrumbs.enabled":false,"window.controlsStyle":"custom","workbench.activityBar.location":"hidden","window.customTitleBarVisibility":"auto","window.titleBarStyle":"custom","workbench.colorCustomizations":{"editor.background":"#00000060","terminal.background":"#00000060"},"theme":"Existing Theme 1","themes":[{"name":"Existing Theme 1","tab":{"background":"#00000080","unfocusedBackground":"#00000000"},"tabRow":{"background":"#00000000","unfocusedBackground":"#00000000"},"window":{"applicationTheme":"light","useMica":true}},{"name":"Existing Theme 2","tab":{"background":"#00000080","unfocusedBackground":"#00000000"},"tabRow":{"background":"#00000000","unfocusedBackground":"#00000000"},"window":{"applicationTheme":"light","useMica":true}},{"name":"Existing Theme 3","tab":{"background":"#00000080","unfocusedBackground":"#00000000"},"tabRow":{"background":"#00000000","unfocusedBackground":"#00000000"},"window":{"applicationTheme":"light","useMica":true}},{"name":"Existing Theme 4","tab":{"background":"#00000080","unfocusedBackground":"#00000000"},"tabRow":{"background":"#00000000","unfocusedBackground":"#00000000"},"window":{"applicationTheme":"light","useMica":true}}]}' ; parse JSON string into nested AutoIt data structure: Global $vJSON = _JSON_Parse($sJSONRAW) If @error Then Exit MsgBox(0,"Erro", "Error " & @error & " during parsing the JSON string") ; print to console to check the structure of the input JSON string ConsoleWrite("------- Input --------- " & @CRLF & _JSON_Generate($vJSON) & @CRLF & @CRLF) ; determine the number of elements already in the array "themes" Global $iNewIdx = UBound($vJSON["themes"]) ; add the new theme at the first free index: _JSON_addChangeDelete($vJSON, "theme", "Default Theme Test") _JSON_addChangeDelete($vJSON, "themes[" & $iNewIdx & "].name", "Default Theme Test") _JSON_addChangeDelete($vJSON, "themes[" & $iNewIdx & "].tab.background", "#00000020") _JSON_addChangeDelete($vJSON, "themes[" & $iNewIdx & "].tab.unfocusedBackground", "#00000000") _JSON_addChangeDelete($vJSON, "themes[" & $iNewIdx & "].tabRow.background", "#00000000") _JSON_addChangeDelete($vJSON, "themes[" & $iNewIdx & "].tabRow.unfocusedBackground", "#00000000") _JSON_addChangeDelete($vJSON, "themes[" & $iNewIdx & "].window.applicationTheme", "dark") _JSON_addChangeDelete($vJSON, "themes[" & $iNewIdx & "].window.useMica", true) ; create JSON string out of the AutoIt datastructure and print it to the console ConsoleWrite("------- Output --------- " & @CRLF & _JSON_Generate($vJSON) & @CRLF & @CRLF) Exactly - after _JSON_Parse() the whole thing no longer has anything to do with JSON but you have completely normal AutoIt data structures. However, this data is heavily nested. In AutoIt you can easily read information from nested data structures. But changing them is extremely cumbersome. To simplify exactly this, there is the _JSON_addChangeDelete() which (contrary to its name) has nothing to do with JSON but takes care of nested AutoIt data structures. The "cleaner" way mentioned is therefore already the way that _JSON_addChangeDelete() takes, as it changes the data at AutoIt level - just with a simpler interface than building the nested array element manually. Btw:, here's a little trick: With structures as complex as your theme description, you don't have to add everything manually. If you already know the structure beforehand, then simply describe it directly in JSON, have the theme element created completely from it and add it as a whole. In other words, you can replace the entire _JSON_addChangeDelete() in the example with a single one (similar to the jq example above): _JSON_addChangeDelete($vJSON, "themes[" & $iNewIdx & "]", _JSON_Parse('{"name":"Default Theme Test","tab":{"background":"#00000020","unfocusedBackground":"#00000000"},"tabRow":{"background":"#00000000","unfocusedBackground":"#00000000"},"window":{"applicationTheme":"dark","useMica":true}}'))
    3 points
  3. Don't belittle yourself so much 😅 , your knowledge of AutoIt isn't bad at all. But I get your point @WildByDesign , yes. For beginners with JSON a proper example flow would be helpful. 🤝
    2 points
  4. Once it is parsed the array is the same as any other Array in AutoIt , when you're done with modifying the array you can simply pass it as the value to your favourite JSON function without any additional steps
    2 points
  5. Here one way : ; From Nine #include <GDIPlus.au3> #include <GUIConstants.au3> Opt("MustDeclareVars", True) Example() Func Example() _GDIPlus_Startup() Local $hGUI = GUICreate("Example", 400, 400) GUISetState() Local $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI) _GDIPlus_GraphicsClear($hGraphic, 0xFFFFFFFF) Local $hBrush = _GDIPlus_LineBrushCreate(50, 200, 320, 200, 0xFF000000, 0xFFFFFFFF) Local $hFormat = _GDIPlus_StringFormatCreate() Local $hFamily = _GDIPlus_FontFamilyCreate("Arial") Local $hFont = _GDIPlus_FontCreate($hFamily, 28, 2) Local $tLayout = _GDIPlus_RectFCreate(80, 100, 320, 40) Local $aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, "AutoIt Rulez !", $hFont, $tLayout, $hFormat) _GDIPlus_GraphicsDrawStringEx($hGraphic, "AutoIt Rulez !", $hFont, $aInfo[0], $hFormat, $hBrush) Do Until GUIGetMsg() = $GUI_EVENT_CLOSE _GDIPlus_StringFormatDispose($hFormat) _GDIPlus_FontFamilyDispose($hFamily) _GDIPlus_FontDispose($hFont) _GDIPlus_BrushDispose($hBrush) _GDIPlus_GraphicsDispose($hGraphic) _GDIPlus_Shutdown() EndFunc ;==>Example
    2 points
  6. Yes, you can't know everything from the start. Either you assume that a certain functionality already exists - then you explicitly search for it. Or you implement the functionality yourself. You have done the latter and that shows that you have understood what is necessary and how to implement it. I don't care in principle. However, I think it takes me longer with Github to see that something is there. In addition, only problems and feature requests should really be posted there and not a general discussion about how to deal with the UDF. Ok - I just saw that the JSON-Test.au3 is still in German. I could adapt that at the same time. But I still don't quite know what is meant by "Write in the JSON file". How to create the JSON string is already shown in the example. Basically, all you need is a FileWrite and you're done. So perhaps I haven't quite understood what exactly needs to be added.
    1 point
  7. Like @argumentum suggested: In case we (you @WildByDesign) have more questions, let's create a new thread in the forum help section which references to this thread. Therefore the overview about the UDF will increase, at least in my understanding. Just to understand it: @AspirinJunkie do you prefer conversation about the UDF (library) here (in the forum as new threads) or as GitHub issues? Best regards Sven
    1 point
  8. This Ubound example is really nice. Short, sweet and concise. I ended up writing a Do...Until loop lastnight that is about 12 lines that does exactly what your single Ubound line does. Sometimes I learn and figure things out the hard way. However, I am definitely switching to your single line method now. Now this is magical. It always amazed me, here in the AutoIt forums, how there always seems to be an almost unlimited amount of ways to achieve the same end goal. Thank you for enlightening me with several methods and for taking time out of your day to share your knowledge. It was helped tremendously.
    1 point
  9. I would agree with this. For AutoIt/JSON beginners like myself, this was exactly the first thing that I was looking for in an example from the various JSON UDFs. I don't think any of them had an example of a simple load/parse/modify/save JSON. I think that an example would certainly be beneficial.
    1 point
  10. Hi @AspirinJunkie 👋 , I would suggest to extend the JSON-Test.au3 by a example of how to write in the json file. If so, also adjust the README with this example. I cannot promise it, but I would add a PR (pull request) for that. But not in the next days - too much workload at the moment. What do you think? Is it worth it? Best regards Sven
    1 point
  11. ioa747

    _SectionsArrays

    _SectionsArrays A library for reading, writing, and managing 1D or 2D arrays, stored in a single text file using a section-based format. It provides functions to easily handle data, update existing sections, or add new ones. ; ; https://www.autoitscript.com/forum/topic/213059-_sectionsarrays/ ;--------------------------------------------------------------------------------------- ; Title...........: _SectionsArrays ; Description.....: A library for reading, writing, and managing 1D or 2D arrays, ; stored in a single text file using a section-based format. ; It provides functions to easily handle data, update existing sections, or add new ones. ; AutoIt Version..: 3.3.16.1 Author: ioa747 Script Version: 0.3 ; Note............: Testet in Win10 22H2 ;--------------------------------------------------------------------------------------- #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 #include <Array.au3> #include <GUIConstantsEx.au3> #include <GuiListView.au3> #include <WindowsConstants.au3> Example() Func Example() Local $sArraysFilepath = @ScriptDir & "\arrays.txt" If Not FileExists($sArraysFilepath) Then _MakeArrays($sArraysFilepath) If @error Then ConsoleWrite("_MakeArrays @error=" & @error & @CRLF) Local $aSections = _ArraysFromFile($sArraysFilepath) If @error Then ConsoleWrite("_ArraysFromFile @error=" & @error & @CRLF) ShellExecute($sArraysFilepath) Sleep(500) _ArraysDisplay($aSections, "Array Sections") EndFunc ;==>Example Func _MakeArrays($sFilePath) ; Make examples arrays ; Prepare Sections array Local $aSections[1][2] $aSections[0][0] = 0 Local $Info ; "Monthly zoo Report" array Local $Array[][] = [ _ ["Month", "Bears", "Dolphins", "Whales"], _ ["jan", 8, 150, 80], _ ["feb", 54, 77, 54], _ ["mar", 93, 32, 10], _ ["apr", 116, 11, 76], _ ["may", 137, 6, 93], _ ["jun", 184, 1, 72]] ; Add array to Sections array $Info = _AddArrayToArrays($aSections, "Monthly zoo Report", $Array) ConsoleWrite("add $Info=" & $Info & @CRLF) ; "Base Items" array Local $aArray_Base[2][2] = [["Item 0 - 0", "Item 0 - 1"], ["Item 1 - 0", "Item 1 - 1"]] ; Add array to Sections array $Info = _AddArrayToArrays($aSections, "Base Items", $aArray_Base) ConsoleWrite("add $aArray_Base=" & $Info & @CRLF) ; "item Index" array Local $aIndex[10] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] ; Add array to Sections array $Info = _AddArrayToArrays($aSections, "item Index", $aIndex) ConsoleWrite("add $aIndex=" & $Info & @CRLF) ; "item Index2" array Local $aIndex2[1][10] = [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]] $Info = _AddArrayToArrays($aSections, "item Index2", $aIndex2) ConsoleWrite("add $aIndex2=" & $Info & @CRLF) ; Save arrays to file _ArraysToFile($sFilePath, $aSections) EndFunc ;==>_MakeArrays ;-------------------------------------------------------------------------------------------------------------------------------- ; Help functions (A library for reading, writing, and managing 1D or 2D arrays, stored in a single text file using a section-based format.) ;-------------------------------------------------------------------------------------------------------------------------------- Func _ArraysDisplay(ByRef $aItems, $sTitle = "") ; Creates a GUI window to display and browse the contents of multiple arrays. Local $hGUI = GUICreate($sTitle, 300, 200, -1, -1, $WS_OVERLAPPEDWINDOW) Local $idListview = GUICtrlCreateListView("Sections | Arrays", 0, 0, 300, 200, $LVS_SINGLESEL) GUICtrlSetFont(-1, 10) _GUICtrlListView_SetExtendedListViewStyle($idListview, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT)) _GUICtrlListView_SetColumnWidth($idListview, 0, 170) _GUICtrlListView_SetColumnWidth($idListview, 1, 120) GUISetState(@SW_SHOW) Local $aIdItems[UBound($aItems, 1)] If @error Then Return SetError(@error, 0, 0) $aIdItems[0] = $aItems[0][0] For $i = 1 To $aIdItems[0] Local $sLabel = "error" Local $aArray = $aItems[$i][1] If IsArray($aArray) Then Local $iDimension = UBound($aArray, $UBOUND_DIMENSIONS) ; The dimension of the array e.g. 1/2/3 dimensional. Local $sDim = "" For $d = 1 To $iDimension $sDim &= "[" & UBound($aArray, $d) & "]" Next $sLabel = "Array" & $sDim EndIf $aIdItems[$i] = GUICtrlCreateListViewItem($aItems[$i][0] & " | {" & $sLabel & "}", $idListview) Next Local $iMsg While 1 $iMsg = GUIGetMsg() Switch $iMsg Case $GUI_EVENT_CLOSE ExitLoop Case $GUI_EVENT_RESIZED Local $iWH = WinGetClientSize($hGUI) GUICtrlSetPos($idListview, 0, 0, $iWH[0], $iWH[1]) Case $aIdItems[1] To $aIdItems[$aIdItems[0]] Local $id = $iMsg - $aIdItems[1] + 1 $aArray = $aItems[$id][1] _ArrayDisplay($aArray, $aItems[$id][0]) EndSwitch WEnd GUIDelete() EndFunc ;==>_ArraysDisplay ;-------------------------------------------------------------------------------------------------------------------------------- Func _AddArrayToArrays(ByRef $aArrays, $sName, $aNewArray) ; Adds a new array to the main array structure, or updates an existing one. Local $iCurrentSections = $aArrays[0][0] ; Check if the section name already exists For $i = 1 To $iCurrentSections If $aArrays[$i][0] = $sName Then ; Section exists, update the array $aArrays[$i][1] = $aNewArray Return True EndIf Next ; Section does not exist, add a new one ReDim $aArrays[$iCurrentSections + 2][2] ; Add the new section name and the array $aArrays[$iCurrentSections + 1][0] = $sName $aArrays[$iCurrentSections + 1][1] = $aNewArray ; Update the total number of sections count $aArrays[0][0] = $iCurrentSections + 1 Return True EndFunc ;==>_AddArrayToArrays ;-------------------------------------------------------------------------------------------------------------------------------- Func _ArraysToFile($sFilePath, $aArrays, $sDelim_Col = "|") ; Saves a multi-array structure back to a text file. Local $sOutput = "" Local $iNumSections = $aArrays[0][0] ; Loop through each section For $i = 1 To $iNumSections Local $sSectionName = $aArrays[$i][0] Local $aCurrentArray = $aArrays[$i][1] ; Get the array dimension Local $iDimension = UBound($aCurrentArray, $UBOUND_DIMENSIONS) ; Append the section name with the dimension to the output string $sOutput &= "[" & $sSectionName & "]" & $iDimension & "D" & @CRLF ; Convert the array to a string Local $sArrayString = _ArrayToString($aCurrentArray, $sDelim_Col) ; Append the array string to the output, followed by a newline $sOutput &= $sArrayString & @CRLF & @CRLF Next Local $hFile = FileOpen($sFilePath, $FO_OVERWRITE + $FO_UTF8_NOBOM) If $hFile = -1 Then Return SetError(1, 0, False) EndIf FileWrite($hFile, $sOutput) FileClose($hFile) Return True EndFunc ;==>_ArraysToFile ;-------------------------------------------------------------------------------------------------------------------------------- Func _RemoveArraySection(ByRef $aArrays, $sName) ; Removes a section and its corresponding array from the main array structure. Local $iCurrentSections = $aArrays[0][0] ; Check if the section name exists For $i = 1 To $iCurrentSections If $aArrays[$i][0] = $sName Then _ArrayDelete($aArrays, $i) $aArrays[0][0] = $iCurrentSections - 1 ReDim $aArrays[$iCurrentSections][2] Return True EndIf Next Return False ; Section not found EndFunc ;==>_RemoveArraySection ;-------------------------------------------------------------------------------------------------------------------------------- Func _ArraysFromFile($sFilePath, $sDelim_Col = "|") ; Reads a text file containing multiple sections and converts them into a 2D array structure. Local $sFileTxt = FileRead($sFilePath) If @error Then Return SetError(1, 0, "") ; Put a @CRLF in start, and convert all @LF, @CR to @CRLF $sFileTxt = @CRLF & StringRegExpReplace($sFileTxt, "(\r\n|\n)", @CRLF) ; split String in (@CRLF & "[") Local $aPart = StringSplit($sFileTxt, @CRLF & "[", 1) If $aPart[0] < 2 Then Return SetError(2, 0, "") Local $aSections[$aPart[0]][2] $aSections[0][0] = $aPart[0] - 1 For $i = 2 To $aPart[0] ; normal first line is the (name & ]) Local $iPos = StringInStr($aPart[$i], "]", 0, -1) If Not $iPos > 0 Or @error Then Return SetError(3, 0, "") ; find the name and the Dimension Local $sName = StringLeft($aPart[$i], $iPos - 1) Local $iDimension = 0 + Number(StringMid($aPart[$i], $iPos + 1, 1)) If Not ($iDimension = 1 Or $iDimension = 2) Then Return SetError(4, 0, "") ; remove leading CRLF or LF from front & back Local $sString = StringRegExpReplace(StringTrimLeft($aPart[$i], $iPos + 2), '(^[\r\n]+|[\r\n]+$)', '') Local $aArrayFromText ; $iDimension - 1 to $bForce2D => true $aArrayFromText = _ArrayFromString($sString, $sDelim_Col, Default, $iDimension - 1) $aSections[$i - 1][0] = $sName $aSections[$i - 1][1] = $aArrayFromText Next Return $aSections EndFunc ;==>_ArraysFromFile ;-------------------------------------------------------------------------------------------------------------------------------- Please, every comment is appreciated! leave your comments and experiences here! Thank you very much
    1 point
  12. ioa747

    _SectionsArrays

    and imagine that I tried it, and it didn't work. But now with the certainty that you said it, and with a second glance it cleared up, and it worked Thank you very much for the support. update to version: 0.3
    1 point
  13. argumentum

    _SectionsArrays

    #include <Array.au3> _ArrayFromString ( $sArrayStr [, $sDelim_Col = "|" [, $sDelim_Row = @CRLF [, $bForce2D = False [, $iStripWS = $STR_STRIPLEADING + $STR_STRIPTRAILING]]]] ) $bForce2D [optional] default is False. True will force a 2 dimensional array even if 1 dimensional. If it does not work as described, let me know to fix it.
    1 point
  14. ioa747

    _SectionsArrays

    I took option 2 [my array ]1D or [my array ]2D I couldn't trick _ArrayFromString into creating a 2D array with one row and had to create a new function. Thank you very much for the suggestion and the enlightenment. update to version: 0.2
    1 point
  15. argumentum

    _SectionsArrays

    ;Local $aIndex[10] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Local $aIndex[1][10] = [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]] It'd be nice to do something to differentiate between 1D and 2D. Tho this is the only scene where it would not conform to recreate the original array 🤔 Thanks for sharing Edit: ; split String in (@CRLF & @CRLF & "{") ; for 1D ? ; split String in (@CRLF & @CRLF & "[") ; for 2D ?, and yes I added a 2nd @CRLF Edit2: or, add what it is to it, like: [my array 1D] or [my array 2D] just as an internal indicator ?
    1 point
×
×
  • Create New...