title: Formating Phone Numbers in Applescript
body: <p>I am trying to create an Automator task that uses an applescript to format a phone number into the format (XXX)XXX-XXXX</p>
  
  <pre><code>on clean_number(phoneNumber)
      set newString to {}
      repeat with c in the characters of phoneNumber
          set c to c as text
          if c is not "-" then
              if c is not " " then
                  set the end of newString to c
              end if
          end if
      end repeat
      return newString
  end clean_number
  
  on run {input, parameters}
      set originalText to clean_number(input) as string
      if length of originalText is 10 then
          set areaCode to items 1 thru 3 of originalText as string
          set localExchange to items 4 thru 6 of originalText as string
          set phoneNumber to items 7 thru end of originalText as string
          set result to "(" &amp; areaCode &amp; ")" &amp; localExchange &amp; "-" &amp; phoneNumber as string
          return result
      end if
  end run
  </code></pre>
  
  <p>when I run this I get no result. I have tested out the run function individually and it works. The problem I am getting seems to be in the repeat, because when I try to return c from inside the repeat I get no result</p>
  
  <p>edit:
  As iayork recommended I tried running the script through terminal, and it worked! This implies that the problem lies somewhere in the way Automator is handling the script.</p>