On Sep 01, 2011, at 17:42, jamie wrote:
> Thanks for the responses. Yes exactly. Just swapping out a language 2 letter 
> ISO code and saving new TXT documents with that ISO code in the filename. I 
> usually have 30 - 50 instances of the 2 letter code in my TXT documents.
______________________________________________________________________

Hey Jamie,

This of course presupposes that I've understood your problem correctly.

Populate the countryCodeList variable according to your needs (see comment in 
the script).

Save the script as an Applescript and put it in the BBEdit script menu.

Give it a handy keyboard shortcut.

Open your saved 'EN' text file template from the directory you wish to create 
the new files in.

Run the script.

If it doesn't work as desired let me know the particulars, and I'll fix it.

I wrote script 2 first to demonstrate how to use BBEdit for this task.  It took 
about 20 seconds to run through 50 country codes with an 1100 line file and 
about 100 replacements.

That seemed quite overlong to me, so I rewrote it (script 1) using Perl to do 
the find/replace and directly writing the files to disk.  This takes about 1.6 
seconds to complete the same task.  No doubt a pure Perl script would be 
faster, but my skill with it is as yet rudimentary.  (JD I see has beat me to 
the punch, and isn't his script nice and terse.  :)

Just for fun I replaced the Perl find/replace with the Satimage.osax's and got 
the time down to 0.3 seconds. (The Satimage.osax is a 3rd party freeware 
Applescript extension.)

So.  As you can see this tedious task you've been doing can be reduced to a 
triviality.

--
Best Regards,
Chris


------------------------------------------------------------------------------------------------
# SCRIPT 1: PERL FIND/REPLACE, DIRECT WRITE TO FILE
------------------------------------------------------------------------------------------------
on findReplace(srcString, findText, replaceText)
  set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's text item 
delimiters, findText}
  set newString to text items of srcString
  set AppleScript's text item delimiters to replaceText
  set newString to newString as text
  set AppleScript's text item delimiters to oldTIDS
  return newString
end findReplace
------------------------------------------------------------------------------------------------
on getParent(fileAlias)
  set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's text item 
delimiters, ":"}
  set parentFolderStr to ("" & text items 1 thru -2 of ("" & fileAlias)) & ":"
  set AppleScript's text item delimiters to oldTIDS
  return parentFolderStr
end getParent
------------------------------------------------------------------------------------------------
# WRITE TEXT TO A FILE OVERWRITING ANY CONTENT                      MODIFIED: 
2010-10-30 : 04:11
------------------------------------------------------------------------------------------------
on Write_To_File(someText, targetFile)
  try
    set resultNumber to open for access targetFile ¬
      with write permission
    set eof of resultNumber to 0
    write someText to resultNumber
    close access resultNumber
  on error errMsg number errNum
    try
      close access resultNumber
    on error errMsg number errNum
      beep
      display dialog "Error: " & errMsg & return & "Error Number: " & errNum
    end try
  end try
end Write_To_File
------------------------------------------------------------------------------------------------

#### INSERT ALL DESIRED COUNTRY CODES HERE ####
set countryCodeList to {"AD", "AE", "AF", "AG", "AI", "AL", "AM", "AN", "AO", 
"AQ", "AR", "AS", "AT", "AU", "AW", "AX", "AZ", "BA", "BB", "BD", "BE", "BF", 
"BG", "BH", "BI", "BJ", "BM", "BN", "BO", "BR", "BS", "BT", "BV", "BW", "BY", 
"BZ", "CA", "CC", "CD", "CF", "CG", "CH", "CI", "CK", "CL", "CM", "CN", "CO", 
"CR", "CS"}

tell application "BBEdit"
  try
    tell front text document
      try
        set docFile to its file as alias
      on error
        error "DOCUMENT IS UNSAVED!"
      end try
      set docName to its name
      set parentFolder to getParent(docFile) of me
      set docContents to quoted form of (get its contents)
    end tell
    
    repeat with ndx in countryCodeList
      set countryCode to (ndx)
      set newDocName to findReplace(docName, "_EN.", "_" & countryCode & ".") 
of me
      set newDocPath to parentFolder & newDocName
      set newText to do shell script "perl -ne 's/\\bEN\\b/" & countryCode & 
"/g; print' <<< " & docContents
      
      Write_To_File(newText, newDocPath) of me
      
    end repeat
    
  on error errMsg number errNum
    set sep to "=============================="
    set e to sep & return & "Error: " & errMsg & return & sep & return ¬
      & "Error Number: " & errNum & return & sep
    beep
    display dialog e
  end try
end tell
------------------------------------------------------------------------------------------------

################################################################################################

------------------------------------------------------------------------------------------------
# SCRIPT 2: USES BBEDIT FOR MOST OPERATIONS
------------------------------------------------------------------------------------------------
on findReplace(srcString, findText, replaceText)
  set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's text item 
delimiters, findText}
  set newString to text items of srcString
  set AppleScript's text item delimiters to replaceText
  set newString to newString as text
  set AppleScript's text item delimiters to oldTIDS
  return newString
end findReplace
------------------------------------------------------------------------------------------------
on getParent(fileAlias)
  set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's text item 
delimiters, ":"}
  set parentFolderStr to ("" & text items 1 thru -2 of ("" & fileAlias)) & ":"
  set AppleScript's text item delimiters to oldTIDS
  return parentFolderStr
end getParent
------------------------------------------------------------------------------------------------

#### INSERT ALL DESIRED COUNTRY CODES HERE ####
set countryCodeList to {"AD", "AE", "AF", "AG", "AI", "AL", "AM", "AN", "AO", 
"AQ", "AR", "AS", "AT", "AU", "AW", "AX", "AZ", "BA", "BB", "BD", "BE", "BF", 
"BG", "BH", "BI", "BJ", "BM", "BN", "BO", "BR", "BS", "BT", "BV", "BW", "BY", 
"BZ", "CA", "CC", "CD", "CF", "CG", "CH", "CI", "CK", "CL", "CM", "CN", "CO", 
"CR", "CS"}

tell application "BBEdit"
  try
    tell front text document
      try
        set docFile to its file as alias
      on error
        error "DOCUMENT IS UNSAVED!"
      end try
      set docName to its name
      set parentFolder to getParent(docFile) of me
      set docContents to its contents
    end tell
    
    repeat with ndx in countryCodeList
      set countryCode to (ndx)
      set newDocName to findReplace(docName, "_EN.", "_" & countryCode & ".") 
of me
      set newDocPath to parentFolder & newDocName
      set newDoc to make new text window with properties {contents:docContents}
      tell newDoc
        set bbrep to replace "EN" using countryCode ¬
          options {case sensitive:true, match words:false, search mode:grep, 
starting at top:true}
        tell its active document
          save to file newDocPath without saving as stationery
          close
          delay 0.01 # Without this delay BBEdit stacks up windows before 
closing them.
        end tell
      end tell
    end repeat
    
  on error errMsg number errNum
    set sep to "=============================="
    set e to sep & return & "Error: " & errMsg & return & sep & return ¬
      & "Error Number: " & errNum & return & sep
    beep
    display dialog e
  end try
end tell
------------------------------------------------------------------------------------------------

-- 
You received this message because you are subscribed to the 
"BBEdit Talk" discussion group on Google Groups.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
<http://groups.google.com/group/bbedit?hl=en>
If you have a feature request or would like to report a problem, 
please email "[email protected]" rather than posting to the group.
Follow @bbedit on Twitter: <http://www.twitter.com/bbedit>

Reply via email to