Alternative to "On Run" to pass parameter between scripts ?

I have a large script which is an applet. My hope is for the user to run a service script in their web browser which will copy the current URL, call the applet and pass the URL to the applet which will then do the processing. Also, I want the user to be able to just run the applet and paste a URL into the relevant text box in a dialog.

For testing the calling script looks like this:

set copied_URL to "Just a test"
set applet_to_call to "/Users/[home]/Desktop/Code-development.app"
run script applet_to_call with parameters (copied_URL)

So far, the only way I’ve found to pass parameters between scripts is for the called script to have an explicit run handler. It would look like this:

on run (copied_URL as text)
-- Lots of code which processes the URL
end run

This works. But, I get an error if the applet (ie. Code-development.app) is run from within AppleScript. Also, when Code-development.app is run as an application the variable copied_URL contains the text “current application” which seems odd.

Another way to do this would be to avoid passing any parameters. For example, the calling script could copy the URL into clipboard which can then be picked-up by the applet but, that might not be ideal if Clipboard has other things in it. A third method, could be to save the URL into a temporary file which is read by Code-development.app then deleted which seems cumbersome.

Is there another way of having the content of a variable available to another script ?

Thanks.

You can just call a handler. Save this applet:

on doIt(x)
	activate me
	display dialog x
end doIt

Now run this script:

tell application "Macintosh HD:Path:to:Test app.app"
	doIt("Hello")
end tell

Shane, many thanks. I’ve given it a try. At present, the called app doesn’t activate - just a bouncing icon in the Dock. I added activate to the calling script but that didn’t change the behaviour.

Anyway, this is much better than the on run and other methods.

Cheers.

Add a launch statement:

tell application "Macintosh HD:Path:to:Test app.app"
   launch
   doIt("Hello")
end tell

Another, many thanks.

I’ve had to fiddle with the code but it’s working a treat now.

Cheers.

It doesn’t seem odd, the code is simply wrong (not according to the ALG). There are some implicit coercions involved which fails and therefore you get the “current application” value back. This is a fallback if AppleScript cannot coerce certain values. That the other examples worked for you is just luck.

First of all parameters should be a list but to give it more context you could also use:


tell application "myappletname"
	-- call the entry point of the applet
	run script with parameters {copied_URL}
end

or you can call the way you did but again, parameters – as the plural form implies – has to be an list:

set copied_URL to "Just a test"
set applet_to_call to "/Users/[home]/Desktop/Code-development.app"
run script applet_to_call with parameters {copied_URL}

The run handler should look like:

on run argv
	set copied_URL to item 1 of argv
	-- Lots of code which processes the URL
end run