Thursday, September 21, 2017

#1 2017-08-27 01:15:08 am

Neophyte
Member
From:: Canberra, ACT, Australia
Registered: 2017-08-20
Posts: 12
Website

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:

Applescript:

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:

Applescript:

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.


Public servants are people too

Offline

 

#2 2017-08-27 07:49:47 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5125

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

You can just call a handler. Save this applet:

Applescript:

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

Now run this script:

Applescript:

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


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#3 2017-08-27 08:10:02 pm

Neophyte
Member
From:: Canberra, ACT, Australia
Registered: 2017-08-20
Posts: 12
Website

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

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.


Public servants are people too

Offline

 

#4 2017-08-27 08:54:36 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5125

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

Neophyte wrote:

the called app doesn't activate



Add a launch statement:

Applescript:

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


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#5 2017-08-29 01:45:44 am

Neophyte
Member
From:: Canberra, ACT, Australia
Registered: 2017-08-20
Posts: 12
Website

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

Another, many thanks.

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

Cheers.


Public servants are people too

Offline

 

#6 2017-08-29 07:04:56 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2709
Website

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

Neophyte wrote:

Also, when Code-development.app is run as an application the variable copied_URL contains the text "current application" which seems odd.



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:

Applescript:


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:

Applescript:

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:

Applescript:

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

Last edited by DJ Bazzie Wazzie (2017-08-29 07:10:36 am)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)