1

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?

2
  • 1
    This is an XY-problem probably. The real question is: How do I strip off a leading 0 of a telephone number to get it to work with the whatsapp API in an Apple Script/Automator.
    – klanomath
    Commented Apr 14, 2020 at 15:28
  • If you have 0501234567 on the clipboard and your goal is to open this URL https://api.whatsapp.com/send?phone=971501234567", then simply, do shell script "open https://api.whatsapp.com/send?phone=971" & text 2 thru -1 of (the clipboard as string) should do it. Or am I missing something? Commented Apr 14, 2020 at 16:08

4 Answers 4

1

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.

3
  • I would really appreciate if you can explain how the second solution works exactly. It's perfect, but I don't know what it's doing
    – Render
    Commented Apr 15, 2020 at 18:56
  • 1
    In this case, quoted form of (the clipboard as string) is actually '0501234567' not "0501234567", as quoted form always uses single quotes. Not exactly sure why Apple did that, but probably to not allow variable expansion, or to avoid issues with shell special characters, or maybe it makes it easier for AppleScript to parse the do shell script command. Just guessing at logical reasons. Commented Apr 15, 2020 at 19:54
  • And that makes the whole difference between displaying the number in exponential form? Wow never knew that
    – Render
    Commented Apr 17, 2020 at 7:52
1

Looking at your example 0501234567 and your expected output 971501234567, then I'm assuming all you are really trying to do is strip the first character from 0501234567, of what's on the clipboard, and append 501234567 to the URL https://api.whatsapp.com/send?phone=971 so that you have https://api.whatsapp.com/send?phone=971501234567 as the URL for use with the open command in the do shell script command.

If I'm understanding your need correctly, then simply this is all you need:

do shell script ¬
    "open https://api.whatsapp.com/send?phone=971" & ¬
    text 2 thru -1 of (the clipboard as text)
  • Note that the use of the line continuation character ¬ is not necessary, I'm just using it so the whole command line shows without having to scroll.

The phone number e.g. 0501234567 on the clipboard after all is not a real number in the sense of an integer that mathematical calculations need to be performed on it, in this use case. It is simply a text string of numeric characters for the manner in which it needs to be used and should be expressly treated as such.

0

convertNumberToString

Apple's Mac Automation Scripting Guide contains the following code in the Manipulating Numbers section titled Converting a Long Number to a String:

In AppleScript, long numeric values are displayed in scientific notation. For example, 1234000000 is displayed by a script as 1.234E+9. When this value is coerced to a string, it becomes: "1.234E+9". The handler (below) in Listing 20-3 converts a number, regardless of length, to a string of numeric characters instead of a numeric string in scientific notation.

set myNumber to 1 - "0501234567" as real
set myResult to "971" & convertNumberToString(1 - myNumber)

-- https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/ManipulateNumbers.html
on convertNumberToString(theNumber)
    set theNumberString to theNumber as string
    set theOffset to offset of "E" in theNumberString
    if theOffset = 0 then return theNumberString
    set thePrefix to text 1 thru (theOffset - 1) of theNumberString
    set theConvertedNumberPrefix to ""
    if thePrefix begins with "-" then
        set theConvertedNumberPrefix to "-"
        if thePrefix = "-" then
            set thePrefix to ""
        else
            set thePrefix to text 2 thru -1 of thePrefix
        end if
    end if
    set theDecimalAdjustment to (text (theOffset + 1) thru -1 of theNumberString) as number
    set isNegativeDecimalAdjustment to theDecimalAdjustment is less than 0
    if isNegativeDecimalAdjustment then
        set thePrefix to (reverse of (characters of thePrefix)) as string
        set theDecimalAdjustment to -theDecimalAdjustment
    end if
    set theDecimalOffset to offset of "." in thePrefix
    if theDecimalOffset = 0 then
        set theFirstPart to ""
    else
        set theFirstPart to text 1 thru (theDecimalOffset - 1) of thePrefix
    end if
    set theSecondPart to text (theDecimalOffset + 1) thru -1 of thePrefix
    set theConvertedNumber to theFirstPart
    set theRepeatCount to theDecimalAdjustment
    if (length of theSecondPart) is greater than theRepeatCount then set theRepeatCount to length of theSecondPart
    repeat with a from 1 to theRepeatCount
        try
            set theConvertedNumber to theConvertedNumber & character a of theSecondPart
        on error
            set theConvertedNumber to theConvertedNumber & "0"
        end try
        if a = theDecimalAdjustment and a is not equal to (length of theSecondPart) then set theConvertedNumber to theConvertedNumber & "."
    end repeat
    if theConvertedNumber ends with "." then set theConvertedNumber to theConvertedNumber & "0"
    if isNegativeDecimalAdjustment then set theConvertedNumber to (reverse of (characters of theConvertedNumber)) as string
    return theConvertedNumberPrefix & theConvertedNumber
end convertNumberToString
0

This converts a string to decimal integer, then back to a string. This eliminates any preceding zeros.

on run {input, parameters}
    set text1 to the clipboard
    if false then -- set to true if you need whitespace removed.
        set AppleScript's text item delimiters to {space, tab, linefeed, return}
        set text1 to text items of text1
        set AppleScript's text item delimiters to {}
        set text1 to text1 as string
    end if
    set text2 to "$((10#" & text1 & "))"
    do shell script "open https://api.whatsapp.com/send?phone=971" & text2
    return input
end run

Example: 0501234567
Actual Output : 971501234567
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .