0

The question and answer at this link Send a text message from R use a function to create the AppleScript. I have copied it below

send_text = function(message){
  system(paste('osascript -e \'tell application "Messages"\' -e \'send "', message, 
               '" to buddy "xxx-yyy-zzzz" of (service 1 whose service type is iMessage)\' -e \'end tell\''))
}
send_text("Hello world")

I'd like to modify the function so it can take both the message and the number, like this - send_text = function(number, message)

I can't figure out how the paste works to replace xxx-yyy-zzz with the variable number.

JerryN
  • 2,356
  • 1
  • 15
  • 49

1 Answers1

1

The paste function works by combining everything separated by commas into a single string. All you have to do is extract the "xxx-yyy-zzzz" from the sentence, replace it with number, and use commas.

send_text = function(message, number){
    paste('osascript -e \'tell application "Messages"\' -e \'send "', message, 
            '" to buddy \"', number, '\" of (service 1 whose service type is iMessage)\' -e \'end tell\'')
}
send_text("Hello world", "555-888-0909")
#> [1] "osascript -e 'tell application \"Messages\"' -e 'send \" Hello world \" to buddy \" 555-888-0909 \" of (service 1 whose service type is iMessage)' -e 'end tell'"

Or alternatively you could use string interpolation from the glue package and insert the variable directly into the string with brackets.


send_text_2 <- function(message, number){
    glue::glue(
        'osascript -e \'tell application "Messages"\' -e \'send "{message}" to buddy "{number}" of (service 1 whose service type is iMessage)\' -e \'end tell\''
    )
}

send_text_2("Hello world", "555-888-0909")
#> osascript -e 'tell application "Messages"' -e 'send "Hello world" to buddy "555-888-0909" of (service 1 whose service type is iMessage)' -e 'end tell'

Created on 2023-02-19 with reprex v2.0.2