是否可以使用Automator/Applescript读取文件夹内容生成JSON文件?

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

我想知道是否可以使用 Automator 和 Applescript 自动创建 JSON 文件。

场景: 我有图像文件夹,我一直在创建 JSON 文件来列出图像的各种属性:数字、文件名、百分比、类别.

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

我的 JSON 代码格式如下:

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

是否可以想出一个脚本来查看文件夹的内容并为每个图像创建一个具有上述属性的 JSON 文件?我是 Applescript(或任何形式的自动化代码)的新手,所以任何帮助我走向正确方向的帮助都会很棒。

我建议使用 bash shell 脚本,您可以使用 Applescript 调用该脚本:

do shell script "SCRIPTNAME"

因此,要了解我的方法如何工作,您可以 运行 Apple-supplied sips 程序,如下所示:

sips -g all *.png

示例输出

/Users/mark/Whosebug/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/Whosebug/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/Whosebug/2.png
  pixelWidth: 640
  pixelHeight: 480
  ...
  ...

所以,我建议用一些 awk 来解析它,它基本上是寻找模式并在找到它们时做一些事情。将其保存为 $HOME/parser.sh:

#!/bin/bash

# Do specified directory, or HOME if none specified
cd ""

# 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 = [=13=]}        # 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
   }'

然后使它可执行(只需要一次):

chmod +x $HOME/parser.sh

然后你可以运行它用这样的命令来检查你桌面上的所有图像:

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

并且 运行 来自 Applescript 的类似内容:

do shell script parser.sh "SomeDirectory"

输出是这样的:

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

如果您希望在桌面上名为 assets.json 的文件中输出,请使用:

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

如果您希望在输出的开始和结束处使用方括号,请进行如下更改:

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

如果您不熟悉编写 shell 脚本,并且您使用 TextEdit,请确保不要制作 RTF 文档,因此请使用 TextEditFormat菜单做Format->Make Plain Text.