send parameters to script

Hi,
Anybody has sent parameters to an AppleScript from another AppleScript, or from the command line of type " myscript -a 20 -b 10"?
I want to run a remote script, with a diferent set of paramenters each time the script runs

thanks

Martin!

Here’s an example of one script passing parameters to another.

Script 1: Remote script

on run {x, y}
	display dialog x & return & y
end run

Script 2: Local script

set remote_ to "path:to:Remote script"
run script alias remote_ with parameters {"one", "two"}

– Rob

Thanks
How I can to run the script on diferents computers?

My remote computer is OS 9.1 and my local computer is OS X 10.2.8. You’ll need to make sure that both computers are set up correctly to allow this to happen. If I recall correctly, the related settings can be found in the settings for sharing (sorry, I don’t have time to offer configuration suggestions). Here’s how I ran the remote script on my local network for the purpose of this test.

  1. Place script 1 (above) on the desktop of the remote computer’s startup disk.
  2. The path to the remote script file is: “Mac HD:Desktop Folder:dd remote test”
  3. Run the following script from local computer.
using terms from application "Finder"
	tell application "Finder" of machine "eppc://username:password@192.168.0.102"
		run script file "Mac HD:Desktop Folder:dd remote test" with parameters {"Hello", "Martin"}
	end tell
end using terms from

This resulted in a “Hello Martin” dialog on the remote machine and it returned the results of the dialog to the local script.

– Rob

Hi Rob

I’ve been trying to get something like this to work properly, I must be doing something wrong, so here are the 2 scripts I have been working with (once I get them working I can mangle them to do what I really want them to do!)
this is called call

set id_me to POSIX path of (path to me)


set {theName, thePath} to nameAndPath out of id_me

on nameAndPath out of pathString
	tell AppleScript
		set olD to text item delimiters
		set text item delimiters to "/"
		set reqItem to -1
		if last item of pathString = "/" then set reqItem to -2
		set theName to text item reqItem of pathString
		try
			set thePath to ((text items 1 through (reqItem - 1) of pathString) as string) & "/"
		on error -- it's a disk 
			set thePath to theName & "/"
		end try
		set text item delimiters to olD
		return {theName, thePath}
	end tell
	
end nameAndPath
--display dialog theName
--display dialog thePath
display dialog thePath & "remote_run"

set remote_script to thePath & "remote_run"

set x to "Parameter 1"
set y to "Parameter 2"
using terms from application "Finder"
	tell application "Finder"
		run script file remote_script with parameters {x, y}
	end tell
end using terms from


#############################################################################
#############################################################################
end script
#############################################################################

new script called remote_run saved in same directory code is below

#############################################################################

on run {x, y}

	display dialog x & return & y
	
end run

the error message I get is “Finder got an error: Can’t make some data into the expected type.” the expected type was “item”.

Help, I really do need this to work!

cheers
Biz

The problem appears to be the path that’s being passed to the Finder in the calling script. Does this work?

set id_me to POSIX path of (path to me)

set {theName, thePath} to nameAndPath out of id_me

on nameAndPath out of pathString
	tell AppleScript
		set olD to text item delimiters
		set text item delimiters to "/"
		set reqItem to -1
		if last item of pathString = "/" then set reqItem to -2
		set theName to text item reqItem of pathString
		try
			set thePath to ((text items 1 through (reqItem - 1) of pathString) as string) & "/"
		on error -- it's a disk 
			set thePath to theName & "/"
		end try
		set text item delimiters to olD
		return {theName, thePath}
	end tell
	
end nameAndPath
--display dialog theName 
--display dialog thePath 
display dialog thePath & "remote_run"

set remote_script to POSIX file (thePath & "remote_run") as text -- MODIFIED

set x to "Parameter 1"
set y to "Parameter 2"
using terms from application "Finder"
	tell application "Finder"
		run script alias remote_script with parameters {x, y} -- MODIFIED
	end tell
end using terms from

– Rob

Hi Rob

I’ve just managed to get it to work without any chaotic code! The problem was in the POSIX path as I had been passing stuff to the command line!

Much simpler code as follows:

tell application "Finder" to set myPath to folder of (path to me) as string
set remote_script to myPath & "remote_run"
display dialog remote_script
set x to "Parameter 1"
set y to "Parameter 2"

run script file remote_script with parameters {x, y}

then remote_run is the same as before:

on run {x, y}
	
	display dialog x & return & y
	
end run

and it works perfectly!

cheers
Biz

I was so happy! I searched the archives and found that my question had been answered a year ago and, according to the original poster, the solution “works perfectly”! I copied the sample code to two new scripts, but upon running the “local” I get “Applescript Error: Can’t make some data into expected type.” This occurs on the last line of the local script, “run script…”

