Jonny
Jonny

Reputation: 16298

AppleScript: Concatenate to existing string

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

Upvotes: 4

Views: 5253

Answers (1)

dj bazzie wazzie
dj bazzie wazzie

Reputation: 3542

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

Upvotes: 6

Related Questions