Applescript - Function: Code optimization

Hey all,

Objective:

  1. Remove repeating code with a function or equivalent

Issues:

  1. Using the function, I get this error “Can’t continue addValues”

on addValues()
	set end of x to y
	return
end addValues

addValues()

Question:

  1. How do I execute the code above each time I need it in different parts of the applescript?

TIA

That doesn’t work on it’s own.

Is there any way to remove repeating code?
Like a procedure instead of a function or something equivalent?

TIA

Are you asking how to write and call a function?


to addValues(x, y)
	set combined to x + y
	return combined
end addValues

set theTotal to addValues(5, 4)

Thanks Craig for the reply.

Actually I want is to take:


code
code 
code
print "Hello World"
code
code 
code
print "Hello World"
code
code 
code
print "Hello World"

Result:


Hello World
Hello World
Hello World

TO THIS:


to textPrinter()
print "Hello World"
end textPrinter

code
code 
code
textPrinter()
code
code 
code
textPrinter()
code
code 
code
textPrinter()

Result:


Hello World
Hello World
Hello World

I hope that is clearer?!?

Not exactly but looks like you already get the idea from the sample you posted.

If you are actually not clear how to proceed then post some real code you are
having trouble converting to a function.

There are some excellent tutorials on this forum under unScripted that you
might look at as well.

hth,

Craig

So from what I’m getting.

  1. There are no data structures that you can use to modularize redundant code?
  2. Function have to take and return values?

Of course they are, a handler is one of the essential functions of AppleScript

Take a look at the three parts of Ben Waldie’s Getting Started with Handlers

http://bbs.macscripter.net/viewtopic.php?id=24727
http://bbs.macscripter.net/viewtopic.php?id=24738
http://bbs.macscripter.net/viewtopic.php?id=24743

Not exactly what I thought after further investigation