Lasso Soft Inc. > Home

  • Articles

Lasso 8.5 AppleScript and Automator Support

Taken from the tip of the week for September 7, 2007 this article discusses how Lasso 8.5 can call AppleScript commands and Automator workflows on Mac OS X systems. AppleScript allows you to interact with other running applications, restart the server, and more.

Introduction

Lasso 8.5 can call AppleScript commands on your Mac OS X server. This tip includes an [AppleScript] tag which compiles and executes an AppleScript from within Lasso. It also includes an [Automator] tag which can run any automator workflow saved as an application. These tags can be used to perform many different tasks on your server.

- Restart your server

- Speak warning messages

- Run Automator workflows

Important - The ability to run AppleScript on your server opens up a lot of functionality, but may also make it possible for Lasso programmers to access functionality that you don't intend. The AppleScript tag should only be installed in sites which are programmed by trusted users.

See this earlier tip for general information about the [OS_Process] type.

Mac OS X Shell Support

Installation

Download the following file and place it into the LassoStartup folder for your site. This file will define the [AppleScript] and [Automator] tags and make them available throughout our site. You also need to install the [OS_Process] type using the instructions below.

<FILE MISSING IN ACTION  http://downloads.lassosoft.com/pub/totw/totw_9303.zip>

The [OS_Process] type is not installed by default. In order to activate this type you must copy the "OS_Process.dylib" file from the Extensions/OS_Process folder in the Lasso 8.5 application folder to the LassoModules folder and restart Lasso Service.

AppleScript

The [AppleScript] tag can be used to execute short AppleScript segments. AppleScript is capable of performing countless tasks, but we'll just give a few examples. If the AppleScript returns a result it will be returned by the [AppleScript] tag.

Speak

Say is a simple AppleScript command which uses the built-in speech synthesizer in Mac OS X. This simple AppleScript could be used to speak server status messages. Note that the speech is going to be heard in the server room, not on the client's machine.

  [AppleScript: 'say "The ghost in the machine!"']

 

Restart the Server

System Events is a background application which allows many system tasks to be performed. The following AppleScript restarts the server as if the "Restart" command was selected from the Apple menu.

  [AppleScript: 'tell application "System Events" to restart']

 

AppleScript Results


The System Additions contain many useful tools for getting the date and time, manipulating XML files, and more. The following script gets the current month and returns it.

  [AppleScript: 'get month of (current date)']

 

Multiple Lines

AppleScripts can be formatted on multiple lines with indents. The following script gets the current month and then speaks it aloud.

  [AppleScript: '
      set myVariable to get month of (current date)
      say "It is " & myVariable & "!"
    ']

 

Creating an AppleScript

You should always create your AppleScripts in the Script Editor (which can be found in the /Applications/AppleScript/) folder before pasting them into Lasso. It is much easier to debug your AppleScripts in the editor than to do it in Lasso.

Within the Script Editor choose "Open Dictionary" from the File menu to see the dictionary for any applications on your machine. Of particular interest are the System Events and Scripting Additions dictionaries. These dictionaries contain many generally useful commands.

If you are not familiar with AppleScript then you might want to try the Automator which is described in the next section.

Automator

Automator is an application which Apple provides in the latest version of Mac OS X which makes it easy to create simple AppleScripts. An Automator Workflow is created by dragging actions from a menu on the left into the workflow on the right. Each action can have options which are set using text fields and pop-up menus. In general the result of each action flows into the next action as its input.

For example, open the Automator application and select iTunes on the left. Drag a "Find Songs in iTunes" action into the empty workflow on the right. You can customize the action by choosing to find a particular artist.

 Find "Songs" Whose "Artist" "Is Equal To" "Plaid"
  [Automator: '/Automator/Play Plaid.app']

Now, drag a "Start iTunes Playing" action into the workflow below the "Find Songs in iTunes" action.

Click the "Play" button to run this workflow. Assuming you have some songs by "Plaid" in your iTunes library you will start hearing "Quick Emax" or another song.

Save this workflow and choose the "Application" file format. Give the workflow a name like "Play Plaid". I saved mine in a folder called "Automator" which I created in my Web server root. Now, find the workflow in the Finder and double click on it. The selected music will start playing in iTunes.

Now, the [Automator] tag can be used to execute this workflow from within Lasso. The tag should be passed the name of the application created by Automator for the workflow, including the ".app" file suffix.

 

I have found that Automator is able to execute many actions on the server which AppleScript is not. For example, AppleScripts which manipulate iTunes can sometimes fail from within Lasso since the "lasso" user is not the user currently logged in to the machine. But, Automator workflows do not seem to have this same difficulty.

Implementation

The implementation of both tags is shown here. The tags can be used within understanding the implementation, but these are a good example of how to wrap the [OS_Process] type to make a more user-friendly tag.

The [AppleScript] tag requires a single parameter -Script. The tag is -Privileged so that it will hold onto administrator privileges after it is created in Lasso Startup. It initiates the process /usr/bin/osascript with the command line parameter - which tells the process that it should expect an AppleScript on standard input. The passed #script is then piped into the process using the ->Write tag. The ->CloseWrite tag lets the process know that the script is complete. The result is read using ->Read and the process is closed using ->Close. Finally, the result of ->read is returned.

 

The [Automator] tag requires a single parameter -Path. The tag is -Privileged so that it will hold onto administrator privileges after it is created in Lasso Startup. The tag attmpets to find the executable within the automator workflow application which is specified. If it finds it within the application package then it executes it. Note that ->Read is called just before ->Close. This is necessary to trigger many processes into actually executing.

  Define_Tag('AppleScript', -Required='script', -Privileged);
    Local('result' = '');
    Local('osascript' = OS_Process('/usr/bin/osascript',Array('-')));
    #osascript->Write(#script);
    #osascript->CloseWrite;
    #result += #osascript->Read;
    #osascript->Close;
    Return((#result);
  /Define_Tag;
  Define_Tag('Automator', -Required='path', -Privileged);
    Local('result' = '');
    Fail_If(#path->EndsWith('.app') == False, -1,
        'Automator Error: Specified file must be a .app file created by Automator.');
    If(#path !>> '//');
      Local: 'fullpath' = (Response_LocalPath - Response_FilePath) + (File: #path)->Path;
    Else;
      Local: 'fullpath' = #path;
    /If;
    Local('fullpath' = #fullpath + '/Contents/MacOS/' +
        (#path->Split('/')->Last - '.app'));
    Fail_If(File_Exists(#path) == False, -1,
        'Automator Error: Specified file must be a .app file created by Automator.');
    Local('osascript' = OS_Process(#fullpath));
    #result += #osascript->Read;
    #osascript->Close;
    Return((#result);
  /Define_Tag;


More Information

More information about all of the tags used in this tip of the week can be found in the Lasso 8.5 Language Guide or in the online Lasso Reference

 

Author: Marc Pope
Created: 7 Sep 2001
Last Modified: 16 Mar 2011

Comments

No comments found
You must be logged in to comment.

Please note that periodically LassoSoft will go through the notes and may incorporate information from them into the documentation. Any submission here gives LassoSoft a non-exclusive license and will be made available in various formats to the Lasso community.

LassoSoft Inc. > Home

 

 

©LassoSoft Inc 2015 | Web Development by Treefrog Inc | PrivacyLegal terms and Shipping | Contact LassoSoft