Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

syntax for abbreviated code

set text to "Now" & space & space & space & space --(etc.) works

--Can this somehow be written in a more efficient manner, something like:

set text to "Now" & (4 * space) --which doesn't work

--????

MAC pro, Mac OS X (10.4.11)

Posted on Jun 11, 2009 9:48 AM

Reply
Question marked as Best reply

Posted on Jun 11, 2009 9:50 AM

Here:

set the_text to "Now"
repeat 4 times
set the_text to the_text & space
end repeat

(44448)
5 replies

Jun 11, 2009 11:44 AM in response to Noaks

You can also grab some text from another string (don't forget that you can use your own handlers):

<pre style="
font-family: Monaco, 'Courier New', Courier, monospace;
font-size: 10px;
margin: 0px;
padding: 5px;
border: 1px solid #000000;
width: 720px;
color: #000000;
background-color: #FFEE80;
overflow: auto;"
title="this text can be pasted into the Script Editor">
set MyText to AddSpacesTo("Now", 4)
log MyText

to AddSpacesTo(SomeText, SomeNumber) -- add spaces to the end of SomeText
set Spaces to " " -- 10 spaces
return SomeText & (text 1 thru SomeNumber of Spaces)
end AddSpacesTo
</pre>

Jun 11, 2009 12:49 PM in response to Noaks

I didn't check out red_menace's suggestion with respect to this issue, but Niel's solution --while it probably works in most cases -- does seem to have a pitfall:

Suppose I'm specifying text for a TextEdit document. If I write:

set the_text to "Nowxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
tell application "TextEdit"
make new document at front
repeat 20 times
set the_text to the_text & space
end repeat
set text of front document to the_text
end tell

Possible results:
1) If the code is writing actual characters when it reaches the end of a line, then the characters wrap properly to the next line (assuming preferences are set up for wrapping).
2) But, if the code is writing spaces when it reaches the end of a line, then it doesn't wrap. I think this will present a problem if an attempt is made to add more text. Try this:

set the_text to "Nowxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
set the text2 to "and now more text"
tell application "TextEdit"
make new document at front
repeat 20 times
set the_text to the_text & space
end repeat
set text of front document to the_text and the text2
end tell

(On my machine there certainly aren't 20 spaces between the two text blocks -- just the number of spaces left at the end of the preceding line.)

(What happens all depends on the font size and the width of the open document window and, of course, the number of actual characters preceding the spaces. It's also possible, I guess, that what happens depends on the specific application.)

Message was edited by: Noaks

Jun 11, 2009 1:20 PM in response to Noaks

Mine will do the same, since what you are seeing is the result of what text editors such as TextEdit use to determine where to break up the text. Some characters will immediately wrap to the next line, and others will not - for example, try your example using some other character, like a dash. If you are wanting to use a space that doesn't break, instead of using space, use a text string with an option-space in it (I don't there is a constant defined for the non-breaking space).

Jun 11, 2009 1:22 PM in response to Noaks

Hello Noaks,

After all, you'd need to define some handler for it.
There's no operator overriding in AppleScript. All you can do is to mimic it to your liking.
E.g.

--CODE1
return "A" & (multiple of space by 4) & "B"
on multiple of x by n
if x's class is in {real, integer} then
x * n
else
set result to ""
repeat n times
result & x
end repeat
end if
end multiple
--END OF CODE1

--CODE2
return "A" & |*|(space, 4) & "B"
on |*|(x, n)
if x's class is in {real, integer} then
x * n
else
set result to ""
repeat n times
result & x
end repeat
end if
end |*|
--END OF CODE2

etc.

Good luck,
H

syntax for abbreviated code

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.