Passing a parameter to a script on another machine.

I run the (slightly cleansed) script below from an iMac running Lion to a MacBook Pro running Snow Leopard (because the remote script is talking to an app that must use Rosetta):

	-- toggles external drive power on if off and vice versa
	tell application "Finder" of machine "eppc://userName:PassWord@10.0.1.4" -- on my LAN
		set BKUP to alias "ACB-MBP:Users:moi:Dropbox:Scripts:PowerOnOff:Toggle_HD.app:"
		open BKUP
	end tell

I could substantially simplify the script on the iMac (of which the above is only a part) and on the MBP if I could pass a parameter to the MBP script telling it whether I wanted the power to be ON or OFF. As it is, I have to fiddle around at both ends to make sure of the state. Is this an option?

Adam:

Assuming I understand your object, here’s a rather flaky suggestion.

Why not have have a user choice on/off dialog in the iMac script that would set the clipboard on the MacBook to either ‘On’ or ‘Off’… then have the MacBook app read the clipboard to ‘decide’ what to do.

Seems you might (instead) have the Finder on the MacBook move a file from place to place, then have the MacBook look for the file. If exists…

I know you wanted to actually pass a parameter directly, but there would appear to be workarounds.

Peter B.


I have a solution for you. My solution is to send an osascript command to the remote machine (instead of using the Finder’s open command) because you can pass a parameter to an applescript that way.

The Problem
I quickly found a problem with that though because I was not able to send a “do shell script” command using remote apple events and thus couldn’t issue the osascript command. No matter how I tried my 10.7 machine couldn’t issue a “do shell script” command to a 10.6 machine.

The Solution
Use ssh to issue the osascript command. I was able to ssh into the remote machine and issue the osascript command with a parameter. Here’s how to run an ssh session through applescript and issue your osascript command…

-- to use this script just setup the ssh parameters and the osascript command, the rest should just work

-- setup the ssh parameters
set sshHost to "machineName"
set sshUserName to "username"
set sshPassword to "password"

-- setup the osascript command
set posixScriptPath to "/posix/path/to/script.scpt" -- this is the posix path on the remote machine
set scriptParameter to "some parameter"
set osascriptCmd to "osascript " & quoted form of posixScriptPath & space & quoted form of scriptParameter

-- This is how you can script ssh and send a username/password and perform some commands
-- We basically create a shell script in the applescript code and execute it using "sh -c"
-- which allows us to pass a shell script as text to the command line.
-- In the shell script we ssh into a computer, issue our osascript command, and then exit.
-- We use the command line tool "expect" in the shell script to get around ssh scripting issues.
set shScript to "#!/bin/bash

HOST=" & sshHost & "
USER=" & sshUserName & "
PASS=" & sshPassword & "
PROMPT=#

expect -c \"
spawn ssh $USER@$HOST
expect \\\"password:\\\"
send \\\"$PASS\\r\\\"
expect \\\"$PROMPT\\\"
send \\\"" & osascriptCmd & "\\r\\\"
expect \\\"$PROMPT\\\"
send \\\"exit\\r\\\"
\"
"

do shell script "sh -c " & quoted form of shScript

Now you just have to setup your applescript on the remote machine to accept your parameter. Something like this…

on run argList
	set theArg to item 1 of argList
	-- do something with the passed argument
end run

That is rather neat, Hank. A little rearranging of scripts is required (I should add that the arrangement I’ve got works by doing a bunch of testing at both ends) and then I’ll try it out.

Be back…

… Later

I tried it without the parameter (just commented out) and selected a script that I’d know had run as the test case, but it didn’t run and I know it works because it does on the other machine. The trouble with this is that you don’t get any feedback about where things went wrong.

Hi, Adam.

The Finder’s ‘open’ command has an optional ‘with properties’ parameter. The only information I’ve been able to find about its use is a discussion on the AppleScript-Users list from December 2000!

The properties record should be in a form your script applet’s been written to expect, say {|target state|:“OFF”}:

tell application "Finder" of machine "eppc://userName:PassWord@10.0.1.4" -- on my LAN
	set BKUP to alias "ACB-MBP:Users:moi:Dropbox:Scripts:PowerOnOff:Toggle_HD.app:"
	open BKUP with properties {|target state|:"OFF"}
end tell

The applet’s run handler would have to look something like this:

on run given «class prdt»:props
	set targetState to |target state| of props

	-- etc.
end run

The ‘with properties’ parameter isn’t optional if the applet’s expecting it!

The idea works in Snow Leopard and I presume it would work on a remote machine.

Well I know the idea works because I tested it many times. However… just before I uploaded it to this thread I moved the osascript command outside the bash shell script part to make it a variable. I thought I tested that last change to the code but maybe not??? My guess on the issue is with…

set osascriptCmd to "osascript " & quoted form of posixScriptPath & space & quoted form of scriptParameter

being inside the final call of…

do shell script "sh -c " & quoted form of shScript

Possibly the quoted form stuff inside the quoted form stuff causes a problem. If you want to make a second try then try changing the osascript command to this…

set osascriptCmd to "osascript \\\"" & posixScriptPath & "\\\" \\\"" & scriptParameter & "\\\""

Maybe that will fix it. I don’t have access to my remote machine right now to check. I’m certain the idea is correct so now it’s just a syntax thing.

But hopefully Nigel’s suggestion works so that’s probably your easiest route anyway. That would be nice. If not though then tweak the syntax a little.

Thank you, gentlemen – more grist for the mill.