Hi,
First of all I am an absolute beginner in scripting, still I thought there is nothing easier than to record a set of repeating steps in TextWrangler.
The task is the following: I need to replace a number of characters and special characters in a file opened by TextWrangler. Characters in Western Encoding are for example something like “Ÿ” that should be replaced by “z”, “ss” to be replaced by “ß”, a " (quote sign) replaced by nothing (or dimply deleted), a tab to be replaced by ", " (comma and space). I am dealing with a large number of documents daily that contain characters to be replaced, so I wanted to automatize the process.
I opened a sample text in TextWrangler: “Amro, Versailles” wanting to replace quotes with nothing.
So I recorded just one step: the replacement of a quote by nothing. This is how the recorded script looks like:
tell application “TextWrangler”
activate
open find window
replace “"” using “” searching in text 1 of text document “Amro.asc” options {starting at top:true, case sensitive:true}
close find window saving no
end tell
But running the above very simple script I got the following error message:
TextWrangler got an error: “"” doesn’t understand the “replace” message.
So I got stuck. I do not know how to continue.
I would appreciate if you could give me a simple sample of how the script should like like where I could easily insert the characters that should be replaced, and of course the replacements.
I’ve searched the forum looking for character replacement, but none of the described solutions helped me.
Thank you.
I created a fake document with the contents of your message then ran :
tell application "TextWrangler"
activate
open find window
replace "\"" using "" searching in text 1 of text document 1 options {starting at top:true, case sensitive:true}
close find window saving no
end tell
which did the job flawlessly.
Yvan KOENIG running Sierra 10.12.0 in French (VALLAURIS, France) jeudi 13 octobre 2016 09:56:28
Thank you, Yvan, this works perfectly.
Now I tried to add a list of new commands in order to replace "(, " (opening bracket comma space) with single “(” (opening bracket). Here it is:
tell application "TextWrangler"
activate
open find window
replace "(, " using "(" searching in text 1 of text document 1 options {starting at top:true, case sensitive:true}
close find window saving no
end tell
It stopped working.
There are many different characters and special characters that should be replaced on daily basis in multiple documents, so I need a scripti where I can simple add characters to be replaced and their replacement.
Since I am not an expert, I tried repeating the commands:
tell application "TextWrangler"
activate
open find window
replace " " using ", " searching in text 1 of text document 1 options {starting at top:true, case sensitive:true}
close find window saving no
end tell
tell application "TextWrangler"
activate
open find window
replace " " using " " searching in text 1 of text document 1 options {starting at top:true, case sensitive:true}
close find window saving no
end tell
tell application "TextWrangler"
activate
open find window
replace ", , " using ", " searching in text 1 of text document 1 options {starting at top:true, case sensitive:true}
close find window saving no
end tell
tell application "TextWrangler"
activate
open find window
replace "(," using "(" searching in text 1 of text document 1 options {starting at top:true, case sensitive:true}
close find window saving no
end tell
But it does not work.
Sorry to bother you, but could you please suggest a proper formula?
Thank you very much again.
Hi Vitruvian. Welcome to MacScripter.
Your script works perfectly on my system (TextWrangler 5.5.2, Mac OS 10.11.6), although because it’s recorded (which records what you do rather than just what TextWrangler needs to do), a couple of the lines are superfluous:
tell application "TextWrangler"
activate
replace "\"" using "" searching in text of text document "Amro.asc" options {starting at top:true, case sensitive:true}
end tell
I can reproduce your error exactly by running the script when the document isn’t open. Is that what’s happening in your case? It’s a strange and unhelpful error message if it is.
This version below asks you to choose the file, opens it as a document in TextWranger, does the edit, and saves and closes the document:
set docFile to (choose file)
tell application "TextWrangler"
activate
set thisDocument to (open docFile)
replace "\"" using "" searching in thisDocument options {starting at top:true, case sensitive:true}
close thisDocument saving yes
end tell
it’s possible to write code to do the same thing directly in AppleScript without the need for TextWrangler. It’s several lines longer, but it’s faster simply because there’s no application being scripted. However, that’s probably enough information until the cause of your error’s confirmed.
Hi Nigel,
Thank you for the warm welcome and your reply.
My version of TextWrangler is also 5.5.2 running on MacOS Sierra (10.12).
Unfortunately I got the described error in post #1 on an open document. Running your script the quotes in the text were not deleted.
The script added by Yvan Koenig in post #2 is perfect. But I got stuck. I tried to add similar commands in order to replace other characters, too, but that is not working again. See my post #3.
So I do not know how to continue.
You may try to run :
set thePairs to {{"\"", ""}, {"(, ", "("}, {"(,", "("}, {" ,)", ")"}, {",)", ")"}, {" ", ", "}, {" ", ", "}, {", , ", ", "}}
tell application "TextWrangler"
activate
open find window
repeat with aPair in thePairs
replace aPair's item 1 using aPair's item 2 searching in text 1 of text document 1 options {starting at top:true, case sensitive:true}
end repeat
close find window saving no
end tell
Yvan KOENIG running Sierra 10.12.0 in French (VALLAURIS, France) jeudi 13 octobre 2016 19:00:28
Merci, Yvan, I’ll try this script.