0

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?

1

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
| improve this answer | |
  • Replace do shell script "open https://api.whatsapp.com/send?phone=" with open location "https://api.whatsapp.com/send?phone=", which performs the same function without having to create a shell process to do it. Also, as this is running within macOS, you can actually bypass the browser altogether, by using open location "whatsapp://send?phone=". – CJK Mar 4 at 4:10
  • @CJK does this have any notable effects on the script? – Render Oct 15 at 7:39
  • Effects on the script include being shorter, easier-to-read, and will be something less likely to be earmarked as an obviously "novice" script. In terms of execution, it'll be faster and more reliable (open location is an AppleScript command, but do shell script calls the shell program open, which a shell function, alias or a variety of other things can redirect open to do something else. Also, if you want to just run a shell command, Automator has a Shell script action). Using the whatsapp:// scheme instead of https:// doesn't bring the browser up, and is a lot faster. – CJK Oct 26 at 9:17
  • Also, your script here doesn't do what you stated you wanted to achieve in your question above. I think what you want is this: ignoring white space // set text item delimiters to {null, space, "+9940", "+994"} // "whatsapp://send?phone=994" & (text items of (the clipboard) as text as number as miles as text) // end. This will trim off all leading zeroes and any instance of "+994" plus up to one zero after (if present), then prepend the URL and "994" . So +99412555555, 012555555, and +994012555555 all become "whatsapp://send?phone=99412555555". open location result to finish. – CJK Oct 26 at 9:54

You must log in to answer this question.

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