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

问题描述

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

场景:我有图像文件夹,并且我一直在创建JSON文件,该文件列出了有关图像的各种属性:num,文件名,百分比,类别。

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"

因此,要了解我的方法是如何工作的,您可以像这样运行苹果提供的sips程序:

sips -g all *.png

示例输出

/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
  ...
  ...

因此,我建议使用一些awk进行解析,该$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\",filename) printf(" \"percent\": %f,aspect) printf(" \"category\": \"%s\"\n",category) printf("}\n") serial += 1 }'

chmod +x $HOME/parser.sh

然后使用以下命令将其变为可执行文件(只需一次):

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

然后,您可以使用以下命令运行它以检查桌面上的所有图像:

do shell script parser.sh "SomeDirectory"

并通过类似以下内容的Applescript运行它:

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

输出如下:

assets.json

如果要在桌面上名为$HOME/parser.sh SOMEDIRECTORY > $HOME/Desktop/assets.json 的文件中输出,请使用:

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

如果要在输出的开头和结尾使用方括号,请进行如下更改:

Format

如果您不熟悉编写Shell脚本,并使用 TextEdit ,请确保不要制作RTF文档,因此请使用 TextEdit Format菜单做Make Plain Text-> SharedPreferences sharedPref = MainActivity.getPreferences(Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPref.edit(); editor.putInt(cd.getColor(),"colorID"); editor.apply();

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...