<?php
/* 
= Register a script for use by shortcode only if present on page
* WordPress: 3.3.1
* Version: 1.1
* URL: elbone.com.au
* Files: functions.php
*/

// Register a script
add_action('init', 'register_my_script');
function register_my_script() {
    wp_register_script('my-script', plugins_url('my-script.js', __FILE__), array('jquery'), '1.0', true);
}

// The shortcode
add_shortcode('myshortcode', 'my_shortcode_handler');
function my_shortcode_handler($atts) {
    global $add_my_script; // global used by print_my_script();
 	$add_my_script = true;
 	// actual shortcode handling here
}

// Print the script if global is set
add_action('wp_footer', 'print_my_script');
function print_my_script() {
	global $add_my_script;
	if ( ! $add_my_script )
		return;
	wp_print_scripts('my-script');
}