Mdfind piped to automator

automatorcommand line

I have an Automator app called convert2xlsx.app that takes file inputs and runs the Convert Format of Excel Files action to turn .xls files into .xlsx files. I can call it from the command line for one file like so:

automator -i "some file name.xls" ~/Desktop/convert2xlsx.app

and it works just fine.

I also have a command-line query like this:

mdfind -onlyin . 'kMDItemContentType == com.microsoft.excel.xls'

that finds all .xls files in the working directory and below.

I would really like to marry the two together but I can't seem to get it to work.

My first thought was this:

mdfind -onlyin . 'kMDItemContentType == com.microsoft.excel.xls' | xargs -J {} automator -i "{}" ~/Desktop/convert2xlsx.app

but that only converts the last file in the results returned by mdfind. I also tried:

mdfind -onlyin . 'kMDItemContentType == com.microsoft.excel.xls' -0 | xargs -0 -J {} automator -i "{}" ~/Desktop/convert2xlsx.app

with the same result.

I was able to piece it together in Automator, using an AppleScript action to get the directory of the front Finder window, a Shell Script action to run the mdfind in that directory and then the Convert Format of Excel Files on the result of the mdfind. So, problem solved. Yay! But…

I would really like to know where I was going wrong on the command line, as I'm making an effort to learn Bash scripting and use it more. Any advice?

Best Answer

As a test I created an Automator app that just copies finder items to a directory. "Copy Finder Items" Action

And then used

theFind=`mdfind -onlyin . 'kMDItemContentType == com.microsoft.excel.xls'`;automator -i "$theFind"  /Users/UserName/Desktop/testF/test2.app

The backpicks "`" insure that the first commands result is placed in the variable theFind and then we can pass theFind onto the next command by calling it with "$theFind". The quotes around it also in sure any text passed on is quoted.

The Automator app is then passed an Alias list.

{alias "hard drive:Users:Username:Dropbox:Kit:Unit2.xls", alias "hard drive:Users:Username:Dropbox:Kit:Unit3.xls"}

So depending on you other actions you may need to convert them to POSIX paths.

{"/Users/Username/Dropbox/Kit/Unit2.xls", "/Users/Username/Dropbox/Kit/Unit3.xls"}