I am writing a small program and I need to replace symbols such as “]” and “/” in a text file. How would I write a script that will take a text file, then replace these symbols for letters or numbers? I am hoping to do this script in automator, if that makes any difference.
Browser: Safari 525.13
Operating System: Mac OS X (10.4)
It’s not glamorous, but this works here on 10.4.11…
Peter B.
set text_example to "bit[of/text"
--set text_example to read (choose file)
set ASTID_Priors to AppleScript's text item delimiters
set AppleScript's text item delimiters to "["
set interim_result to every text item of text_example as list
set AppleScript's text item delimiters to " "
set interim_result to every text item of interim_result as text
set AppleScript's text item delimiters to "/"
set interim_result to every text item of interim_result as list
set AppleScript's text item delimiters to " "
set example_result to every text item of interim_result as text
set AppleScript's text item delimiters to ASTID_Priors
example_result --> "bit of text"
set the_list to {"/", "["}
set item_num1 to count of (items of the_list)
set text_example to "bit[of/text"
--set text_example to read (choose file)
set ASTID_Priors to AppleScript's text item delimiters
repeat with i from 1 to item_num1
set AppleScript's text item delimiters to item i of the_list
set interim_result to every text item of text_example as list
set AppleScript's text item delimiters to " "
set interim_result to every text item of interim_result as text
end repeat
set example_result to interim_result
set AppleScript's text item delimiters to ASTID_Priors
example_result --> "bit of text"
set the_list to {"[", "/", "_", "]"}
set item_num1 to count of (items of the_list)
set text_example to "bit[of/text_for]now"
--set text_example to read (choose file)
set ASTID_Priors to AppleScript's text item delimiters
repeat with i from 1 to item_num1
set AppleScript's text item delimiters to item i of the_list
set interim_result to every text item of text_example as list
set AppleScript's text item delimiters to " "
set interim_result to every text item of interim_result as text
set text_example to interim_result
end repeat
set example_result to interim_result
set AppleScript's text item delimiters to ASTID_Priors
example_result --> "bit of text"
Hi there, I have found this to be the most reliable especially for large files. It’s also the quickest in terms of processing, at least it is in my opinion but I’m happy for someone to show me a better way.
on run
set theString to "" as string -- The string you want to alter
set badChars to {"]", "/"} -- and whatever else you want in here
set newString to ""
set iChar to 1
repeat until iChar > (count of theString)
if badChars contains (item iChar of theString) then
set newString to newString & "" as string -- what you'd like to replace it with
else
set newString to newString & (item iChar of theString) as string
end if
set iChar to iChar + 1
end repeat
log newString
end run