do shell script /usr/bin/mdls not working in Sierra

Hi:

Upgraded from Mavericks to Sierra 10.12.6, and now this do shell script exits with “a non-zero status”:


do shell script ("/usr/bin/mdls -name kMDItemNumberOfPages " & pdfMapFPX & " | /usr/bin/grep -o '[0-9]\\+$'")

Both mdls and grep are in my /usr/bin folder.

If I try this command in Terminal, it says it can’t find the pdf file (which does exist, as I dragged it to the Terminal to complete the command):

/usr/bin/mdls -name kMDItemNumberOfPages /Volumes/data/folder/folder/jazzy_pdf.pdf

Does anyone know why this do shell script doesn’t work? Do I need to do something with permissions somewhere to enable shell scripting with Applescript? Any help is appreciated!

-k

At first there is nothing wrong. by posting the error itself would be much more helpful for us.

Is the error from grep or mdls?
Is the variable pdfMapFPX in quoted form? Best known rule, arbitrary data should always be in quoted form.
Can you search spotlight? Meta database get damaged when updating, re-indexing can help.

In terminal type:

[format]cd /Volumes/data/folder/folder/
ls[/format]

do you see the pdf file in the terminal?

Hi:

Thanks for looking at this! The error is a dialog box that says:


"The command exited with a non-zero status.

The command exited with a non-zero status. (1)

             Edit button     OK button"

Is the error from grep or mdls?

Can’t tell, but I tried running the mdls without a grep argument with the same result.

Is the variable pdfMapFPX in quoted form?
Yes, prior to the do shell script I do:


set pdfMapFPX to (quoted form of POSIX path of pdfMapFP) as text

do you see the pdf file in the terminal?

Yes, running the ls command does show the pdf file.

Also, I just ran do shell Applescripts with ditto and rsync in them and they performed normally! I don’t get it. Maybe I should re-index Spotlight somehow? Thanks DJ!

Just a suggestion: try my Metadata Lib instead.

use mdLib : script "Metadata Lib" version "1.0.0"
use scripting additions

set theInfo to mdLib's fetchMetadataFor:"/Users/shane/Desktop/Simple.pdf" -- record of all metadata
set pageCount to kMDItemNumberOfPages of theInfo

It’s also considerably faster.

https://www.macosxautomation.com/applescript/apps/Script_Libs.html#Metadata_Lib

The error is clearly from grep but seems that mdls is not sending any data to the pipe. What if you run the following command and select the pdf file from the dialog box?

do shell script "mdls " & quoted form of POSIX path of (choose file)

Does it say it cannot find the file again? Does it say that with every file, if you select other files as well?

As Shane mentioned you could use the Spotlight in different ways. Not sure if you have AppleScript Toolbox installed but if you have it already installed you could try the following command instead:

set md to AST metadata for file "/Volumes/data/folder/folder/jazzy_pdf.pdf"
set pageCount to kMDItemNumberOfPages of md

Either way, it is to ensure that your SpotLight (and metadata importers) is working correctly. So we can help you further on the mdls command in the shell. Re-indexing is simply done using the following terminal command:

[format]sudo mdutil -i on /[/format]

Just a suggestion: try my Metadata Lib instead.

Hi Shane. I installed it but it returns this error:

 error "missing value doesn’t understand the “attributes” message." number -1708 from missing value

My script is:


use mdLib : script "Metadata Lib" version "1.0.0"
use scripting additions

set theFile to choose file
set theMetadata to mdLib's fetchMetadataFor:theFile

I got the same error using your example script with the hard-coded filepath. Did I install everything I need to? Thanks!

Hi DJ:

What if you run the following command and select the pdf file from the dialog box?

I get the error:

 /Volumes/data/folder/folder/jazzy_pdf.pdf: could not find /Volumes/data/folder/folder/jazzy_pdf.pdf.

Does it say that with every file, if you select other files as well?

Yes, I tried several other pdfs too.

if you have AppleScript Toolbox installed

Yes, just downloaded, installed and restarted.

Re-indexing is simply done using the following terminal command:

I get this error:

 Error: Index is already changing state.  Please try again in a moment.

However, I have tried numerous times before and after a restart and get the same msg. Corrupted Spotlight or maybe corrupted disk? I ran diskutil repairvolume and it said the disk was OK. Thanks!

It seems that the metadatabase is corrupted or an mdimporter for pdf files. I’ve seen this earlier with Mavericks to El Capitan updates as well.

I’m almost sure the database is damaged or an mdimporter

If the result is an empty list, SE throws an error or unexpectly quits (with older versions) the database is probably damaged.

Well, all errors you get seems to point that the database is damaged or an mdimporter is crashing. What if you turn it off first and turn it on again in the terminal? You get the same results when you go system preferences->spotlight and add your HD to the privacy locations, remove it and restart your machine.

Last but not least you should check the spotlight import tools of 3rd party applications that may not run properly in Sierra. The locations are /Library/Spotlight and ~/Library/Spotlight . If you find rare metadata importers (bundles with the extension mdimporter) you could move it outside the folder and see if it works. When is works and fails when you put the metadata importer back into the Spotlight folder you have found your problem.

Hi DJ:

I did the commands in this article ( http://blog.shiraj.com/2014/01/terminal-commands-for-improving-spotlight-indexing-volume/ ), including editing the /etc/hostconfig file, restarted, and voila, my original script works. Before doing those, I also deleted 3 mdimporter files from my /Library folder. (One was from Microsoft dated 2011.) Thanks to you and Shane I now also have AST and his tools to experiment with, so all is good. I would have never figured this out without you guys – THANKS!

So in case you have trouble with metadata again, here’s a way of getting the information directly:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "Quartz" -- for PDF stuff
use scripting additions


set thePath to POSIX path of (choose file of type {“pdf”})
set theURL to current application’s |NSURL|'s fileURLWithPath:thePath
set theDoc to current application’s PDFDocument’s alloc()'s initWithURL:theURL
set theCount to theDoc’s pageCount()



On under 10.11+, more simply:

use AppleScript version "2.5" -- El Capitan (10.11) or later
use framework "Foundation"
use framework "Quartz" -- for PDF stuff
use scripting additions

set theFile to (choose file of type {"pdf"})
set theDoc to current application's PDFDocument's alloc()'s initWithURL:theFile
set theCount to theDoc's pageCount()

Thanks for the feedback and the link to your solution. Nice to see everything is working properly again.

Thanks, Shane. That works great when I run it from the Script Editor in Sierra, but I get this error message when I try to save it as an .scpt:

 "The document “Untitled” could not be saved as “pdf page count.scpt”. C and Objective-C pointers cannot be saved in scripts."

Is there a way to save it as a script with Script Editor? Thanks!

Yes – you just need to recompile it before saving it, to clear the values stored by the top-level variables. You can also put it in a handler, to side-step the problem.

Thanks Shane, it worked!