Alternative: Since Automator supports other interpreters too, there is no strict need to use AppleScript.
Here's an alternative solution that uses a Run Shell Script
action to combine bash
with awk
, which makes for much less code:
- Create an Automator service that:
- receives
selected text
inany application
- has check box
Output replaces selected text
checked - contains a
Run Shell Script
action with the code below
- receives
- and then invoke the service with the entire sentence ("I like pizza, put bacon on it!") selected.
awk -F ', ' '{
printf $1 # print the first "field"; lines without ", " only have 1 field
for (i=2; i<=NF; ++i) { # all other fields, i.e., all ", "-separated clauses.
# Join the remaining clauses with ". " with their 1st char. capitalized.
printf ". " toupper(substr($i,1,1)) substr($i,2)
}
printf "\n" # terminate the output with a newline
}'
Caveat: incredibly, awk
(as of OS X 10.9.2) doesn't process foreign characters correctly - passing them through unmodified works, but toupper()
and tolower()
do not recognize them as letters. Thus, this solution will not correctly work for input where a foreign char. must be uppercased; e.g.: "I like pizza, ṕut bacon on it!"
will NOT convert the ṕ
to Ṕ
.