I have tried saving the remote_run both as an .scpt and as a .app, putting the files in the same folder, etc., but I always get the same error. It acts like a syntax error in the local script. It never even gets to the remote_run script. Are we sure that this works? Does it still work under Panther? I’d REALLY appreciate some help.

Jerry

:

Hi Jerry

This was done originally on Panther, and it did work a treat, but I wasn’t using Script Editor, I was using ScriptDebugger3, which operates correctly (unlike script editor!). Script Editor looks for the path to Script Editor, instead of the path to the script and gets it’s knickers in a twist from then on.

If you embed the applescript information into an app (i.e. via XCode as it’s free!) then it will work as advertised.

However, to get it to work directly from the scripts ‘without’ ScriptDebugger simply didn’t work for me either! I got the same error messages as you did!

Hope that saves you loads and loads of time!
cheers
Biz

BTW- both scripts were in same directory!

Biz,

I’m happy to see that you’re still on the list! Thanks very much.

I believe I understand everything you said; I too had noticed that Script Editor was putting in the path to itself. I just repeated my experiments, attempting to remove Script Editor from the picture.

I saved both “local_script” (as I call it) and “remote_run” as both .app and .scpt, thus making four files. I put all four files in the same folder, quit Script Editor, and doubleclicked on “local_script.app” in the Finder.

Result: First, a dialog displaying “JerryPbHD:Users:jk:Documents:Applescripts:AudioHijack:remote_run”. Also, the menu bar shows that the frontmost application is “local_script”. This is all as expected. The path shown is correct.

But when I click “OK” in this box, I get another box with the Applescript icon, saying “Can’t make some data into the expected type”.

Arghhh!!! Does this result make sense to you? I would happily buy ScriptDebugger if that might solve this problem, but ultimately I need something that will work from the Finder like this.

Jerry

The problem was that the local_script was looking for a remote_run with no filename extension.

Here’s how I found it. In the last line of local_script, I changed the keyword “file” to “alias”. When I did this, I now got a different error message, saying that that it could not find the file “JerryPbHD:Users:jk:Documents:Applescripts:AudioHijack:remote_run”. Aha!! Note that there is no filename extension on what it was looking for. So, I duplicated remote_run.app and renamed it with no extension. I then reran local_script and it worked! Then, I changed the keyword “alias” back to “file” and it still worked.

If anyone can explain this, I’d still appreciate it, since I don’t understand what I’m doing and thus I’m likely to get in trouble again soon. Why, when I call it “file”, do I get the wrong error message “Can’t make some data into the expected type”, when the error was actually “file not found”?

Jerry

OK, folks I did some more reading, some more experimenting and I think I am beginning to understand this mystical business of scripts calling other scripts, with and without parameters. I’ve written three tutorial/demo scripts and pasted in the text below. These scripts were tested under Mac OS 10.3.5. Requires listening to audio out (speakers or headphones).

‘:D’ Jerry Krinock jerry@ieee.org San Jose, CA USA

------------------ PollyCaller.app --------------------------


(*This is the "root" script.  Run this script to
demonstrate calling the other two "Polly" scripts *)

(* The following lines set up the path to "PollyWithoutHandlers.app" and
"PollyWithHanders.app", assuming, for convenience, that these two
application files are in the same folder as this script, "PollyCaller".
It will work if launched as an application, but will
not work if run from within Script Editor, because "me" will be
the Script Editor, giving (in general) the wrong path.  *)
tell application "Finder" to set myPath to folder of (path to me) as string

set theCall to myPath & "PollyWithoutHandlers.app"
display dialog "About to run script "" & theCall & """
set whatToSay to "No Handler!"
set howManyTimes to 2

run script file theCall with parameters {whatToSay, howManyTimes}

set theCall to myPath & "PollyWithHandlers.app"

set whatToSay to "Hi Polly!"
set howManyTimes to 3

display dialog "About to run script "" & theCall & """

tell application theCall
	sleep2()
	sayIt(whatToSay, howManyTimes)
	quit
end tell

-----------PollyWithoutHandlers.app-------------


on run {theWords, nTimes}
	repeat with i from 1 to nTimes
		say theWords
	end repeat
end run

-----------PollyWithHandlers.app-------------


(* In order for its handler calls to work, this script
must be saved as an application with the option "Stay
open" checked.  Otherwise, it will quit immediately after
the "tell" statement, and subsequent calls to either
handler will result in "Connection is invalid" error. *)

(* demonstrates how "tell" = "run" *)
on run
	say "Running Polly with Handlers!"
end run

(* demo handler with no parameters *)
on sleep2()
	say "sleeping"
	do shell script "sleep 2"
	say "waking"
end sleep2

(* demo handler with parameters *)
on sayIt(theWords, nTimes)
	repeat with i from 1 to nTimes
		say theWords
	end repeat
end sayIt