20260410

Applescript: URL PROTOCOL SUPPORT

URL PROTOCOL SUPPORT

NOTE記事一覧ですnote.com

URL PROTOCOL SUPPORT


Until now, providing AppleScript examples to those interested in learning to script has been problematic. Copying examples from books and printed publications is a laborious process often prone to input errors. Examples copied from web pages and other HTML-based sources often are missing characters and spaces that make compiling the script example difficult. The new Script Editor application solves this problem with its support for URL Protocol Messaging.


URL Protocol Messaging provides the ability to retrieve script examples embedded in web pages or PDF files by simply clicking a link or picture on a page. The embedded AppleScript code will be automatically transferred to the Script Editor application and either placed in a new script window, inserted into the front window at the current insertion point, or appended to the end of the script in the front window.


URL Protocol Messaging is a safe, secure method for sharing scripts. The execution of transferred examples requires direct action from the user and under no conditions can a transferred script be automatically executed.


The following information describes how to create URL Protocol Messages.



Creating Message Links


Links targeting the new Script Editor application are created using the following parameters. Links not following this design will not trigger a response from the Script Editor application. Each link is composed of four sections: the URL protocol, the application address, the action tag, and the script tag.



The URL Protocol


Message links begin with the URL protocol applescript:// which is registered with the operating system as a URL type that will be handled by the Script Editor application. Any URL beginning with this protocol will be passed to the Script Editor application for processing.



The Application Address


The opening URL protocol is followed by the application identifier of the Script Editor application: applescript://com.apple.scripteditor



The Action Tag


The action tag follows the opening URL structure appended with a question mark character (?) and followed with the tag: action=action type


This tag indicates one of three possible actions supported by the new Script Editor:

  1. new -- a new script window containing the passed script code will be created.
     
    applescript://com.apple.scripteditor?action=new
     
  2. insert -- the passed script code will be inserted into the script of the frontmost script window at the active insertion point.
     
    applescript://com.apple.scripteditor?action=insert
     
  3. append -- the passed script code will be added to the end of the script in frontmost script window.
     
    applescript://com.apple.scripteditor?action=append
     


The Script Tag


The script code is an AppleScript script that has had returns, tabs, and other special characters encoded using standard URL encoding. For example, a carriage return is encoded to be: %0D


The script code follows the action tag appended with an ampersand character (&) and followed by the tag: script=encoded text


applescript://com.apple.scripteditor?action=new&script;=display%20dialog%20%22Hello!%22



Examples


Here are examples of the three action-types:

  1. placing script text in a new script window
  2. inserting text into the frontmost script window
  3. appending to a script in the frontmost script window


The Encoding Script


Click here for a script you can use to encode a script copied to the clipboard. Save this script in your user Scripts folder to have it available from the Script Menu.


NOTE: In the initial release of Mac OS X v10.3, the Script Editor will not translate embedded scripts containing high-ASCII characters such as: ¬, “ or ”



Macscripter Message Boards Script


Some message boards, such as macscripter.net, require alternate URL formats for embedding links in forum messages.


[url=applescript://com.apple.scripteditor/?action=new&script;=script%20code]link text[/url]


Click here for a special version of the encoding script that will replace a script copied to the clipboard with an encoded version encased in URL tags ready for posting on macscripter.net forums.