Automator Using Numbers in Exponential Form – Fix Guide

applescriptautomator

I created this code that should launch whatsapp with a specific number in clipboard:

on run {input, parameters}
    set text1 to the clipboard
        set text2 to 1 - text1 as real
        do shell script "open https://api.whatsapp.com/send?phone=971" & 1 - text2
    return input
end run

*

The subtractions are added to copy the correct number into the form

*

However, the phone number is stated in an exponential form which gives an error.

Example: 0501234567
Expected Output: 971501234567
Actual  Output : 9715.01234567E+8

How do I fix this?

Best Answer

A simple and fast way requires to install a scripting edition Satimage.osax (direct d/l) and works with regex. The certificate of the pkg sadly expired!

on run {input, parameters}
    set text1 to change "^0+" into "" in (the clipboard as string) with regexp
    do shell script "open https://api.whatsapp.com/send?phone=971" & text1
    return input
end run

^0+ strips off leading zeros!


A second one with sed but no additional installs:

on run {input, parameters}
    set text1 to do shell script "echo " & quoted form of (the clipboard as string) & " | sed 's/^0*//'"
    do shell script "open https://api.whatsapp.com/send?phone=971" & text1
    return input
end run
  • quoted form of (the clipboard as string): '0501234567'
  • do shell script "echo " & '0501234567' & " | sed 's/^0*//'": run shell command echo '0501234567' | sed 's/^0*//' in an Apple script
  • echo '0501234567' | sed 's/^0*//': send the output of echo to the stream editor sed and do something with it
  • ^0*: regular expression: ^ = start of line * = quantifier- matches between zero and unlimited times, as many times as possible
  • 's/^0*//': 's/reg_ex/replacement/': substitute the replacement string for the first instance of the regular expression in the pattern space. This means: replace as many leading zeros as possible with the replacement string (=NIL/nothing) = strip off leading zeros
  • set text1 to ...: $text1=501234567
  • do shell script "open https://api.whatsapp.com/send?phone=971" & text1: open https://api.whatsapp.com/send?phone=971501234567

Both tested in 10.11.6 (El Capitan) only.