Launch Scripts from Webpage Links

AppleScript applications can interact with websites displayed on your computer to perform tasks and display information. This is done through the process of the AppleScript application registering itself with the OS as the handler of specific URL protocols and schemes.

The following describes how to create your own helper script applications.

The Info.plist File

When you save the script as an application bundle, it will contain the standard Mac OS X bundle elements including an XML property list file defining important aspects of the script application.

To access the Info.plist property list, click on the script application with the Control key held down to access the Finder Contextual Menu. Choose Show Package Contents from this menu to open the script application bundle in a new window. Open the Contents folder within the new window to reveal the Info.plist file. Open this file in a text or property list editor and add the following XML keys and values to the property list.

To identify the Application, add the following lines to the property list, replacing the blue placeholder text shown here with a unique bundle identifier for your application:

<key>CFBundleIdentifier</key>
<string>com.apple.AppleScript.WebpageHelper</string>

To identify the URL handler that triggers the applet, add the following item to the property list, replacing the blue text with title of your URL protocol and the URL scheme of your protocol:

<key>CFBundleURLTypes</key>
<array>
	<dict>
		<key>CFBundleURLName</key>
		<string>Webpage Helper</string>
		<key>CFBundleURLSchemes</key>
		<array>
			<string>webpagehelper</string>
		</array>
	</dict>
</array>

The Script Code

In order to respond to the links embedded in webpages, the script application must contain a handler for the open location Apple event. Place within this handler the AppleScript code necessary to parse the URL passed by the OS to determine the action the script should perform.


on open location this_URL
 -- When the link is clicked in thewebpage, this handler will be passed
 -- the URL that triggered the action, similar to:
 --> yourURLProtocol://yourBundleIdentifier?key=value&key=value

 -- EXTRACT ARGUMENTS
 set x to the offset of "?" in this_URL
 set the argument_string to text from (x + 1) to -1 of this_URL
 set AppleScript's text item delimiters to "&"
 set these_arguments to every text item of the argument_string
 set AppleScript's text item delimiters to ""

 -- PROCESS ACTIONS
 -- This loop will execute scripts located within the Resources folder
 -- of this applet depending on the key and value passed in the URL
 repeat with i from 1 to the count of these_arguments
 set this_pair to item i of these_arguments
 set AppleScript's text item delimiters to "="
 copy every text item of this_pair to {this_key, this_value}
 set AppleScript's text item delimiters to ""
 if this_key is "action" then
 if this_value is "1" then
 if my run_scriptfile("Action Script 01.scpt") is ¬
 false then error number -128
 else if this_value is "2" then
 if my run_scriptfile("Action Script 02.scpt") is ¬
 false then error number -128
 else if this_value is "3" then
 if my run_scriptfile("Action Script 03.scpt") is ¬
 false then error number -128
 end if
 end if
 end repeat
end open location

on run_scriptfile(this_scriptfile)
 -- This handler will execute a script file
 -- located in the Resources folder of this applet
 try
 set the script_file to path to resource this_scriptfile
 return (run script script_file)
 on error
 return false
 end try
end run_scriptfile

on load_run(this_scriptfile, this_property_value)
 -- This handler will load, then execute, a script file
 -- located in the Resources folder of this applet.
 -- This method allows you to change property values
 -- within the loaded script before execution,
 -- or to execute handlers within the loaded script.
 try
 set the script_file to path to resource this_scriptfile
 set this_script to load script script_file
 tell script this_script
 set some_script_property to this_property_value
 end tell
 return (run script this_script)
 on error
 return false
 end try
end load_run