Hey MacScripters!
I’m trying to build a script and I’m beginning to wonder if it’s too ambitious, or a flat out pipe dream.
On a basic level, I’m just trying to execute a find a replace, but on a larger scale.
Here’s the skinny: I have documents that I need to change from one version of English spelling rules (eg – Canadian) to a different English spelling (eg - USA, Australian, Indian, Irish, etc, etc). This literally only entails finding and replacing specific words in a sentence (translating from US English to UK English, for example, “truck” becomes “lorry”, “maneuver” becomes “manoeuver”, “traveling” becomes “travelling”, etc). Obviously this isn’t really a translation, but it’s probably easier just to call this a Translator than a Finder-Replacer-of-English-Words
What I want to have happen is this:
-
Users select the file(s) they need ‘translated’
-
Users then select a spreadsheet with all the words for each given language where the column heading is the country, and the cells below that heading are the words used in that country (eg – A1 is “USA”, A2 is “truck”, A3 is “maneuver”, A4 is “traveling”; B1 is “Great Britain”, B2 is “lorry”, B3 is “manoeuver”, B4 is “travelling”, etc)
-
Users then select what version of English the file they uploaded is starting in, and the version of English they want it to become (eg - USA to Irish)
-
The applescript then looks through the uploaded file: I want it to find words that exist under one country, and replace those single instances of those words with the versions that exist in another country (eg – find “maneuver” in the document and replace it with “manoeuver”, or NOT, depending on the countries selected).
My code, so far, is below. It’s just the basic set up but I think it will help illuminate what I’m trying to accomplish if the above explanation is a bit too long-winded.
The “how” of this all is really big, I feel, and I’m not quite sure how to get a handle on it. I’d like to avoid system events because I worry, given the volume of files I want the script to handle, it might add onto the processing time. I’ll probably need a handler or shell script, neither of which I’m particularly adept at coding, but before I even get there I wanted to ask if what I’m trying to do is even possible?
Any tips or advice would be super helpful! Thanks so much!
--------------------------------------------------------------------------------------------------
--User selects file(s) needed to be localized.
set Files_For_Translation to (choose file with prompt "Select the File you want to localize English for. Multiple selections ARE permitted:" with multiple selections allowed)
set Each_File_For_Translation to every item of Files_For_Translation
--------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------
--User selects file that contains ALL the EN variant dialects on it. The reason I want them to select it is because if the file moves at all, and the file location is hard-coded into the Applescript, then the Applescript will break unless someone moves the file back, or changes the hardcoding in the script.
set English_Variant_Dialects_Dictionary to (choose file with prompt "Select the excel file that contains all the English language variant dialects" of type {"XLSX", "XLS"})
--------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------
--User selects the English Variant dialect the the content was written in
set Original_Language to (choose from list {"USA", "Canada", "Australia"} with prompt "Select the file's CURRENT English Language Variant Dialect")
--User selects the English Variant dialect they want the content translated to
set Desired_Language to (choose from list {"USA", "Canada", "Australia"} with prompt "Select the English Variant dialect you would like to translate your file(s) to")
display dialog "Click OK to begin translations."
--------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------
--Establishing language dictionary based on user input.
tell application "Microsoft Excel"
activate
open English_Variant_Dialects_Dictionary
--establishing origin language. This will be the basis of a "find" order in excel
if Original_Language is "Canada" then
set Original_Language_Dictionary to contents of range "A:A" of English_Variant_Dialects_Dictionary
else if Original_Language is "Australia" then
set Original_Language_Dictionary to contents of range "B:B" of English_Variant_Dialects_Dictionary
else if Original_Language is "USA" then
set Original_Language_Dictionary to contents of range "C:C" of English_Variant_Dialects_Dictionary
end if
--establishing desired language for the file to be converted to. This will be the basis of a "replace" order in excel
if Desired_Language is "Canada" then
set Desired_Language_Dictionary to contents of range "A:A" of English_Variant_Dialects_Dictionary
else if Desired_Language is "Australia" then
set Desired_Language_Dictionary to contents of range "B:B" of English_Variant_Dialects_Dictionary
else if Desired_Language is "USA" then
set Desired_Language_Dictionary to contents of range "C:C" of English_Variant_Dialects_Dictionary
end if
end tell
--------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------
--Now it gets to work doing the finding and replacing
tell application "Microsoft Excel"
activate
open Each_File_For_Translation
end tell
--------------------------------------------------------------------------------------------------