HOW TO: Run an AppleScript (and shell script)

Find out how to get things done with Servoy. Post how YOU get things done with Servoy

HOW TO: Run an AppleScript (and shell script)

Postby david » Tue Nov 18, 2003 11:36 pm

1) Do you want to run AppleScripts from Servoy? It's easy and it works in Developer and Client (on a Mac of course).

Create a sample script:

>tell application "Finder"
> say "sweet!"
>end tell

Save it as text format.

Load the file into a blob field. (Make sure the field type is 'media', then you can right-click on the field to load a file into the field.)

Create a method that will run the script stored in the blob field:

>application.writeFile('my_script', blob_field);
>application.executeProgram('osascript','my_script');

Storing the AppleScript in a blob field has a major advantage: you don't have to worry about whether or not the script is present on the client machine in the correct directory. The disadvantage is that if the script is large and you use it a lot, saving the script every time adds a lot overhead.

2) Let's run an AppleScript that runs a shell command that returns the contents of the current working directory.

Here's the AppleScript:

>set my_folder to (do shell script "ls")

Here's the Servoy method:

>application.writeFile('my_script', blob_field);
>globals.terminal_result=application.executeProgram('osascript','my_script');

To see the results, put "globals.terminal_result" on a form as a text area field.

Summary: don't blow your machine up with all this power. :P

- david
Last edited by david on Wed Nov 19, 2003 5:19 am, edited 1 time in total.
User avatar
david
 
Posts: 1727
Joined: Thu Apr 24, 2003 4:18 pm
Location: Washington, D.C.

Postby Harjo » Tue Nov 18, 2003 11:53 pm

Great!, but nog cross-platform! :(
Harjo
 
Posts: 4321
Joined: Fri Apr 25, 2003 11:42 pm
Location: DEN HAM OV, The Netherlands

Postby david » Wed Nov 19, 2003 12:15 am

HJK wrote:Great!, but nog cross-platform! :(


Use this to detect the client operating system:

//Returns the name of the operating system
var osname = application.getOSName();

Then run an AppleScript or the equivalent VBScript depending on what platform is returned.

Someone else will have to do the "HOW TO" for windows (and linux). I can't imagine it being much different though -- maybe just change out "osascript" with the VBScript equivalent when calling application.executeProgram().

- david
User avatar
david
 
Posts: 1727
Joined: Thu Apr 24, 2003 4:18 pm
Location: Washington, D.C.

Re: HOW TO: Run an AppleScript (and shell script)

Postby Riccardino » Thu Nov 20, 2003 12:31 am

david wrote:1)
Summary: don't blow your machine up with all this power. :P

- david


Too late. :wink:
But I can't make the procedure work with other apps. I tried to open an image using Preview, but I get a "cannot execute" error...
Have you tried with apps other than applescript, by any chance?
User avatar
Riccardino
 
Posts: 911
Joined: Thu Apr 24, 2003 11:42 am
Location: Ferrara, Italy

Re: HOW TO: Run an AppleScript (and shell script)

Postby david » Thu Nov 20, 2003 5:19 pm

Riccardino wrote:But I can't make the procedure work with other apps. I tried to open an image using Preview, but I get a "cannot execute" error...
Have you tried with apps other than applescript, by any chance?


In OS X terminal, this command opens a pdf in Preview:

open -a preview /Users/admin/Desktop/93260403.pdf

The parameters for executeProgram would look like this then:

application.executeProgram('open','-a','preview','/Users/admin/Desktop/93260403.pdf');

Combine this with the applescript from the first post, you could return a list of all the jpg files in a directory and then open them in preview. All without leaving Servoy.

- david
User avatar
david
 
Posts: 1727
Joined: Thu Apr 24, 2003 4:18 pm
Location: Washington, D.C.

Re: HOW TO: Run an AppleScript (and shell script)

Postby Riccardino » Thu Nov 20, 2003 8:32 pm

david wrote:
Riccardino wrote:But I can't make the procedure work with other apps. I tried to open an image using Preview, but I get a "cannot execute" error...
Have you tried with apps other than applescript, by any chance?


In OS X terminal, this command opens a pdf in Preview:

open -a preview /Users/admin/Desktop/93260403.pdf

The parameters for executeProgram would look like this then:

application.executeProgram('open','-a','preview','/Users/admin/Desktop/93260403.pdf');

Combine this with the applescript from the first post, you could return a list of all the jpg files in a directory and then open them in preview. All without leaving Servoy.

- david


I tried and it works. Thanks :)
But, I'm just curios, why do we have to use a shell command to open an application? Isn't it possible to call directly an app and make it launch the file, as the sample in method editor shows for Windows environment?
User avatar
Riccardino
 
