Lasso Soft Inc. > Home

[applescript]

Linkapplescript
AuthorJohan Solve
CategoryTechnical
Version6.x
LicensePublic Domain
Posted10 Mar 2006
Updated05 Sep 2006
More by this author...

Description

AppleScript tag
Version 1.0b2 2003-11-06
by Johan Sölve johan@montania.se

Adjusted for LP7 syntax 2004-05-25

Requires PassThru 3.0 or later.
http://www.execuchoice.net/
This tag executes AppleScript from within Lasso, using PassThru and osascript command line utility.
The AppleScript can either be specified as text source directly when calling the tag or point to a compiled AppleScript file using the optional -file parameter. If -file is used, the tag begins looking for the script file in the specified user´s home directory but a full path from root can also be specified.

Usage:
applescript: '-- this is a script', -username=, -password= ;
applescript: -file='compiled.scr', -username=, -password= ;
Parameters:
one unnamed parameter (optional) -- the script source text (can be multiple lines)
-username='user' (required) -- a MacOS X username that the AppleScript is executed as. The machine must be logged in as this user (in Panther it can be one of multiple logged in users). Possibly this is not required for some type of scripts, in particular if they don´t interact with the GUI.
-password='pwd' (required) -- the password of the MacOSX user that the script runs as
-file='filename' (optional) -- specifies a plain text or compiled AppleScript file. Using a relative path begins looking at the user´s home directory. No Lasso file permissions are applied on the file specification. Only MacOS permissions apply.
-noerr (optional) -- supress error messages from the script

For more information about how AppleScripts are actually executed, type "man osascript" in Terminal. One detail to note is that the only way to pass arguments to the script is to put dynamic values in the script source text.

Due to how PassThru always operates using sudo, script execution is slowed down by the sudo authentication which usually takes a few seconds. However, this is MUCH faster under 10.3 than 10.2. The sudo authentication overhead seems to be completely gone, and applescript execute much faster.

Specifying a compiled AppleScript file is likely the most efficient use. Otherwise the script source must be compiled before it can be executed, and this adds some overhead. This makes no perceivable difference for small scripts, but more complex scripts would likely benefit from being precompiled. But then there´s no way to pass arguments to the script.

Running AppleScript from Lasso is not suitable for a public high-traffic sites. It is better intended for low-traffic solutions where it´s likely that only a few users are expected to trigger an AppleScript at the same time.

1.0b2 2003-11-06
Adjustments in documentation
use "~user/" instead of "/Users/user/" to get user´s home directory

1.0b1 2003-10-23
Initial release

Sample Usage

applescript: 'set a to "hello world it\'s here"
a & " yes"', -username='xx', -password='yy';

Source Code

Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.

> 'Mac');
		return: 'The AppleScript tag is not supported on ' 
			+ (lasso_version: -lassoplatform); 
	/if;
	if: !(lasso_tagexists: 'passthru') || !((passthru: -version) >> 'Version 3');
		return: 'The AppleScript tag requires PassThru 3';
	/if;
	local: 'thescript'='', 
		'output'='',
		's_flag'='h',
		'path'='',
		'myfile'='';
	// capture named params manually since -required and -optional 
	// can be autopopulated by unnamed params
	local: 'myusername'=(params -> (find: '-username') -> (get: 1) -> value),
		'mypassword'=(params -> (find: '-password') -> (get: 1) -> value),
		'mynoerr'=(params >> '-noerr'); // boolean
	if: (params >> '-file') && 
		(params -> (find: '-file') -> (get: 1) -> type == 'pair');
		#myfile=(params -> (find: '-file') -> (get: 1) -> value);
	/if;
		
	// get first unnamed param
	iterate: params, (local: 'thisparam');
		if: #thisparam -> type == 'string'
			&& #thisparam != '-noerr';
			#thescript = (string: #thisparam);
			loop_abort;
		/if;
	/iterate;

	// check if we want errors reported to standard output
	if: #mynoerr;
		// pass osascript errors to stderr (not seen by the tag)
		#s_flag += 'e';
	else;
		// pass osascript errors to the output
		#s_flag += 'o';
	/if;
	
	// use -file if there is one
	if: #myfile != '';
		#thescript = '';
		if: #myfile -> beginswith: '/';
			#path = '';
		else;
			// default to the user´s home directory
			#path = '~' + #myusername + '/';
		/if;
		#thescript = #path + #myfile;
	else;
		#myfile='';
	/if;
	if: #myfile =='';
		// normalize line breaks in the script text
		#thescript = (string_replace: 
			(string_replace: #thescript, 
				-find='\r\n', -replace='\r'), 
			-find='\n', -replace='\r');
		// prepare quotes so the can be passed at command line
		#thescript = (string_replace: #thescript, 
				-find='\'', -replace='\'\\\'\''); 
		#thescript = #thescript -> split: '\r';
		// prepare the script text to be passed to osascript, one -e parameter for each script line
		#thescript = ' -e \'' + (#thescript -> join: '\' -e \'') + '\'';
	/if;
	
	#output = (passthru: 'osascript -s ' + #s_flag + ' ' + #thescript, 
		-username=#myusername, 
		-password=#mypassword);
	return: #output;
/define_tag;


?>

Comments

17 Sep 2007, Johan Solve

Another option

See this Tip Of The Week for a different implementation that uses [os_process]:
http://www.lassosoft.com/Documentation/TotW/index.lasso?9303

07 Sep 2006, Johan Solve

os_process

On the TODO list is also to update this tag to use the native os_process tag in Lasso 8.5.

07 Sep 2006, Johan Solve

Parameters in Tiger

osascript starting with MacOS X 10.4 supports sending dynamic parameters to the script. The tag could use an update to support this.

09 Jun 2006, Brian Loomis

PassThru 2.7

This works fine with PassThru 2.7 just comment out the test for the version.

Please log in to comment

Subscribe to the LassoTalk mail list

LassoSoft Inc. > Home

 

 

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