Search code examples
automationapplescriptautomator

Is it possible to use Automator/Applescript to generate a JSON file by the reading contents of a folder?


I would like to know if it's possible to automate the creation of a JSON file by using Automator and Applescript.

Scenario: I have folders of images, and I have been creating JSON files that list the various properties about the images: num, filename, percent, category.

num = each file in the folder is assigned a number, 1 to infinity
filename = name of the image file in the folder (without the extension)
percent = the image's height divided by the image's width
category = the name of the folder that the images are located

My JSON code is formatted like:

 {
   "num": 1,
   "filename": "image-name",
   "percent": 66.67,
   "category": "nature-images"
 }

Would it be possible to come up with a script that could look at the contents of a folder and create a JSON file with the above properties per image? I'm brand new to Applescript (or any form of automation code) so any help to put me in the right direction would be great.


Solution

  • I would suggest doing that with a bash shell script, which you can call from Applescript using:

    do shell script "SCRIPTNAME"
    

    So, to see how my approach works, you could run the Apple-supplied sips program like this:

    sips -g all *.png
    

    Sample Output

    /Users/mark/StackOverflow/1010.png
      pixelWidth: 100
      pixelHeight: 100
      typeIdentifier: public.png
      format: png
      formatOptions: default
      dpiWidth: 72.000
      dpiHeight: 72.000
      samplesPerPixel: 4
      bitsPerSample: 2
      hasAlpha: yes
      space: RGB
      profile: sRGB IEC61966-2.1
    /Users/mark/StackOverflow/1020.png
      pixelWidth: 100
      pixelHeight: 100
      typeIdentifier: public.png
      format: png
      formatOptions: default
      dpiWidth: 72.000
      dpiHeight: 72.000
      samplesPerPixel: 4
      bitsPerSample: 2
      hasAlpha: yes
      space: RGB
      profile: sRGB IEC61966-2.1
    /Users/mark/StackOverflow/2.png
      pixelWidth: 640
      pixelHeight: 480
      ...
      ...
    

    So, I propose parsing this with some awk which basically looks for patterns and does things when it finds them. Save this as $HOME/parser.sh:

    #!/bin/bash
    
    # Do specified directory, or HOME if none specified
    cd "$1"
    
    # Generate a list of all image filenames and heights and widths with "sips", discarding errors
    sips -g all * 2> /dev/null | awk -v category="$(pwd)" '
       BEGIN          {serial = 1}           # allocate serial number
       /^[^ ]/        {filename = $0}        # pick up path if line does not start with space
       /pixelWidth:/  {width = $NF}          # pick up last field as width
       /pixelHeight:/ {
           height  = $NF                     # pick up last field as height
           sub(".*/", "", filename)          # extract filename from full path
           aspect = 100*height/width         # calculate aspect ratio
           printf("{\n")
           printf("   \"num\": %d,\n", serial)
           printf("   \"filename\": \"%s\",\n", filename)
           printf("   \"percent\": %f,\n", aspect)
           printf("   \"category\": \"%s\"\n",category)
           printf("}\n")
           serial += 1
       }'
    

    Then make it executable (just necessary once) with:

    chmod +x $HOME/parser.sh
    

    Then you can run it with a command like this to check all images on your Desktop:

    $HOME/parser.sh /Users/YOU/Desktop
    

    And run it from Applescript with something like:

    do shell script parser.sh "SomeDirectory"
    

    The output is like this:

    {
       "num": 1,
       "filename": "1010.png",
       "percent": 100.000000,
       "category": "/Users/mark/StackOverflow"
    }
    {
       "num": 2,
       "filename": "1020.png",
       "percent": 100.000000,
       "category": "/Users/mark/StackOverflow"
    }
    {
       "num": 3,
       "filename": "2.png",
       "percent": 75.000000,
       "category": "/Users/mark/StackOverflow"
    }
    

    If you want the output in a file called assets.json on your Desktop, use:

    $HOME/parser.sh SOMEDIRECTORY > $HOME/Desktop/assets.json
    

    If you want square brackets at the start and end of your output, change as follows:

    BEGIN  { serial = 1; print "[" }
    END    { print "]" } 
    

    If you are unfamiliar with writing shell scripts, and you use TextEdit, be sure not to make an RTF document, so use the TextEdit Format menu to do Format->Make Plain Text.