Posts: 911
Joined: Thu Apr 24, 2003 11:42 am
Location: Ferrara, Italy

Re: HOW TO: Run an AppleScript (and shell script)

Postby david » Thu Nov 20, 2003 10:10 pm

Riccardino wrote:But, I'm just curios, why do we have to use a shell command to open an application? Isn't it possible to call directly an app and make it launch the file, as the sample in method editor shows for Windows environment?


My assumption about application.executeProgram() is that it doesn't open a program directly but passes the command string on to the operating system "console" which does the actual execution.

The example given works for windows because that's the correct command string for the windows console.

For OS X, the operating system "console" is the default shell which you normally access with the terminal app. Hence, the sample in the method editor -- no matter how you format the paths -- will not work on OS X because that's not how you specify opening a file using the OS X default shell.

If my guesses are correct, the windows example and my example are the same thing. If I'm not correct...hey, at least it works!

- david
User avatar
david
 
Posts: 1727
Joined: Thu Apr 24, 2003 4:18 pm
Location: Washington, D.C.

Postby david » Sat Nov 22, 2003 12:01 am

Example #3:

You don't have to store an applescript as a file in order to run it from Servoy -- you can type the applescript string into a Servoy method directly (use the osascript shell command format):

>application.executeProgram('osascript','-e','tell application "Finder" to say "David"');

Which gives you the ability to use Servoy field data to build your applescript string:

application.executeProgram('osascript','-e','tell application "Finder" to say "' + contactname + '"');

I've kept this example simple to get my point across. I hope that it is obvious that you can do a LOT with this -- up to and including building an entire AppleScript "wizard" in Servoy a la the SQL wizard that is included in the CRM example solution.

- david
User avatar
david
 
Posts: 1727
Joined: Thu Apr 24, 2003 4:18 pm
Location: Washington, D.C.

Postby Riccardino » Sat Nov 22, 2003 12:26 pm

david wrote:I've kept this example simple to get my point across. I hope that it is obvious that you can do a LOT with this
- david


Yes, indeed. :)

I'm going to test it with applescripts I use more often (most of them are related to pdf services).

It sounds promising... :wink:
User avatar
Riccardino
 
Posts: 911
Joined: Thu Apr 24, 2003 11:42 am
Location: Ferrara, Italy

Postby Harjo » Tue Nov 25, 2003 4:10 pm

Hi David

open -a preview /Users/admin/Desktop/93260403.pdf

Do you know a vbscript, which does this?? or a workaround?
Im trying to launch files (not programs!) within Servoy on Windows.
I can't get right!
Harjo
 
Posts: 4321
Joined: Fri Apr 25, 2003 11:42 pm
Location: DEN HAM OV, The Netherlands

Postby david » Tue Nov 25, 2003 7:36 pm

HJK wrote:Do you know a vbscript, which does this?? or a workaround? Im trying to launch files (not programs!) within Servoy on Windows. I can't get right!


I was afraid you would ask this question HJK....

For starters, you already know that the example for application.executeProgram() doesn't work as the example indicates. You need to have forward slashes in the paths instead of back slashes:

application.executeProgram('C:/path to excel program', 'C:/path to excel file'); //this works

