Jump to content

Parsing exiftool output into Script Filter JSON


Recommended Posts

What I'm trying to achieve

I wish to search a given folder for *.xmp files (sidecar files containing photo metadata), run the results through exiftool and extract the filename and EXIF description field—displaying the results in the script filter.

 

What I can achieve

In my script filter I can procure the appropriate output from exiftool—which will list the results like this:

filename1 : A beautiful photo
filename2 : An amazing photo
filename3 : An award-winning photo

 

What the script filter contains

# The following exiftool command works perfectly
# exiftool -p '$filename : $description' -q -f -m -ext DNG $1

targets=($(exiftool -p '$filename : $description' -q -f -m -ext DNG $1))

osascript -l JavaScript -e 'function run(argv) {
  const sfItems = argv.map(argumentItem => ({
    title: argumentItem,
    subtitle: "⏎ to open; ⌥ + ⏎ to reveal in Finder; ⇧ + ⏎ to Action in Alfred",
    arg: argumentItem
  }))
  return JSON.stringify({ items: sfItems })
}' "${targets[@]}"

 

What happens when the script filter runs

The results are presented something like this:

filename1
 : 
A
Beautiful
Photo
filename2
[etc.]

 

The problem

If not apparent, the problem is that the $description needs to be a single string and not split.

 

I'd be grateful for some guidance (something of an understatement) and have put the basic workflow temporarily on Dropbox. As I have never coped with JSON—and never shall—please be gentle.

 

Stephen

Link to comment

OK - it's like going to the doctor and the complaint disappearing as soon as you open your mouth there. I am an idiot. The following at the start of the script filter resolves the issue:

# Set the Internal Field Separator to newline
IFS=$'\n'

 

Apologies for taking your time.

 

Stephen

Link to comment
12 minutes ago, Stephen_C said:

it's like going to the doctor and the complaint disappearing as soon as you open your mouth there.

 

There is a name for that in computing. I would say there is an important distinction from the doctor’s case: you did figure out what was wrong and have the “cure” for future cases.

 

I’d also like to point out how JSON wasn’t the issue here, that you have perfect!


For the benefit of other users bumping into this later and looking to understand more, the issue was that exiftool produces one large string, with one result per line, each having several words (more specifically, pieces of text separated by spaces). When that output is converted to an array (via the surrounding parenthesis), the shell is taking the one large string, splitting it on (by default) spaces, newlines, and tabs, and making each of those into an item.


Thus the solution is to instruct the shell to “hey, just this once, only split on newlines”. Thus overriding the IFS, or Internal Field Separators.

Link to comment
  • vitor changed the title to Parsing exiftool output into Script Filter JSON

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...