Jump to content

simple applescript


Recommended Posts

I am hoping someone can help me since I am not a very experienced Applescript user or programmer.

 

I'd like an Applescript application that lets me drop a file containing a list of filenames. The script proceeds to process each filename on the list and converts the file from rtf to docx. I'd actually like a framework in which most any Office type conversion can be inserted. However, for the moment, rtf2docx is in the critical path. (But doc to docx, docx to text, xls to xlsx, are among the things needed.)

 

I need the output files written to the same directory as the input. The files, however, are scattered over a 4T disk, so don't tell me to put them all in one folder, or use Finder/Automator. If the latter is possible, I am not seeing how at present since there are lots of files and they exist in random places.

 

I have found scripts to do the conversions, but most of these want you to drag and drop individual files. That doesn't work when you have thousands of files to process. Therefore, accepting a filelist as input is critical because I generate that list from the commandline.

 

Also, don't tell me to use textutil. That tool is broken - it ignores information in the headers of the documents, which is a problem for us.

 

TIA,

 

-Joe

 

Link to comment

I'm assuming that `rtf2docx` is a command line utility because it sounds like one. If it isn't, then go look at pandoc.

 

Continuing with that premise: if we can use it on the command line, then just stay in the command line and use a simple bash script. Note: the following script is untested and needs to be modified for the right conversion command. Don't use it without testing it first:

#!/bin/bash

# This is the full path to the filelist, which should have one file per line,
# and it should be in plain text
file_list="/full/path/to/file/list"

# Start a while loop to go through each file
while read file
do
    # Check to make sure the file exists
    if [[ ! -f "${file}" ]]; then
        echo -e "Error: ${file} does not exist... skipping."
        continue
    fi
    # Check to make sure that the file has an rtf extension
    if [[ "${file#*\.*}" != "rtf" ]]; then
        echo -e "Error: ${file} is not an RTF... skipping."
        continue
    fi
    
    # Set some variables
    dir=$(dirname "${file}") # The directory where the file is stored
    filename=${s##*/}        # The filename with extension
    filename=${s%.*}         # the name of the file with no extension

    # This is the actual command to do the conversion, so alter it to make it fit the correct syntax to invoke the command
    /path/to/rtf2docx --in="${file}" --out="${dir}/${filename}.docx"

done < $file_list

Link to comment

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