This approach has it's problems in that you have to specify a path to the program. This post discusses in detail:

http://forum.servoy.com/viewtopic.php?t=221

Now, here is where I reach my limitations. I don't know how to open a file using VBScript. But I can't imagine it being a big deal if you know VBScript -- unless it is a big deal because they kept file management out of VBScript since it was primarily intended as a client-side scripting language for the web. However, I think there are extensions or something to get around this limitation but I may be dead wrong.

Here's the next best thing -- how to run a VBScript using application.executeProgram():

1) open note pad -- type "wscript.echo "hello dude" -- save as "hello.vbs"
2) run cmd -- navigate to the directory you saved hello.vbs -- type in "cscript hello.vbs" -- as you might expect, it returns "hello dude."
3) in Servoy, create a method with this line: "field_name = application.executeProgram('cscript','C:/path to file/hello.vbs'); -- create a button to run it and put the field "field_name" on a form, and "hello dude" is returned to the field.

Somebody else will need to take it from here. I'm would think that you could utilize a number of different scripting languages in this fashion (replace out 'cscript' with equivalent command to run a perl script for example? -- just guessing here). All I can confirm is that you can run .vbs files this way so whatever you can put in one of them is going to work from Servoy.

I'd be interested in the VBScript to open a file myself if someone could post it.

Cheers - david
User avatar
david
 
Posts: 1727
Joined: Thu Apr 24, 2003 4:18 pm
Location: Washington, D.C.

Postby wim » Tue Nov 25, 2003 11:07 pm

david wrote:
I'd be interested in the VBScript to open a file myself if someone could post it.

Cheers - david


My favourite subject: VBScript.

A very simple example that will open a file with the app associated with the externsion;

set SH = wscript.createobject("WScript.Shell")
SH.run("c:\whateverFile.pdf")

If you want to control what app opens the file, you need to change the run command to include the app executable and pass the filename as an argument.

I like the idea of storing VBscripts in a BLOB, it's a lot eaiser to edit them that way then to nest them directly in a method.

Wim
wim
 
Posts: 4
Joined: Wed Jul 02, 2003 1:13 am
Location: Toronto, ON

Postby Harjo » Tue Nov 25, 2003 11:17 pm

I have file called: test.vbs with
Code: Select all
set SH = wscript.createobject("WScript.Shell")
SH.run("c:\1.pdf")


if I run it, nothing happened.
the file 1.pdf is there! What am I doing wrong??
btw using win xp
Harjo
 
Posts: 4321
Joined: Fri Apr 25, 2003 11:42 pm
Location: DEN HAM OV, The Netherlands

Postby wim » Tue Nov 25, 2003 11:41 pm

Make sure you have an app associated with the "PDF" extension.

To test: open the Run dialog and type in "c:\1.pdf" (without the quotes)

If nothing happens, there's a problem with the extension association. This is the problem of relying on things like that.

As I hinted at in the previous post, it's better (but more complex) to launch the app and tell it what file to open.

All the MS apps are very nice in that respect, they have a very open API that will let you do a bunch of things with VBscript.

Remember that the Windows Script Host will run more than VBscript. If you are more familiar with javascript syntax you can use Jscript.

VBscript is installed by default and comes with a very nice helpfile. Do a search for "vbs*.chm" (I forget what the complete filename is).

HTH

Wim
wim
 
Posts: 4
Joined: Wed Jul 02, 2003 1:13 am
Location: Toronto, ON

Postby Harjo » Tue Nov 25, 2003 11:52 pm

Yes, running: c:\1.pdf (manualy)
is opening my pdf nicely in acrobat reader 6
but running the file: test.vbs is doing nothing!
Harjo
 
Posts: 4321
Joined: Fri Apr 25, 2003 11:42 pm
Location: DEN HAM OV, The Netherlands

Next

Return to How To

Who is online

Users browsing this forum: No registered users and 9 guests