Hi,
i’ve a huge amount of files which are all UPPERCASE.
I need to change them all so that the first letter is uppercase and all following letters become lowercase (FOOBAR - > Foobar)
Does anyone know if there’s a way to do this?
Thanks!
Hi,
i’ve a huge amount of files which are all UPPERCASE.
I need to change them all so that the first letter is uppercase and all following letters become lowercase (FOOBAR - > Foobar)
Does anyone know if there’s a way to do this?
Thanks!
This is a quickie that works for file names with no more than one space in them. It could certaily be made more efficient.
set theFileName to "FOOBAR QUIBBLE"
set SP to offset of space in theFileName
set NewName to ""
repeat with j from 1 to count of words of theFileName
set Caps to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set FChars to characters of word j of theFileName
considering case
repeat with k from 2 to count of FChars
if item k of FChars is in Caps then set item k of FChars to ASCII character ((ASCII number of item k of FChars) + 32)
end repeat
end considering
set NewName to NewName & FChars as string
end repeat
if SP = 0 then
set theFileName to NewName
else
set theFileName to (text items 1 thru (SP - 1) of NewName & space & text items SP thru -1 of NewName) as string
end if
theFileName
--> "Foobar Quibble"
Hi,
Did you mean that you wanted to change/keep the first character of every word of the name to uppercase or did you mean that you wanted to change/keep just the first word of the name to upper case while making the case of all other characters lowercase?
gl,
I think he’s looking for title case (Like This). Perl can do this really easily:
Therefore (with the appropriate escaping):
set titleCaseString to (do shell script "/bin/echo" & space & quoted form of theString & space & "| /usr/bin/perl -p -e 's/(\\w+)/\\u\\L$1/g;'") as Unicode text
Hi Mikey-San,
The one thing that’s bad about unix code is that you can’t do anything after that. don’t you think it’s very hard to learn anything from that. That was the point of AppleScript. So that users could manipulate things and not have a high learning curve…
That’s some of the things that made macintosh computers user friendly. What do you think?
gl,
Personally, if I saw something like that, I’d give up AppleScript and take up Python or somother scripting language.
So I’m not allowed to post solutions that can be integrated into AppleScript unless they’re vanilla AppleScript? The big AppleScript solution isn’t necessarily a better learning/teaching tool. It might be, totally, and it might not be–but don’t assume that it is just because it doesn’t use strange regular expressions and has a different learning curve[1]. Adam’s script is clever, don’t get me wrong. I simply offered a less verbose way that might interest someone. More people than simply red shirts search these message boards for solutions to problems, you know.
Look at all of the stuff in the big AppleScript solution for which a newbie might have to consult documentation anyway:
offset
moderately complex repeat control statement with nested companion repeat
considering
nested and verbose string character evaluations and manipulations
Is there something wrong with posting an AppleScript + shell script solution here? Should I stop offering alternative code if it might have a learning curve? Maybe someone will read this thread and go, “Hey, that looks really powerful. I should dig into this stuff.” Maybe that person will ask questions. Some coders will stop at the vanilla AppleScript method and be just fine; others will have their interests piqued in the terse pancea.
Don’t kid yourself: AppleScript does not always scream “user friendly”. Apple documents it very inconsistently, there are too many syntax oddities and statement structure variants (is, equals, is equal to, = . . . do we need this many evaluators?), and it’s also often too verbose for its own good.
This having been said, I don’t have anything against AppleScript. I happen to make a good living that involves making things with AppleScript. But there’s always another way to skin a walnut, and perl can get along with AppleScript just fine when needed.
Maybe the original poster will like the do shell script/perl method. Maybe not. Why omit the solution? Readers of the thread now have two solutions and can use whatever works best for their needs, and perhaps challenge themselves with whichever method is least familiar.
[1] I say “different learning curve” because, honestly, when you step back from the AppleScript language for a second, you can see just how obtuse it can be. No easier to learn than regular expressions.
Admin Edit: This statement refered to a post that has been deleted.
No don’t get me wrong. I think that there are a lot of unix commands that add to the AppleScript vocab. But, sometimes using unix instead of AppleScript makes posters think that the learning curve for AppleScript is high when that is not true.
To Jump to a unix solution, when there is a simpler AppleScript soution, “turns off” new potential AppleScirpters because of the higher learning curve.
So, if one can find a simpler way, I feel that one should post that. Otherwise, go for whatever solution you have.
I been drinking so might have not been tactful in my post and sorry if that was the case, but am so sorry, but that’s the way I fee. What do you think? Am I an ass.![]()
It’s not simpler. It’s just verbose. There’s a difference. If someone gets “turned off”, that’s fine, because they already have a solution that suits their tastes; others may gain something from the alternate method. I am confused, however, by the “you can’t do anything after that” statement. You can simply use the resulting string as the file name, like you do in the vanilla example.
Not posting a learnable, useful, and powerful solution just because it’s complex is like saying, “Don’t use big words because your kids won’t want to be smarter.” Again, lots of different people read and search these boards. Someone who wants to skip a “do shell script” command, will.
Don’t post while drinking. Please. Ugh.
I’ve altered the non-perl script in a way to select a folder with the filenames to change.
here’s what i’ve got so far:
set f to choose folder with prompt "Choose a folder with filenames to change"
tell application "Finder" to repeat with i in (get f's files)
tell i to set theFileName to (get name)
tell i to set SP to offset of space in theFileName
set NewName to ""
repeat with j from 1 to count of words of theFileName
tell i to set Caps to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
tell i to set FChars to characters of word j of theFileName
considering case
repeat with k from 2 to count of FChars
if item k of FChars is in Caps then set item k of FChars to ASCII character ((ASCII number of item k of FChars) + 32)
end repeat
end considering
tell i to set NewName to NewName & FChars as string
end repeat
if SP = 0 then
tell i to set theFileName to NewName
else
tell i to set theFileName to (text items 1 thru (SP - 1) of NewName & space & text items SP thru -1 of NewName) as string
tell i to set name to theFileName as string
end if
end repeat
Although i don’t get any errors while executing the script and the filename shows correctly changed in the event-log of the scripteditor, the files in the chosen folder remain untouched…
Does anything know what i’ve done wrong?
Thanks!
Some lessons to be learned here. First, a script that works for me (again not optimized). This one takes account of the possiblity that there’s an extension attached to the file name, and the AppleScript noun “word” will count that as a separate word and lose the period.
set f to choose folder with prompt "Choose a folder with filenames to change"
-- a little insurance (creates copy of original in same location as original just in case there is a glitch; delete when all is well)
tell application "Finder"
duplicate f -- a little insurance (creates copy of original in same location as original just in case there is a glitch; delete when all is well)
set theFiles to f's files -- ignores folders
repeat with ff from 1 to count of theFiles
set theFileName to name of item ff of theFiles
set SP to offset of space in theFileName
set Ext to offset of "." in theFileName
if Ext = 0 then
set baseName to theFileName -- no extension
else
set baseName to characters 1 thru (Ext - 1) of theFileName as string
set Extension to characters Ext thru -1 of theFileName as string
end if
set NewName to ""
repeat with j from 1 to count of words of baseName
set Caps to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set FChars to characters of word j of baseName
considering case
repeat with k from 2 to count of FChars
if item k of FChars is in Caps then set item k of FChars to ASCII character ((ASCII number of item k of FChars) + 32)
end repeat
end considering
set NewName to NewName & FChars as string
end repeat
if SP = 0 then
set baseName to NewName as string
else
set baseName to (text items 1 thru (SP - 1) of NewName & space & text items SP thru -1 of NewName) as string
end if
if Ext = 0 then
set name of item ff of theFiles to baseName
else
set name of item ff of theFiles to baseName & Extension
end if
end repeat
end tell
Now the lessons:
In my view, there is a constituency for both forms here. This and other forums attract both beginners and experts. For those of us, like me, who are neither beginners nor experts, we’re always interested in augmenting our toolboxes for efficient ways to do things in plain vanilla AppleScript, with the help of available FBAs and OSAXen, and with the application of core functions invoked through shell scripts. Mikey-San’s solution in Perl could have been done with sed as well, I suspect.
Sensing that Pulse00 was not an expert scripter (not intended to be insulting at all - welcome to this forum), I wrote the scripts that appear in this thread as a plain vanilla example that I knew would run in almost any AppleScript version. It reflects my own level of expertise as a relative newbie to scripting myself, but not a rank beginner.
If I were doing this myself, I’d probably use Mikey-San’s version (and have used several other shell scripts he’s posted), because he’s posted it before and I have it on file. I could not compose it, however, because my knowledge of regex is rudimentary and I don’t know Perl (which is the ultimate regex engine as I understand it). To me they’re tools.
For those who’ve come at AppleScript from the *NIX world, this might be of interest: A unix whiz’s take on AppleScript.
if the only thing you need is to change case of files, might you consider R-name? its free.
sorry i know this is the programming forum, but it seems much simpler to download a free program if there’s one already available.
http://www2.mitsuya.nuem.nagoya-u.ac.jp/~tagaya/mte/archives/my_software/rname/index.html
if the only thing you need is to change case of files, might you consider R-name? its free.
sorry i know this is the programming forum, but it seems much simpler to download a free program if there’s one already available.
http://www2.mitsuya.nuem.nagoya-u.ac.jp/~tagaya/mte/archives/my_software/rname/index.html
Ahh, but where’s the fun in that? One of the “downsides” of learning AppleScript is that you often solve problems using a script for which there’s a ready-made solution out there because you don’t even look for it - you just put fingers to keyboard.
if the only thing you need is to change case of files, might you consider R-name? its free.
sorry i know this is the programming forum, but it seems much simpler to download a free program if there’s one already available.
http://www2.mitsuya.nuem.nagoya-u.ac.jp/~tagaya/mte/archives/my_software/rname/index.html
How does that help anyone who needs to add a function to a program he or she is writing, though? That’s the biggest benefit of developing and sharing code on a public forum.
I hate to sound like a broken record, but lots of different people with lots of different needs read the boards here. ![]()