Learn how to AppleScript, Apple's built in automation platform. This video will give a brief overview of variables and variable scope.
For more AppleScript tutorial videos, check out this play list.
https://youtube.com/playlist?list=PL5iB9WEZe2j04doUAKVxqxaCv3JJ8TxQr
If you're interested in watching full scripts being written, have a look at this play list.
https://youtube.com/playlist?list=PL5iB9WEZe2j2UIcvjAPVDNMU7oCuCb5eJ