Question

I have the following code which uses the user's clipboard to WhatsApp's online API.

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

Now I want to check if the number matches the international code to use in WhatsApp's API correctly.

For example, if I copied a number 021 123456, I want to replace the first few digits with +994021 123456. What happened here is replace the first 0 with +994. However if the number already starts with +994 then there is no need to amend it.

How do I create a code to do this?

Was it helpful?

Solution

Ok, I've figured it out myself. Putting it out here if anyone needed it in the future.

 on run {input, parameters}
        set text1 to the clipboard
        if first character of text1 is "0" then
            set text2 to 1 - text1 as integer
            do shell script "open https://api.whatsapp.com/send?phone=" & "994" & 1 - text2
            return
        end if
        do shell script "open https://api.whatsapp.com/send?phone=" & text1
        return input
 end run
Licensed under: CC-BY-SA with attribution
Not affiliated with apple.stackexchange
scroll top