changing case in TextEdit

Yes, that simple!
I’m still new to this. What’s the best way to script a change from Upper Case to Lower Case throughout a .txt document?
Thanks!
AB

This should work:

set s to read file "path:to:textfile"
set lowcase to do shell script "echo '" & s & "'|tr '[A-Z]' '[a-z]'"
write lowcase to file "path:to:textfile"
  • Dan

Well, on second thought it will work as long as there’s no single quote in the text. Otherwise

set s to read file "path:to:textfile"
set lowcase to ""
repeat with c in s
	set n to ASCII number (c)
	if n ≥ 65 and n ≤ 90 then set c to ASCII character (n + 32)
	set lowcase to lowcase & c
end repeat
write lowcase to file "path:to:textfile"

should do it.

  • Dan

There’s a couple of bugs in that (not using quoted form of s, not opening file with write access), plus the usual size limit on passing input data in a shell command.

Note that “tr ‘[A-Z]’ ‘[a-z]’” is only good for converting plain ASCII files, which, btw, you can do completely from the shell with much less fuss:

tr '[A-Z]' '[a-z]' < oldfile.txt > newfile.txt

For other text encodings (e.g. unicode), you really need to do it some other way. Using a decent text editor like TextWrangler is one option, and there are other ways if needed. (The OP would need to indicate the file encoding[s] they’re dealing with for more specific suggestions.)

HTH

Wait, Standard Additions’ ‘write’ command does seem to write to existing files even without using ‘open for access f with write permission’ first (smells dodgy to me, but that’s AppleScript for ya). So technically you can scratch that in this particular case.

In its place, here’s a different bug: your AS code reads the file as string and writes it as unicode (UTF16). The ‘do shell script’ command returns a Unicode text value, so you need to use ‘write lowcase to file … as string’ to preserve the file’s original encoding.

HTH

I’d like to learn here…How does surrounding the string with single quotes differ from “quoted form of”? Also, what is the advantage of opening the file with write access (when the above works as is)? Also, I’m aware that this can be done completely in the shell, but I was assuming an AppleScript solution here.

Thanks…

  • Dan

Our posts crossed. Your UTF16 comment is duly noted - should have been “as string”…
Thanks…

  • Dan

This should work a little better:

property desiredCase : "lower"

on run
	choose file with prompt "Choose a text file to change case of:" with multiple selections allowed without invisibles
	open (result)
end run

on open droppedItems
	display dialog "Change case to." buttons {"Cancel", "upper", "lower"} default button desiredCase
	set desiredCase to button returned of result
	
	repeat with thisItem in droppedItems
		changeCase(POSIX path of thisItem)
	end repeat
	beep
end open

on changeCase(thisFile)
	try
		do shell script "python -c \"import sys; print unicode(sys.argv[1], 'utf8')." & desiredCase & "().encode('utf8')\" \"`cat " & quoted form of thisFile & "`\" > " & quoted form of thisFile
	on error errorMsg
		display dialog errorMsg buttons "Cancel" default button 1
	end try
end changeCase

Model: Mac mini
AppleScript: 1.10
Browser: Safari 412
Operating System: Mac OS X (10.4)

Using the string’s ‘quoted form’ will quote it correctly for the shell; this includes escaping any quote marks it contains. Using “echo '”& s & “'” won’t, and will blow up horribly if the string contains any quote marks. Apart from the reliability issue it’s also a horrendous security hole; e.g. try:

set s to "'; echo 'You idiot!'; echo '"
do shell script "echo ' " & s & " ' "

Now imagine if the string contained something really nasty like “'; rm -rf .*; echo '” (DO NOT TRY THIS!!!): it could wipe all the files off your hard drive before you knew what was going on. There really should be a compulsory health warning on that ‘do shell script’ command; so many folk just don’t realise how damned dangerous it is if used carelessly.

Bit simpler:

do shell script "python -c \"import sys; print unicode(sys.stdin, 'utf8')." & desiredCase & "().encode('utf8')\" < " & quoted form of inputFilePath & " > " & quoted form of outputFilePath

Also, note that that Python script is only suitable for processing ASCII and UTF8 data. (ASCII is a subset of UTF8.) You might want to parameterise it so the user can specify the encoding themselves.

HTH

Thanks for the discussion on the topic. I clearly have a ways to go. My shell scripting is elementary at best.
guardian34’s AppleScript is a charm!

Model: Powerbook G4
Browser: Safari 125.12
Operating System: Mac OS X (10.4)