I made a script to add line breaks at the end of paragraphs in files inside a folder, for posting on a website. After batch proccessing about 12 folders, I now have a bunch of files with the line break I wanted, but all the files also contain invisible control characters that show up in my browser. Here’s the script I used:
set theFolder to choose folder
tell application "Finder"
try
set fileList to every file of folder theFolder as alias list
on error number -1700
set fileList to first file of folder theFolder as alias as list
end try
end tell
tell application "Finder"
repeat with theFile in fileList
set theContents to read theFile
set theParagraphs to every paragraph of theContents as list
set newContents to ""
repeat with thisParagraph in theParagraphs
if "<br>" is not in thisParagraph then
set thisParagraph to thisParagraph & "<br>" as string
end if
set newContents to newContents & thisParagraph & return as string
end repeat
set f to (open for access (theFile) with write permission)
set eof of f to 0
write newContents to f as Unicode text
close access f
end repeat
end tell
beep 2
How did the control characters get there? Is there a problem with the syntax of the script?
…And more importantly:
Is there a way to use applescript to zap gremlins in many files at a time? I know I can use BBEdit, but there doesn’t seem to be a way to zap gremlins in many files at once.
Thanks in advance for any help/info.
-G
Browser: Firefox 3.0.3
Operating System: Mac OS X (10.4)
You’re reading text from the file as though it’s a string (the default for ‘read’) but writing it back as Unicode text. Might that have something to do with it?
If the file contents were Unicode text to begin with, you should have used this to read them:
Thanks for the reply. I guess that was a pretty stupid mistake on my part. Do you know of a good way to fix the problem in over 300 files without going through them one at a time? I’m afraid I really screwed up this time.
It looks like I found a fix for my problem using TextWrangler.
Here’s the script I made to batch-zap those pesky gremlins in case someone else runs into the same problem:
set theFolder to choose folder
tell application "Finder"
try
set fileList to every file of folder theFolder as alias list
on error number -1700
set fileList to first file of folder theFolder as alias as list
end try
end tell
repeat with theFile in fileList
set theName to name of (info for theFile)
tell application "TextWrangler"
activate
open theFile with LF translation
zap gremlins text 1 of text document theName zap action delete_gremlin with non ASCII characters and controls
close text window 1 with saving
end tell
end repeat
Thanks for posting your solution. I had an opportunity to set up some test files and try it out last night. It appears to work well.
One formality: the dictionary for TextWrangler 2.1.2 says that value of the ‘saving’ parameter for the ‘close’ command is ‘yes/no/ask’, not a boolean, so ‘close text window 1 with saving’ should be ‘close text window 1 saving yes’.
The dictionary doesn’t say so, but TextWrangler’s ‘open’ command returns a reference to the opened document. Using this would save having to get the name from the file, which would speed things up a little:
-- Get the alias list here, then:
repeat with theFile in fileList
tell application "TextWrangler"
activate
open theFile with LF translation
zap gremlins text 1 of result zap action delete_gremlin with non ASCII characters and controls
close text window 1 saving yes
end tell
end repeat
Thanks for the info. I just upgraded TextWrangler after creating and using the script that I posted.
I’ll make the changes, thanks to your post. I’m glad I remained subscribed to this topic.
These boards are extremely helpful.
-G