Applescript: Split sentence at cursor - remove punctuation from selection, capitalize letter and insert period

StackOverflow https://stackoverflow.com/questions/23298541

  •  09-07-2023
  •  | 
  •  

Question

I'm new to AppleScript, but think it can automate a minor annoyance I sometimes deal with.

Lets say I write: "I like pizza, put bacon on it!" But I decide I want to split that into two sentences: "I like pizza. Put bacon in it!"

I'd like to be able to select the string ", p" And with a keyboard shortcut, have a script remove any punctuation, add a period, and capitalize the first letter.

I figured out how to set System Preferences > Keyboard to run an automator service, running an AppleScript, but I can't google enough info to create the script from scratch.

Any help or direction would be great!

Was it helpful?

Solution

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 in any application
    • has check box Output replaces selected text checked
    • contains a Run Shell Script action with the code below
  • 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 .

OTHER TIPS

If you:

  • create an Automator service that:
    • receives selected text in any application
    • has check box Output replaces selected text checked
    • contains a Run AppleScript action with the code below
  • and then invoke the service with the entire sentence ("I like pizza, put bacon on it!") selected

it should do what you want.

on run {input, parameters}

    # Initialize return variable.
    set res to ""

    # Split the selection into clauses by ", ".
    set {orgTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {", "}} #'
    set clauses to text items of item 1 of input
    set AppleScript's text item delimiters to orgTIDs #'

    # Join the clauses with ". ", capitalizing the first letter of all clauses but the first.
    repeat with clause in clauses
        if res = "" then
            set res to clause
        else
            set res to res & ". " & my capitalizeFirstChar(clause)
        end if
    end repeat

    # Return the result
    return res
end run

# Returns the specified string with its first character capitalized.
on capitalizeFirstChar(s)
    set {orgTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {""}} #'
    set res to my toUpper((text item 1 of s) as text) & text items 2 thru -1 of s as text
    set AppleScript's text item delimiters to orgTIDs #'
    return res
end capitalizeFirstChar

# Converts the specified string (as a whole) to uppercase.
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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top