HOW TO: Run an AppleScript (and shell script)

  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.

  1. 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

Great!, but nog cross-platform! :(

HJK:
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

david:
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?

Riccardino:
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

david:

Riccardino:
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?

Riccardino:
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

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

david:
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:

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!

HJK:
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

david:
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

I have file called: test.vbs with

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

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

Yes, running: c:\1.pdf (manualy)
is opening my pdf nicely in acrobat reader 6
but running the file: test.vbs is doing nothing!

See if you can run any other VBscript. If not your virus scanner might be set up to block VBS files. I believe Norton does.

Its very strange. from the 10 times that I doubleclick the script is started once.
the other times I’ll get runtime error.
(if I start with cscript test.vbs //x : debugger mode)
virusscanner is not the issue. (is off!)

set SH = wscript.createobject(“WScript.Shell”)
SH.run “c:\whateverFile.pdf”, 0, false

  • david

Here is the code for Windows to start/launch a file with the associated program.
The vbscript does not allways run, because of security issues (virusscanner etc..)

if(utils.stringMiddle(application.getOSName(),1,7) == "Windows")
{
	application.executeProgram('rundll32', 'url.dll,FileProtocolHandler','C:/out.pdf')
}

In this example the file: C:/out.pdf is launched.

Jan Blok pointed me in the right direction. Thanks Jan.

on mac you can use the open command to achieve the same