Skip to content

4. Files

coffeeRequired edited this page Jun 20, 2024 · 4 revisions

SkJson

Pages - Files

Working with files is always a bit of a nuisance, let's see how we can work with files. The first thing to mention is that we already know the syntax for reading a file and >creating a json object from it, namely json from file "..." and what's next?

Creating a file

Suppose we have the path ./plugins/Script/Jsons/test.json but this file does not >exist. Let's create one

on script load:
   # without content & without check
   new json file "./plugins/Script/Jsons/test.json"

   # without content /w check
   if json file "./plugins/Script/Jsons/test.json" doesn't exist:
       new json file "./plugins/Script/Jsons/test.json"

   # with content /w check
   # Suppose we want to write some content
   set {_content} to json from "{'A': 'Test'}
   if json file "./plugins/Script/Jsons/test.json" doesn't exist:
       new json file "./plugins/Script/Jsons/test.json" with content {_content}

Writing

Consider that we have created a variable {-JSON} which contains json from some >loaded file

write {-JSON} to json file "./plugins/Script/Jsons/test.json"

Changes?

The changes to the dirrectly file are kind of meh, but let's show it. Suppose we have >a file /plugins/Script/Jsons/test.json and this json spits out like this {"data": >[{"username": "Franta"}]}, let's change "Franta"

on script load:

   # /w edit effect
   edit value "data[0]::username" of json file "/plugins/Script/Jsons/test.json" to >"Jakub"
   # Now the /plugins/Script/Jsons/test.json file will look like this
   # {"data": [{"username": "James"}]}

   # /wo edit effect
   set {_json} to json from file "/plugins/Script/Jsons/test.json"
   set value of json object "data[0]::username" in {_json} to "Jakub"
   write {_json} to json file "./plugins/Script/Jsons/test.json"

Loop all files in directory?

You can loop through an files in given directory, if you don't use as files tag you aren't able handle for example an loop-filename or either loop-filesize

on script load:
 
   # without as files
   loop all files in dir "...":
      send "File: &a%loop-file%"
   # with as files
    loop all json files in dir ".\plugins\SkriptHubDocsTool\documentation" as files:
        send "File: &a%loop-file%"
        send "File name: &c%loop-file's name%"
        send "File name: &c%loop-file's name without file type%"
        send "File size: &e%loop-file's size%"
        send "File path: &b%loop-file's path%"
        send "File content: &2%value "metadata" of loop-file's content%"

Clone this wiki locally