Can someone help me automate a script?

Discussion in 'Mac Programming' started by I Need a Drink, Apr 18, 2018.

  1. I Need a Drink macrumors regular

    Oct 14, 2013
    I posted a while ago about problems I was having with Spotlight and .docx files. I thought I had fixed the problem, but apparently that is not correct. This issue is Spotlight simply is not indexing my .docx files. This is happening on all three of my Macs. Spotlight will index the file name and searching for a file name works fine, but it won't search the contents of the document so I can't find a file using a keyword search. This appears to only be a problem on .docx files as I can create a new .doc file and it indexes immediately. I have tried re-indexing Spotlight, both through adding my drive to the Privacy tab and then removing it and through the Terminal command sudo mdutil -e/

    This didn't work as both methods re-indexed the drive, but still left out .docx files (in terms of content). I found a post on Apple's Forums that gave a Terminal command, find ~ -type f -name '*.docx' -print -exec mdimport -d1 {} \; which did fix the problem for existing files as they are all now searchable under Spotlight, Finder and Siri, but new documents and changes are still not indexed. The author of the post mentioned that he has created a script, posted below, that he runs every hour to keep track of newly changed and created documents, but I have no idea how to make my Mac run it automatically. He mentioned Cron, which I have no idea what that is, and I found another post that said make the script a plist and use launchd. Can someone help me figure out how to automate this script so that my new and changed Word documents will be searchable? Thank you.

    # forces spotlight to re-indexing of .docx files in a range of modification/creation times.
    # does this by running the "mdimport" command on the appropriate .docx files.
    # this is intended to be run at least once every 24 hours.
    # setup time stamp file.
    # set time stamp file to current date/time.
    touch "$time_stamp"
    # adjust the date back in time 24 hours.
    touch -a -240000 "$time_stamp"
    # index .docx files that were modified since the time stamp file.
    find ~ -type f -name '*.docx' -newer "$time_stamp" -print -exec mdimport -d1 {} \;
  2. superscape macrumors 6502a


    Feb 12, 2008
    East Riding of Yorkshire, UK
    Sure. Cron is the "old" way of scheduling scripts, but it's probably the easiest. Check out an app called Cronnix which is a front end onto it - it's reasonably self explanatory.

    Using a launch agent is the "new" way but its a bit fiddly. There are instructions here:

    Again, there is an app called Lingon that gives you a GUI to work with - I can't vouch fr it since I've not used it. I hear good reports though.

    Good luck!
  3. I Need a Drink thread starter macrumors regular

    Oct 14, 2013
    Thanks for the post. I'm not much of a coder. In actuality, I have no coding experience so I'm still kind of lost. I do understand the code that I found posted from another forum, which I posted above, but I'm still kind of lost on what to do to make it run. I'm also leery of installing any 3rd party software from a Google group and don't particularly want to purchase a $10 program from the App Store to do this one task, when I know it can be done with a few commands for free. Is there any way you, or someone else, could give me a step by step as to how to make this work without having to install a 3rd party app?

    Thank you
  4. superscape macrumors 6502a


    Feb 12, 2008
    East Riding of Yorkshire, UK

Share This Page