0

This is a apple script as below. I want to read a file content and assign it as a variable for sending as iMessage content. But, I get an error.

tell application "Messages"
    set targetBuddy to "+xxxxxxxxxxx"
    set targetService to id of 1st service whose service type = iMessage
    set Shows to paragraphs of (read POSIX file "/Users/human/macairspace.txt")
    set textMessage to Shows
    set theBuddy to buddy targetBuddy of service id targetService
    send textMessage to theBuddy
end tell

It shows an error message.

error "「Messages」Error:Can't make「file \"Macintosh HD:Users:human:macairspace.txt\"」as「file」。" number -1700 from file "Macintosh HD:Users:human:macairspace.txt" to file
1

One possible cause of that error is the POSIX pathname, "/Users/human/macairspace.txt" is not correct; however, that is not the only thing wrong with your code:

Without going into details about what else is wrong, let be show you an example of how I'd code it, and this works for me when I replace "+nnnnnnnnnnn" and "/path/to/file.txt" with valid values.

Example AppleScript code:

set targetBuddy to "+nnnnnnnnnnn"
set textMessage to (read POSIX file "/path/to/file.txt")

tell application "Messages"
    set targetService to id of 1st service whose service type = iMessage
    set theBuddy to buddy targetBuddy of service id targetService
    send textMessage to theBuddy
end tell

Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the used to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors.

3
  • I am not lucky to get it after your guide. – jefferyear Mar 11 '19 at 3:59
  • @jefferyear, Sorry to hear that; however, just saying "I am not lucky to get it after your guide." gives me literally nothing to try and help you with. As I said, if I use a valid value for "+nnnnnnnnnnn" and "/path/to/file.txt" then the example AppleScript code works for me in macOS High Sierra. What error message do you get with my example when using valid info for the two variables at the start of the example code? – user3439894 Mar 11 '19 at 4:25
  • Thanks. I wonder why it has this matter. I use High Sierra, too. Is it possible on the UTF-8 or something like that? I use non-English on my OS. – jefferyear Mar 11 '19 at 4:40

You must log in to answer this question.

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