Search and Replace String

Hello,

I am working on a script that searches for a set of data errors, and replaces them with correct ones. I think I am on the right track, but my script is not quite working yet…here is what I have so far:
tell application “BBEdit”
activate

tell application "System Events"
	if UI elements enabled then
		tell process "BBEdit"
			set frontmost to true
		end tell
		
		key down command
		keystroke "a"
		key up command
		delay 0.25
		
		key down command
		keystroke "c"
		key up command
		delay 0.5
		
	else
		tell application "System Preferences"
			activate
			set current pane to pane "com.apple.preference.universalaccess"
			display dialog "UI element scripting is not enabled. Check \"Enable access for assistive devices\""
		end tell
	end if
end tell

set search_strings to {"ACTE", "  < - ", "No 1", "No 2", "No 3", "No 4", "No 5", "No 6", "No 7", "No 8", "No 9", ".", "20. ", "19. ", "18. ", "17. ", "16. ", "15. ", "14. ", "13. ", "12. ", "11. ", "1. ", "2. ", "3. ", "4. ", "5. ", "6. ", "7. ", "8. ", "9. ", "10. ", "I ", "II ", "III ", "IV ", "V ", "VI ", "VII ", "VIII ", "IX ", "X ", "XI ", "XII ", "XIII ", "XIV ", "XV ", "XVI ", "XVII ", "XVIII ", "XIX ", "XX ", ", I", ", II", ", III", ", IV", ", V", ", VI", ", VII", ", VIII", ", IX", ", X", ", XI", ", XII", ", XIII", ", XIV", ", XV", ", XVI", ", XVII", ", XVIII", ", XIX", ", XX", ".", " : ", " , ", " A ", " An ", " The ", " And ", " Or ", " For ", " Of ", " To ", " From ", " In ", "Min ", "min ", "Maj ", "maj ", " major", " minor", " major,", " minor,", "sharp", " Sharp", "Sharp", "flat", " Flat", "Flat", "a--Sharp", "b--Sharp", "c--Sharp", "d--Sharp", "e--Sharp", "f--Sharp", "g--Sharp", "a--Flat", "b--Flat", "c--Flat", "d--Flat", "e--Flat", "f--Flat", "g--Flat", "a Minor", "b Minor", "c Minor", "d Minor", "e Minor", "f Minor", "g Minor", "a Major", "b Major", "c Major", "d Major", "e Major", "f Major", "g Major", "√ ", "À", "√¢", "√°", "√£", "√¢", "Â", "√§", "°", "√©", "√®", "ê", "√´", "ï", "î", "ç", "√±", "√¥", "√∏", "ö", "ˆ", "¬Ãº", "√π", "√∫", "√º", "ß", "¬¥", "í", "&", "â", "ã", "ç", "è", "é", "ì", "ñ", "Ø", "õ", "ö", "Ó", "Ã1⁄4", "Ķ", "Ã", "--", "√¶", "√¢", "√°", "n°", "feat.", "feat ", "Versus", "Vs.", ", Op ", "Opus", "OP.", "op.", "Op.", " Op.", ",,", "NO. ", "no.", "No.", "#", "Number", "Television", " Part ", "Part ", "part ", "pt.", "   ", "  ", " TWV", "bwv", "Bwv", "BWV", "Hwv", "HWV", "KV", " K ", " K.", "K1", "K2", "K3", "K4", "K5", "K6", "K7", "K8", "K9", " S ", " S.", "S1", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9", " D ", " D.", "D1", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "L1", "L2", "L3", "L4", "L5", "L6", "L7", "L8", "L9", "H1", "H2", "H3", "H4", "H5", "H6", "H7", "H8", "H9", " Rv", " RV", "grave", "largo", "lento", "adagio", "andante", "allegretto", "allegro", "vivace", "presto", "prestissimo", " Assai ", " Con ", " Brio ", " Non ", " Troppo ", " Molto ", " Poco ", " Più ", " Meno ", "symphony", "orchestra", "chamber", "woodwind", "woodwinds", "brass", "strings", "string", "percussion", " solo ", " duet ", " trio ", " quartet ", " quintet ", " sextet ", " septet ", " octet ", "/", "soprano", "alto", "tenor", "baritone", "bass", "piccolo", "flute", "clarinet", "oboe", "english", "horn", "bassoon", "saxophone", "trumpet", "trombone", "euphonium", "cornet", "piccolos", "flutes", "clarinets", "oboes", "horns", "bassoons", "saxophones", "trumpets", "trombones", "euphoniums", "cornets", "flugel", "tuba", "sousaphone", "violin ", "tubas", "sousaphones", "violins ", "viola ", "cello", "bass", "piano", "organ", "harpsichord", "guita", "pianos", "organs", "harpsichords", "guitars", " lute", "recorder", "recorders", "Volume", "Vol ", "concerto", "concerti", "  ", " - Original", " - Elatus", " - APEX", "'", "( ", " )", "(", ") :", "  ", "'T ", "'S ", "'D ", "(a", "(b", "(c", "(d", "(e", "(f", "(g", "(h", "(i", "(j", "(k", "(l", "(m", "(n", "(o", "(p", "(q", "(r", "(s", "(t", "(u", "(v", "(w", "(x", "(y", "(z", " ,", "  ", "1 No. ", "2 No. ", "3 No. ", "4 No. ", "5 No. ", "6 No. ", "7 No. ", "8 No. ", "9 No. ", "Nr.", "IX ", "IV ", "XV ", " V ", ". . . ", "D. Minor", ": the", "Bb ", "Eb ", "Ab ", "Db ", "Gb ", "ACT", "N.", " Nos ", ". con ", ". Molto ", "sonata", "< ", "Variatio ", "Var.", "variations", "Variationen", "Movement 1: ", "Movement 2: ", "Movement 3: ", "Movement 4: ", "Movement 5: ", "Movement 6: ", "Movement 7: ", "Movement 8: ", "Movement 9: ", "Movement 10: ", "Movement 11: ", "Movement 12: ", "Movement 13: ", "Movement 14: ", "Movement 15: ", "Movement 16: ", "Movement 17: ", "Movement 18: ", "Movement 19: ", "Movement 20: ", "Movement 21: ", "Movement 22: ", "Movement 23: ", "Movement 24: ", "Movement 25: ", "Movement 26: ", "Movement 27: ", "Movement 28: ", "Movement 29: ", "Movement 30: ", "Movement 31: ", "Movement 32: ", "Movement 33: ", "1st Movement: ", "2nd Movement: ", "3rd Movement: ", "Z1", "Z2", "Z3", "Z4", "Z5", "Z6", "Z7", "Z8", "Z9", " L'", "1: No. ", "2: No. ", "3: No. ", "4: No. ", "5: No. ", "6: No. ", "7: No. ", "8: No. ", "9: No. ", ": a", ": p", ": t", ": o", ": i", ": f", ": assai", ": con", ": non", ": molto", ": poco", ": più", ": meno", ". the ", ": the ", " Eb, ", "Ii. ", "Iii. ", "Iv. ", "Vi. ", " Sz1", " Sz2", " Sz3", " Sz4", " Sz5", " Sz6", " Sz7", " Sz8", " Sz9", " B1", "1er Mvt ", "2eme Mvt ", "3eme Mvt ", "4eme Mvt ", "ATTO PRIMO", "ATTO SECONDO", "ATTO TERZO", " First Movement:", " Second Movement:", " Third Movement:", " Fourth Movement:", ". a", " SWV", ". poco", "Sym ", "Min ", "min ", "Maj ", "maj ", " major", " minor", " major,", " minor,", "a Minor", "b Minor", "c Minor", "d Minor", "e Minor", "f Minor", "g Minor", "a Major", "b Major", "c Major", "d Major", "e Major", "f Major", "g Major", "A. Minor", "B. Minor", "C. Minor", "D. Minor", "E. Minor", "F. Minor", "G. Minor", "A. Major", "B. Major", "C. Major", "D. Major", "E. Major", "F. Major", "G. Major", " (1989 Digital Remaster)", " (1990 Digital Remaster)", " (1998 Digital Remaster)", " (1988 Digital Remaster)", " (2000 Digital Remaster)", " (2007 Digital Remaster)", " (1995 Digital Remaster)", " (1999 Digital Remaster)", " (1959 Digital Remaster)", " (1992 Digital Remaster)", " (1961 Digital Remaster)", " (1958 Digital Remaster)", " (1999 Digital Remaster)", " (1961 Digital Remaster)", " (1997 Digital Remaster)", " (1965 Digital Remaster)", " (1987 Digital Remaster)", " (1996 Digital Remaster)", " (1994 Digital Remaster)", " (1973 Digital Remaster)", " (2004 Digital Remaster)", " (1985 Digital Remaster)", " (2002 Digital Remaster)", " (2001 Digital Remaster)", " (1952 Digital Remaster)", " (1954 Digital Remaster)", " (2005 Digital Remaster)", " (1991 Digital Remaster)", " (1986 Digital Remaster)", " (1993 Digital Remaster)", " (1979 Digital Remaster)", " (2006 Digital Remaster)", " (1974 Digital Remaster)", " (1981 Digital Remaster)", " (1975 Digital Remaster)", " (1956 Digital Remaster)", " (1964 Digital Remaster)", " (1976 Digital Remaster)", "Atto Primo / Act ONE / ERSTER AKT / PREMIER Acte", "ERSTER AKT / Act 1 / PREMIER ActE / ATTO PRIMO", "Atto Secondo / Act TWO / ZWEITER AKT / DEUXIEME Acte", "Atto Terzo / Act THREE / DRITTER AKT / TROISIEME Acte", "ATTO QUATRO / Act FOUR / VIERTER AKT / QUATRIEME Acte", "Parte Prima:", "Parte Seconda:", "Parte Terza:", ": - ", "KAnh", " in D.", ". - ", " H. ", " D. ", " BWV ", ",, D. ", ",, BWV", " min:", " maj:", ",, H. ", " BMW "}

set replace_strings to {"Acte", ": ", "No. 1", "No. 2", "No. 3", "No. 4", "No. 5", "No. 6", "No. 7", "No. 8", "No. 9", ". ", "XX. ", "XIX. ", "XVIII. ", "XVII. ", "XVI. ", "XV. ", "XIV. ", "XIII. ", "XII. ", "XI. ", "I. ", "II. ", "III. ", "IV. ", "V. ", "VI. ", "VII. ", "VIII. ", "IX. ", "X. ", "I. ", "II. ", "III. ", "IV. ", "V. ", "VI. ", "VII. ", "VIII. ", "IX. ", "X. ", "XI. ", "XII. ", "XIII. ", "XIV. ", "XV. ", "XVI. ", "XVII. ", "XVIII. ", "XIX. ", "XX. ", ": I", ": II", ": III", ": IV", ": V", ": VI", ": VII", ": VIII", ": IX", ": X", ": XI", ": XII", ": XIII", ": XIV", ": XV", ": XVI", ": XVII", ": XVIII", ": XIX", ": XX", ". ", ": ", ", ", " a ", " an ", " the ", " and ", " or ", " for ", " of ", " to ", " from ", " in ", "Minor ", "Minor ", "Major ", "Major ", " Major", " Minor", " Major,", " Minor,", "Sharp", "-Sharp", "-Sharp", "Flat", "-Flat", "-Flat", "A--Sharp", "B--Sharp", "C--Sharp", "D--Sharp", "E--Sharp", "F--Sharp", "G--Sharp", "A--Flat", "B--Flat", "C--Flat", "D--Flat", "E--Flat", "F--Flat", "G--Flat", "A Minor", "B Minor", "C Minor", "D Minor", "E Minor", "F Minor", "G Minor", "A Major", "B Major", "C Major", "D Major", "E Major", "F Major", "G Major", "à ", "À", "â", "á", "ã", "â", "Â", "ä", "ä", "é", "è", "ê", "ë", "ï", "î", "ç", "ñ", "ô", "ø", "ö", "ö", "œ", "ù", "ú", "ü", "ß", "'", "'", "&", "â", "ã", "ç", "è", "é", "ì", "ñ", "Ñ", "ô", "ö", "Ö", "ü", "...", "à ", "-", "æ", "å", "á", "No. ", "Featuring", "Featuring ", "vs.", "vs.", ", Op. ", "Op.", "Op.", "Op. ", "Op. ", ", Op.", ",", "No. ", "No. ", "No. ", "No. ", "No. ", "TV", " Pt. ", "Pt. ", "Pt. ", "Pt.", " ", " ", ", TWV", "BWV", "BWV", "BWV ", "HWV", "HWV ", "K. ", " K. ", " K. ", "K. 1", "K. 2", "K. 3", "K. 4", "K. 5", "K. 6", "K. 7", "K. 8", "K. 9", " S. ", " S. ", "S. 1", "S. 2", "S. 3", "S. 4", "S. 5", "S. 6", "S. 7", "S. 8", "S. 9", " D. ", " D. ", "D. 1", "D. 2", "D. 3", "D. 4", "D. 5", "D. 6", "D. 7", "D. 8", "D. 9", "L. 1", "L. 2", "L. 3", "L. 4", "L. 5", "L. 6", "L. 7", "L. 8", "L. 9", "H. 1", "H. 2", "H. 3", "H. 4", "H. 5", "H. 6", "H. 7", "H. 8", "H. 9", " RV. ", " RV. ", "Grave", "Largo", "Lento", "Adagio", "Andante", "Allegretto", "Allegro", "Vivace", "Presto", "Prestissimo", " assai ", " con ", " brio ", " non ", " troppo ", " molto ", " poco ", " più ", " meno ", "Symphony", "Orchestra", "Chamber", "Woodwind", "Woodwinds", "Brass", "Strings", "String", "Percussion", " Solo ", " Duet ", " Trio ", " Quartet ", " Quintet ", " Sextet ", " Septet ", " Octet ", " / ", "Soprano", "Alto", "Tenor", "Baritone", "Bass", "Piccolo", "Flute", "Clarinet", "Oboe", "English", "Horn", "Bassoon", "Saxophone", "Trumpet", "Trombone", "Euphonium", "Cornet", "Piccolos", "Flutes", "Clarinets", "Oboes", "Horns", "Bassoons", "Saxophones", "Trumpets", "Trombones", "Euphoniums", "Cornets", "Flugel", "Tuba", "Sousaphone", "Violin ", "Tubas", "Sousaphones", "Violins ", "Viola ", "Cello", "Bass", "Piano", "Organ", "Harpsichord", "Guitar", "Pianos", "Organs", "Harpsichords", "Guitars", " Lute", "Recorder", "Recorders", "Vol.", "Vol. ", "Concerto", "Concerti", " ", "", "", "", "'", "(", ")", " (", "):", " ", "t ", "'s ", "'d ", "(A", "(B", "(C", "(D", "(E", "(F", "(G", "(H", "(I", "(J", "(K", "(L", "(M", "(N", "(O", "(P", "(Q", "(R", "(S", "(T", "(U", "(V", "(W", "(X", "(Y", "(Z", ",", " ", "1, No. ", "2, No. ", "3, No. ", "4, No. ", "5, No. ", "6, No. ", "7, No. ", "8, No. ", "9, No. ", "No.", "IX. ", "IV. ", "XV. ", " V. ", "...", "D Minor", ": The", "B-Flat", "E-Flat", "A-Flat", "D-Flat", "G-Flat", "Act", "No.", " Nos. ", ". Con ", ". Molto ", "Sonata", "", "Variation ", "Variation", "Variations", "Variations", "I. ", "II. ", "III. ", "IV. ", "V. ", "VI. ", "VII. ", "VIII. ", "IX. ", "X. ", "XI. ", "XII. ", "XIII. ", "XIV. ", "XV. ", "XVI. ", "XVII. ", "XVIII. ", "XIX. ", "XX. ", "XXI. ", "XXII. ", "XXIII. ", "XXIV. ", "XXV. ", "XXVI. ", "XXVII. ", "XXVIII. ", "XXIX. ", "XXX. ", "XXXI. ", "XXXII. ", "XXXIII. ", "I. ", "II. ", "III. ", "Z. 1", "Z. 2", "Z. 3", "Z. 4", "Z. 5", "Z. 6", "Z. 7", "Z. 8", "Z. 9", " l'", "1, No. ", "2, No. ", "3, No. ", "4, No. ", "5, No. ", "6, No. ", "7, No. ", "8, No. ", "9, No. ", ": A", ": P", ": T", ": O", ": I", ": F", ": Assai", ": Con", ": Non", ": Molto", ": Poco", ": Più", ": Meno", ". The ", ": The ", " E-Flat, ", "II. ", "III. ", "IV. ", "VI. ", ", Sz. 1", ", Sz. 2", ", Sz. 3", ", Sz. 4", ", Sz. 5", ", Sz. 6", ", Sz. 7", ", Sz. 8", ", Sz. 9", ", B. 1", "I. ", "II. ", "III. ", "IV. ", "Atto Primo", "Atto Secondo", "Atto Terzo", " I.", " II.", " III.", " IV.", ". A", ", SWV ", ". Poco", "Symphony ", "Minor ", "Minor ", "Major ", "Major ", " Major", " Minor", " Major,", " Minor,", "A Minor", "B Minor", "C Minor", "D Minor", "E Minor", "F Minor", "G Minor", "A Major", "B Major", "C Major", "D Major", "E Major", "F Major", "G Major", "A Minor", "B Minor", "C Minor", "D Minor", "E Minor", "F Minor", "G Minor", "A Major", "B Major", "C Major", "D Major", "E Major", "F Major", "G Major", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "Act 1", "Act 1", "Act 2", "Act 3", "Act 4", "Parte Prima,", "Parte Seconda,", "Parte Terza,", ": ", "K. Anh", " in D", ". ", ", H. ", ", D. ", ", BWV ", ", D. ", ", BWV", " Minor:", " Major:", ", H. ", " BWV "}

set the_string to the clipboard
repeat with i from 1 to (count search_strings)
	set the_string to my snr(the_string, item i of search_strings, item i of replace_strings)
end repeat
set the clipboard to the_string

end tell
on snr(the_string, search_string, replace_string)
tell (a reference to my text item delimiters)
set {old_atid, contents} to {contents, search_string}
set {the_string, contents} to {the_string’s text items, replace_string}
set {the_string, contents} to {“” & the_string, old_atid}
end tell
return the_string
end snr

Here is some sample text of what I am trying to edit:
<track_title>1. Allegro moderato</track_title>
<track_version_title></track_version_title>
<track_hierarchy_title>Symphony for strings No.9 in c minor</track_hierarchy_title>
<asset_type>AUDIO</asset_type>
<track_length>0:07:30</track_length>
<track_label>Decca</track_label>
<track_explicit_lyrics></track_explicit_lyrics>
<track_genre>Classical</track_genre>
<track_e_marketing_tag></track_e_marketing_tag>
<track_owning_label></track_owning_label>

Any Suggestions would be great!

Brian

Those can be simplified:

keystroke "a" using command down

You could also use this to get the text:

tell application "BBEdit"
	activate
	
	if exists front text document then
		set the_string to contents of front text document
	else
		display dialog "There isn't an open text document to read from." buttons {"Cancel"} default button "Cancel" with icon note
	end if
end tell

Thanks for the response Bruce…

This helps out a bit, but what I am really after is to get the script to search and replace the text in the XML document. Any Suggestions there?

Brian

I don’t have BBedit to test with so what part is actually not working?

Also is there a reason you have to use BBedit? If it’s a XML file why not just read the file using AS?

TextWrangler should also work.

Yeah I’m a TW user (though Text Mate is my fav), but despite their common heritage I didn’t know if their GUI / Dictionaries would be the same.

Also, James, BBEdit isn’t needed for the replacing that he’s having trouble with.

brianbjames, offhand, there might be some issues with case sensitivity…

Guess thats my question what is the problem. The script seems to work when I run a non BBedit version of it… Then again with 617 search terms I may be missing something it’s hard to verify.

Hi Fellas…

Thank you so much for your help!
This thing totally works now! I was forgeting to paste back to my BBEdit document. :smiley:

Brian

LOL, well glad ya got it Brian. Like I asked though is there any particular reason you are using BBedit when you could just do this natively in AppeScript.

I would guess that he’s running this from either BBEdit’s script menu or Apple’s script menu.