Applescript – Converting Uppercase & Lowercase (AppleScrunix Style)
Thursday 26 May 2011 - Filed under automation + gaps + Technology
As there are no built-in routines for changing the case of characters in AppleScript, the coder is required to add their own handlers. The following script (See credits below) shows one way to use AppleScript to change the names of files in a folder from lowercase to uppercase & vise-versa.
Begin Script…
tell application "Finder" to set the source_folder to choose folder
tell me to activate
display dialog "Change case to:" buttons {"Cancel", "UPPER", "lower"}
set the button_pressed to the button returned of the result
tell application "Finder"
repeat with this_item in entire contents of source_folder
set the current_name to the name of this_item as text
if the button_pressed is "lower" then
set the name of this_item to my change_case ( the current_name , "lower")
else
set the name of this_item to my change_case ( the current_name , "upper")
end if
end repeat
end tell
display dialog "Process complete"
-- AppleScript 'change_case' handler
on change_case ( this_text , this_case )
if this_case is "lower" then
set the comparison_string to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set the source_string to "abcdefghijklmnopqrstuvwxyz"
else
set the comparison_string to "abcdefghijklmnopqrstuvwxyz"
set the source_string to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
end if
set the new_text to ""
repeat with thisChar in this_text
set x to the offset of thisChar in the comparison_string
if x is not 0 then
set the new_text to ( the new_text & character x of the source_string ) as string
else
set the new_text to ( the new_text & thisChar ) as string
end if
end repeat
return the new_text
end change_case
…End Script
The following handler ‘change_case’ was rewritten using AppleScrunix. Not only is there less code, but the script runs faster. Replace the AppleScript version of the handler in the above script with the unix based handler below.
Begin Handler…
on change_case ( this_text , this_case )
if this_case is "lower" then
set the new_text to do shell script "echo " & quoted form of ( this_text ) & " | tr A-Z a-z"
else
set the new_text to do shell script "echo " & quoted form of ( this_text ) & " | tr a-z A-Z"
end if
return the new_text
end change_case
…End Handler
The above AppleScrunix example uses shell command ‘tr’ to translate uppercase to lowercase, etc. Refer to the tr Mac OS X Man (Manual) Page for other uses.
Credits: I picked up the initial example script almost verbatim from Apple Support Communities. It was posted by a user I only know as V.K., on April 16, 2009 @ 10:13 am. ~Thanks VK!
AppleScript / AppleScrunix Examples – using the do shell script command in AppleScript.
2011-05-26 » Russ Leseberg
30 June 2011 @ 9:14 am
[...] If you are interested in solving this problem just using AppleScript, you can get much of the code you need from my previous post, Applescript – Converting Uppercase & Lowercase (AppleScrunix Style). [...]
27 September 2012 @ 5:52 pm
To work properly with non-English locales too, more effort is needed; the following two example subroutines should work:
on toUpper(s)
tell AppleScript to return do shell script "shopt -u xpg_echo; export LANG='" & user locale of (system info) & ".UTF-8'; echo " & quoted form of s & " | tr [:lower:] [:upper:]"
end toUpper
on toLower(s)
tell AppleScript to return do shell script "shopt -u xpg_echo; export LANG='" & user locale of (system info) & ".UTF-8'; echo " & quoted form of s & " | tr [:upper:] [:lower:]"
end toLower
Note:
• `shopt -u xpg_echo` turns off interpretation of ‘\’-letter combinations, e.g. ‘\n’, as escape sequences (required, because `do shell script` invokes the shell as `sh`, causing it to run in POSIX compatibility mode, where – unlike when running as `bash` – interpretation of escape sequences is ON by default.)
• `export LANG=…` sets the locale to the one matching the current user’s – `do shell script` assigns the generic “C” locale by default.
27 September 2012 @ 7:37 pm
Nice solution, thanks for sharing
27 September 2012 @ 11:33 pm
My pleasure; I actually found a slightly more concise solution that obviates the need to set a shell option and uses input redirection (feel free to delete my original post; I’m done tweaking now:))
To work properly with non-English locales too, more effort is needed; the following two example subroutines should work:
on toUpper(s)
tell AppleScript to return do shell script "export LANG='" & user locale of (system info) & ".UTF-8'; tr [:lower:] [:upper:] <<< " & quoted form of s
end toUpper
on toLower(s)
tell AppleScript to return do shell script "export LANG='" & user locale of (system info) & ".UTF-8'; tr [:upper:] [:lower:] <<< " & quoted form of s
end toLower
Note:
• `export LANG=…` sets the locale to the one matching the current user’s with UTF-8 encoding – by default, `do shell script` assigns the generic “C” locale.