Need some Help? Have a tip to share?

Email me at MacTipper@gmail.com, or, leave a comment!

Learn How to Win Free Stuff Online


11/6/09

Global Variables in AppleScript

I've always thought that, when writing an AppleScript subroutine, one needed to pass all the terms to the subroutine. However, if you're working with a constant variable, a Global Variable is called for.

What is a global variable? Global variables allow you to define which terms should be shared throughout the entire script. So, if you set a global variable you can then access that variable anywhere in the script. Keep in mind though that, unlike properties, you do not define a global variable when you create it:

Open in AppleScript Editor

global multiply_factor

on run
--Define Multiply_Factor
set multiply_factor to 64

--Use it in the math_problem subroutine
set the_answer to math_problem(16, 32)
return the_answer
end run

--This subroutine uses the multiply_factor variable even though we didn't pass it the multiply_factor variable during the run statement.
on math_problem(x, y)
set new_x to x * multiply_factor
set new_y to y * multiply_factor
set final_result to new_x + new_y
return final_result
end math_problem

0 comments:

Post a Comment

Subscribe Via Email!

Enter your email address:

Delivered by FeedBurner