4
set message to "Hello world "
set message to message & message

This produces an error. What is the correct way to concatenate strings? I need to use the same variable name as I'll use this in loops.

Or in short PHP style

$message = 'Hello world ';
$message .= $message
3
  • 1
    Which problem does this cause? I've just tested and it works. Do you have the code inside a tell application block? Commented Nov 28, 2012 at 8:02
  • Yes I do have the code inside a tell application block. It's strange because it works and doesn't work on and off without even changing the script - just a different execution time (seconds between). I do use more scripting commands below. I could update the question to include the following code but then it would be off point. The stated method should work for the concatenation part, as for an answer. (something else is probably weird later on) Commented Nov 28, 2012 at 9:53
  • Maybe the script is confused with a message key word of the application dictionary, post the code. Commented Nov 28, 2012 at 10:13

1 Answer 1

6

The word message is for some application and/or OSAX a command, class or property. To make this work no matter in which context this is executed you should wrap pipes around the variable name. So to use your code inside an tell mail block the code should look like this:

tell application "Mail"
    set |message| to "Hello world "
    set |message| to |message| & |message|
end tell

I know it looks ugly but it's the only way that you can force AppleScript to interpret the word as a variable.

edit: FWIW, I'm using variable names camel case and single words always starts with 'the'. So I would use theMessage instead of just message which never conflicts with application key words. 'the' is like the dollar sign in PHP.

edit 2 : grammar

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.