Thursday, September 21, 2017

#1 2017-08-22 10:54:46 am

kerflooey
Member
Registered: 2011-07-07
Posts: 133

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":

     

Applescript:


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

Offline

 

#2 2017-08-22 11:19:08 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2709
Website

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

kerflooey wrote:

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!



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:

cd /Volumes/data/folder/folder/
ls

do you see the pdf file in the terminal?

Offline

 

#3 2017-08-22 11:37:29 am

kerflooey
Member
Registered: 2011-07-07
Posts: 133

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

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:

Applescript:


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!

Offline

 

#4 2017-08-22 08:47:33 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5125

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

Just a suggestion: try my Metadata Lib instead.

Applescript:

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/apples … tadata_Lib


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#5 2017-08-23 08:25:50 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2709
Website

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

kerflooey wrote:

The error is a dialog box that says:



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?

Applescript:

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:

Applescript:

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:

sudo mdutil -i on /

Offline

 

#6 2017-08-23 10:48:38 am

kerflooey
Member
Registered: 2011-07-07
Posts: 133

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

> 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:

Applescript:


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!

Offline

 

#7 2017-08-23 10:56:19 am

kerflooey
Member
Registered: 2011-07-07
Posts: 133

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

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!

Offline

 

#8 2017-08-23 03:21:57 pm

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2709
Website

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

kerflooey wrote:

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.



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.


kerflooey wrote:

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

Yes, I tried several other pdfs too.



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


kerflooey wrote:

> if you have AppleScript Toolbox installed

Yes, just downloaded, installed and restarted.



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

kerflooey wrote:


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



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.

Offline

 

#9 2017-08-23 05:36:00 pm

kerflooey
Member
Registered: 2011-07-07
Posts: 133

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

Hi DJ:

I did the commands in this article ( http://blog.shiraj.com/2014/01/terminal … ng-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!

Offline

 

#10 2017-08-23 10:45:26 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5125

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

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

Applescript:

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

[applescript]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()

[/applescript]
On under 10.11+, more simply:

Applescript:

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()


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#11 2017-08-24 07:38:18 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2709
Website

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

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

Offline

 

#12 2017-08-24 12:16:20 pm

kerflooey
Member
Registered: 2011-07-07
Posts: 133

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

Shane Stanley wrote:

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



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!

Offline

 

#13 2017-08-24 05:26:56 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5125

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

kerflooey wrote:

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



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.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#14 2017-08-24 05:51:30 pm

kerflooey
Member
Registered: 2011-07-07
Posts: 133

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

Thanks Shane, it worked!

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)