>...I have 30 language files that I automate graphics production and the
>text files are identical except each one calls a new file in the path.
>For example,
>
>file://KR.tif
>
>and I need to change the 'KR' to AU, GB, DK and so on and save each
>find and replace as a new text file. Can this be done with BBEdit? Is
>this something else other than what BBEdit was designed for?
It sounds like something that could be done very easily with BBEdit
but until you describe the problem in clear English and say just what
it is you want to save, I doubt if anyone will be able to help.
JD
Seems clear to me. The template file has "some text." JT wants to create new files, replacing "some text" for each of the new files.
I don't see (immediately) how it might be done with a search and replace or factory. If it were a web page, I would use PHP and assign the "some text" language designator to a variable. A base file would set the variable so each reader would get the language of choice.
A small program in perl could do much the same thing for a static file by repeatedly opening the template file, filling in the variable from an array and writing the file back out to a new file.
Bucky
The way I understand his needs, he wants to be able to create
file-templates that contain a "file://**.sufix" line and clone it to
produce 30 separate files replacing the ** with a list of prefixes.
Think of it as automating the process of manually creating a KR file
and saving it as KR.file, doing a find/replace of file://KR. with
file:AU. and saving as AU.file, and then replacing the AU with GB
(saving as GB.file), and repeating until you run out of country
prefixes.
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.
For example:
Text file "MYTEXT_EN.TXT" has:
path:EN.tif change to path:(variable).tif
(variable) is pulled from a list of 35 languages from another file. Such as
KR,
GB,
AU,
etc.
Save out 35 files with MYTEXT_(variable-language).TXT
Is this possible w/ BBedit? I've just picked up some Perl books at B&N
but I'd love to use BBEdit.
Any help is greatly appreciated. Sorry in advance if I'm not
explaining this right.
On Thu, Sep 1, 2011 at 1:24 PM, Robert A. Rosenberg <rar...@banet.net> wrote:
> At 09:20 +0100 on 09/01/2011, John Delacour wrote about Re: BBEdit - Find &
> Replace using a list:
>
>> At 17:05 -0700 31/08/2011, JT wrote:
>>
>>> ...I have 30 language files that I automate graphics production and the
>>> text files are identical except each one calls a new file in the path.
>>> For example,
>>>
>>> file://KR.tif
>>>
>>> and I need to change the 'KR' to AU, GB, DK and so on and save each
>>> find and replace as a new text file. Can this be done with BBEdit? Is
>>> this something else other than what BBEdit was designed for?
>>
>> It sounds like something that could be done very easily with BBEdit but
>> until you describe the problem in clear English and say just what it is you
>> want to save, I doubt if anyone will be able to help.
>>
>> JD
>
>
> The way I understand his needs, he wants to be able to create file-templates
> that contain a "file://**.sufix" line and clone it to produce 30 separate
> files replacing the ** with a list of prefixes.
>
> Think of it as automating the process of manually creating a KR file and
> saving it as KR.file, doing a find/replace of file://KR. with file:AU. and
> saving as AU.file, and then replacing the AU with GB (saving as GB.file),
> and repeating until you run out of country prefixes.
>
> --
> 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 bbe...@googlegroups.com
> To unsubscribe from this group, send email to
> bbedit+un...@googlegroups.com
> 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 "sup...@barebones.com" rather than posting to the group.
> Follow @bbedit on Twitter: <http://www.twitter.com/bbedit>
>
--
Santa Monica, Calif.
>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.
>
>For example:
>
>Text file "MYTEXT_EN.TXT" has:
>
>path:EN.tif change to path:(variable).tif
>
>(variable) is pulled from a list of 35 languages from another file. Such as
>KR,
>GB,
>AU,
>etc.
>
>Save out 35 files with MYTEXT_(variable-language).TXT
>
>Is this possible w/ BBedit? I've just picked up some Perl books at B&N
>but I'd love to use BBEdit.
BBEdit does it's cleverest stuff by using Apple events and shell
scripts -- Perl etc. You could use Perl alone but in this case you
might like to use a combination of AppleScript and Perl.
If you paste this script into Script Editor and run it with your
template file frontmost in BBEdit, the script will find all instances
of "_EN." (case-sensitive), create a file substituting the country
code for EN both in the file name and in its contents. The search
pattern I have used may not be exactly what you need.
The script can be saved in your BBEdit Scripts folder and run from
the palette or with a key shortcut. You could keep the country codes
in a separate file and have the script read that but I don't see the
point.
tell application "BBEdit"
tell front document
save it
set _template_mac to its file
end tell
set _template to POSIX path of _template_mac
end tell
do shell script "perl <<\"END\"
my @country_codes = qw(FR DE GB CN
BR KR SE NO TH CL GR);
my $template = qq~" & _template & "~;
open my $file_handle, qq~<$template~ or die $!;
my @lines = <$file_handle>; # Get the contents of the template
close $file_handle;
print qq~TEMPLATE: $template\\n\\n~; # just for demo
foreach my $cc (@country_codes){
$_ = $template;
s~(_)(EN)(\\.)~$1$cc$3~;
my $new_file = $_; # Create a new file name
print qq~$new_file\\n~; # just for demo
open FH, qq~>$new_file~ or die $!;
foreach (@lines){
s~(_)(EN)(\\.)~$1$cc$3~;
print FH;
}
close FH
}
END
"
--end of script
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
------------------------------------------------------------------------------------------------
> Hi,
>
> I have 30 language files that I automate graphics production and the
> text files are identical except each one calls a new file in the path.
> For example,
>
> file://KR.tif
>
> and I need to change the 'KR' to AU, GB, DK and so on and save each
> find and replace as a new text file. Can this be done with BBEdit? Is
> this something else other than what BBEdit was designed for?
Hi,
the scripts from John and Chris provide pretty good solutions (as always with that two very kind and helpful list members). I just wanted to add a different way to achieve this without the need for scripting (ok, I'm using some shell commands but only the most basic ones). I would use BBEdit's Shell Worksheets and placeholders to do this:
Assuming you have a folder "myproject" on your desktop and your master file is placed in it and named "master.txt", do the following.
1) In your master file replace every occurrence where you want to change the language code with #BASENAME# (in your example file://KR.tif would become file://#BASENAME#.tif)
2) Open a new Shell Worksheet and write this in it:
cd ~/Desktop/myproject
cp master.txt EN.html
cp master.txt DE.html
cp master.txt GB.html
... and so on
(extra tipp: just write down a list of your abbreviations one on each line, select all lines and use Text > Prefix/Suffix Lines to add everything else in one go)
And: just ignore, that your duplicate files are will be html files. They need to be named such to make step 4 work.
3) Select all lines from step 2 and hit Enter (not return!) This should execute all the lines duplicating your master file into all the needed language versions.
4) Select Markup > Update > Folder and update the myproject-Folder. (This command will ignore simple text files, so we had to disguise them as html in step 2).
This step should replace the placeholders in each file with the filename text ("EN", "DE, ...).
5) Rename the files back to something meaningful after updating them. Just repeat the technique from step 2 with a list of commands similar to:
mv EN.html 'my more meaningful file name_EN.txt'
mv DE.html 'my more meaningful file name_DE.txt'
and so on ...
I don't want to compete with the elaborate script solutions already provided. I just wanted to add, that BBEdit provides powerful tools you can use to create simple quick and dirty solutions that are very flexible and may be easier to adapt to similar problems (same, same, but different). To learn it's powers, I highly recommend to read the very fine manual on a rainy weekend (or even before that).
happy editing,
Roland
>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.
Ditto for my script, which now does what it's supposed to
tell application "BBEdit"
tell front document to set _template_mac to its file
set _template to POSIX path of _template_mac
end tell
do shell script "perl -w <<'END_OF_PERL';
use strict;
my @country_codes = qw(DE GB CN
BR KR SE NO TH CL GR);
my $template = qq~" & _template & "~;
open my $fh, qq~<$template~ or die $!;
my @lines = <$fh>; # get the lines of the doc
close $fh;
foreach my $cc (@country_codes){
$_ = $template;
s~(_)(EN)(\\.)~$1$cc$3~; # name the new file
open my $fh, qq~>$_~ or die $!; # create the new file
foreach (@lines){ # loop through the lines
my $modified_line = $_; # making the country code substitutions
$modified_line =~ s~(_)(EN)(\\.)~$1$cc$3~;
print $fh $modified_line; # write the changed line to the new file
}
close $fh
}
END_OF_PERL
"
--END of AppleScript