|Dec 21, 2012, 04:57 AM||#1|
Editorial Style Guide in AppleScript
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.
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
|Dec 21, 2012, 07:22 AM||#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:
-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:
-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.
|Dec 22, 2012, 01:58 PM||#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.
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
In any event, thanks again for your help.
|Dec 22, 2012, 04:24 PM||#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.
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
Space Corps Directive 34124
|Jan 2, 2013, 09:17 AM||#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?
|Jan 3, 2013, 02:50 AM||#7|
(Paraphrased from someone else who I can't remember the name of).
Neural Advance - Mac OS X, UNIX and Windows Development
Last.fm Profile | Extreme Metal Reviews
MP 4x 2.66Ghz Xeons / 6GB RAM / 640GB + 500GB + 750GB + 1TB HDDs / ATI Radeon 4870 / iPad 3
|Thread Tools||Search this Thread|
|thread||Thread Starter||Forum||Replies||Last Post|
|**GUIDE: Bootable Mac Pro 2009-10 SATA III + eSATA Guide + TRIM **||Inconsequential||Mac Pro||92||Apr 3, 2015 08:59 AM|
|AppleScript: How do I save multiple Excel sheets to one pdf using Applescript||sbtaylor1||Mac Programming||0||Oct 28, 2013 11:38 PM|
|AppleScript: How do I save multiple Excel sheets to one pdf using Applescript||sbtaylor1||Mac Applications and Mac App Store||0||Oct 28, 2013 11:32 PM|
|Markdown Text Editor 'Editorial' Launches for iPad with Workflow Automation Features||MacRumors||iOS Blog Discussion||11||Aug 16, 2013 01:54 PM|
|GUIDE: MacBook Air SSD Tweaking Guide||Orlandoech||MacBook Air||1||Feb 1, 2013 06:40 PM|
All times are GMT -5. The time now is 01:06 AM.