1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Editorial Style Guide in AppleScript

Discussion in 'Mac Programming' started by scox, Dec 21, 2012.

  1. macrumors newbie

    #1
    Hi all,

    Long-time reader, first time poster. I'm and editor with a British publishing house, so I'm not a coder by trade and I generally find the help I need by pillaging various forums.

    I recently began using AppleScript to automate the most repetitive editorial changes our department makes to Word (or QuarkXpress) files as we format documents for publication. The script I've written is a very basic find/replace that gets the material from the American English of many of our analysts, into British English (the language of our brand). The script also fixes some random formatting choices we make – changing things like "6 million" into "6m", or "5 percent" into "5%", etc.

    It feels exceedingly clunky and inelegant, and I hope to improve that at some point. Still, the script has reduced my work load immensely and enabled me to focus more on factchecking (what I feel is the more important of my editorial responsibilities.

    I'd like to ask for advice on one specific obstacle. To get text into proper British spelling, I have to wage war on the letter "Z", and the script I've copied below shows how I go about changing all instances of "ize", "ization", and "yze" into their British counterparts with "s". Things like "specialize" become "specialise" and so forth. As an American myself, this feels like a tragic shift every time it is made, and scripting it has somewhat reduced my daily pain, in this regard.

    One sticking point persists: the word "size", which is spelled the same in both dialects. I included a contingency to fix all instances of " sise " so that it could be recovered after being altered by the other script, but this has, so far, failed to work. If the script changes the word "size" to "sise" using the "ize" to "ise" shift, it seems unable to recognize " sise " as an independent word and change it back. However, if I test a text with " sise ", it will identify the offense and amend it to "size". Thus, I'm a bit confused as to where the problem lies.

    Any suggestions on how I can fix this? I've tried to reduce the code below, so that the most relevant bits are present. My comments are included, because I get quickly bewildered when looking at long sections of code and forget what I'm doing. I have many other question, (namely, how to make this prettier) but I would prefer to focus on this for now.

    Code:
    tell application "Microsoft Word"
    	-- choose a file and open it
    	set theFile to choose file with prompt "Select a document:"
    	open theFile
    	read theFile
    	-- change language
    	set language ID of selection to english uk
    	-- simple find/replace for American to British spelling
    	execute find (find object of myRange) find text "labor" replace with "labour" replace replace all
    	execute find (find object of myRange) find text "ize" replace with "ise" replace replace all
    	execute find (find object of myRange) find text "izing" replace with "ising" replace replace all
    	execute find (find object of myRange) find text "ization" replace with "isation" replace replace all
    	execute find (find object of myRange) find text "yze" replace with "yse" replace replace all
    	execute find (find object of myRange) find text " sise " replace with " size " replace replace all
    end tell
    
    I appreciate any and all comments/suggestions, just please remember that I've been at this for all of two weeks, with no prior coding experience, so be forgiving.

    Cheers,
    -s
     
  2. macrumors regular

    #2
    Let me say I don't use MSWord so this may or may not work. But I do a whole heck of a lot of Applescript.
    What you are generally doing is:
    -read file
    -do some changes
    -do changes on the changed text

    You are doing the changes on the "first time through" text change when you read the file. The script, or MSWord, doesn't know about the changes made ON the original text. You need to re-read the file or somehow get the changed text in total, if you need to search for things that you changed the first time. So your structure would be something like this:

    -read file
    -do some changes (such as ize > ise)
    -get the changed file text (not the same syntax as "read file" since you already have the document opened)
    -do changes on anything changed the first time (such as " sise " > " size ")

    This should get you going in the right direction.
     
  3. macrumors newbie

    #3
    Thanks, but still stumped...

    First, thanks a lot for your answer, Supermacguy. I appreciated the didactic approach, because I ended up learning a few things about a number of topics as I tried to follow your advice: loops, text item delimiters, dialog boxes, and some alternative ways for accomplishing the find/replace task.

    Unfortunately, I was still unable to arrive at an answer that works. I observed something interesting though, and maybe you can tell me whether my observation is correct.

    I ran a loop to see if I could return to the beginning of the text to run a find all " sise " and replace with " size ". The result I got was an unstoppable loop returning an infinite stream of repeated " size ", starting from its first instance of " sise ", and I had to stop the script. This makes me think that the script editor reads the entire text once, and 'knows' where the instances of " size " are before changing them, and further, that the script does not make a note of the changes, which is why I end up stuck in the loop.

    I believe I'm correct in this inference, but of course I'm not sure.

    In any event, I decided I didn't need a loop just to go back to the beginning of the document and run the search. I took your advice and tried to tell the script to read the text a second time. One route I tried was to set a new variable equivalent to the changed text. Then I ran the search again for just the one element I needed to check.

    Code:
    tell application "Microsoft Word"
    	-- choose a file and open it
    	set theFile to choose file with prompt "Select a document:"
    	open theFile
    	read theFile
    	set myRange to text object of active document
    	execute find (find object of myRange) find text "ize" replace with "ise" replace replace all
    	read theFile
    	set newRange to text object of active document
    	-- set (every text of newRange where it is " sise ") to " size " <This is an alternative search/replace I learned about>
    	execute find (find object of newRange) find text " sise " replace with " size " replace replace all
    end tell
    
    This didn't work. However, I feel like I'm heading in generally the right direction, so I wanted to say thanks. If you have any other suggestions, I'm all ears. Seems to me that a simple way to do this is to have the script read the text object again... but I'm really not certain how to go about doing that.

    In any event, thanks again for your help.
     
  4. macrumors 6502a

    #4
    Don't know if this will help but it seems to do what I think you wanted. I added the delay to see if the find and replace worked.

    Code:
    set theFile to choose file with prompt "Select a document:"
    tell application "Microsoft Word"
    	-- choose a file and open it
    	open theFile
    	--	read theFile
    	set myRange to text object of active document
    	execute find (find object of myRange) find text "ize" replace with "ise" replace replace all
    	--read theFile
    	--	set newRange to text object of active document
    	-- set (every text of newRange where it is " sise ") to " size " <This is an alternative search/replace I learned about>
    	delay 5
    	execute find (find object of myRange) find text "sise" replace with "size" replace replace all with match whole word
    end tell
    
     

    Attached Files:

  5. ytk
    macrumors regular

    #5
    I looked at this and immediately said to myself, “That looks like a perfect job for regexps!” Then I remembered that would mean using regexps. :D
     
  6. macrumors newbie

    #6
    Still no answer...

    Hi everyone and happy holidays/new year.

    I meant to post a reply and 'thank you' note sooner, but the holidays were somewhat out of control. So, thank you for your ongoing help with this.

    Unfortunately, kryten2, the script didn't work for me. I'm not certain how to make screen shots as nicely as yours, so I'll just say that the results were such that any instance of " sise", regardless of where it is in the text, does get changed to " size" successfully on the first search of the document.

    However, even with the delay scripted in, the script doesn't seem to be re-capturing the text correctly/at all, following the changes. So, wherever there was " sise" to begin with, it stays changed to " size", but if the original text read " size", the computer isn't changing it back to " size" again from " sise".

    It's as if the computer is only reading the text once. I'm not sure how to ask that it read the text again in its entirety, after it has already done so once.

    Any thoughts on that point?
     
  7. macrumors 603

    Cromulent

    #7
    I have a problem. I know I'll use regexps. Now I have two problems.

    (Paraphrased from someone else who I can't remember the name of).
     

Share This Page