The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   OS X Developer (http://hintsforums.macworld.com/forumdisplay.php?f=27)
-   -   applescript listen for apple event (http://hintsforums.macworld.com/showthread.php?t=73581)

faezbhanji 06-11-2007 09:53 AM

applescript listen for apple event
 
I want to know if this is possible, I need to write a applescript that will reside on the users local drive, it will get launched from my application. Once launched it will sit there and wait and listen to see if or when the user launches safari and I also need to then continue to wait and listen for if the user clicks on a particular button on a web page, is this possible

tw 06-11-2007 12:00 PM

faez, this is still the same topic as the one you launched before - you're shooting yourself in the foot by starting a new thread; you lose all the discussion that went into the last thread.

I think the moderators should re-attach this thread to the end of your other thread.

to answer your question, applescript does not have any explicit event sniffing capability. there are ways you could convince it to wait for Safari to launch (or you could write a launchd plist that would trigger an applescript when Safari launched) but clicking on a button in Safari would not in itself create an apple event (apple events are inter-application, not intra-application).

that being said, I think at this point it would be helpful if you told us what is supposed to happen when a user clicks on this button in Safari. what utility is launched, and what does it do? maybe we can find a workaround to what you want.

faezbhanji 06-11-2007 12:17 PM

hi tw, I thought this should be a new thread because it's a whole new approach. Whereas we were trying to do something from within the browser, now we are trying to do it outside the browser...here's the background

We are using a charting tool that chart's users data and displays it in safari.
All this is local machine no network involved.
The problem is the charting tool does not have a facility that allows you to save the chart or copy it to clipboard - don't ask!
So I have done two things
1.put two buttons on the webpage that displays the chart, "capture" "save"
2.I have written a java utility that basically copies the portion of the screen that contains the chart and saves it as a jpg file or copies it to the clipboard

Now there doesn't seem to be a way to launch my java utility when the user clicks on "save" or "capture" from the webpage. Too many security issues etc etc or so I have been led to believe.

So then I thought ok how can I detect that the user has clicked on the save button on the page and launch the java utility, and I thought of applescript
basically this is my train of thought.
When I launch safari, I will also launch an applescript
The applescript would then sit there and listen, if the user clicks the "save" button, if he does then it would continue on and launch my java utility which would go save the chart and quit.

cwtnospam 06-11-2007 12:17 PM

In addition to what tw said, I still don't see why you don't just provide a link to download the file. Eventually, Microsoft is going to break your app, and Apple is never going to allow it, so why bother?

tw 06-11-2007 12:49 PM

a couple of questions:
  1. the original utility that displays a chart in Safari - how does it display the chart? structured divs? java application? something else I'm not thinking of?
  2. is that original utility itself a java app?
  3. is the information being graphed browser related (history info, or something like that), or is it something else that the browser doesn't have immediate access to?
  4. how complex is the chart? simple table, bar graph, something more exotic?

faezbhanji 06-11-2007 01:49 PM

Quote:

Originally Posted by tw (Post 385135)
a couple of questions:
  1. the original utility that displays a chart in Safari - how does it display the chart? structured divs? java application? something else I'm not thinking of?
  2. is that original utility itself a java app?
  3. is the information being graphed browser related (history info, or something like that), or is it something else that the browser doesn't have immediate access to?
  4. how complex is the chart? simple table, bar graph, something more exotic?

1. Its a flash chart
2. No not a java utility its flash
3. the information that is being graphed is data that the flash gets from a xml file which gets generated when the chart is launched
4. bar graphs to complex stock charts, 3D charts which can be panned.

tw 06-11-2007 02:12 PM

ah, that's more complex than I'd hoped - I was thinking you could use javascript to save portions of the screen, but that's not going to work.

of course, you could always save the entire webpage as an archive, but the next time the utility ran it would change the XML file, which would change the saved webpage, so that wouldn't work...

I take it editing the flash app is out of the question?? and that the app isn't designed so you can right-click to save the current image?

it's easy enough to launch the utility, if that's your concern. just give the user with a pre-made local webpage they can click on that loads the flash application. all the 'capture' button needds to do, then in reload the page and it will rerun the flash. saving is a bit more of a problem, though...

faezbhanji 06-11-2007 02:17 PM

Based on the suggestions I've been getting on this forum would the following work???

1. I create a small file and give it an extension....say *.jzcht
2. I repackage my java jar file to a Mac application bundle
3. I then set up an association with the file *.jzcht and set it open with the java app file
4. I store this *.jzcht in a predefined location on the users server, perhaps in the same place as the datafile for our application
5. On the web page, under the button I put the href to the *.jzcht file on the server
6. Now when the user clicks on the button the browser will attempt to download and open the file. to do that it would launch the java app

So now
1. would this above scenario work
2. Also the url to the *.jzcht file would need to in the form http://192.168.x.x/pmz/myFile.jzcht where 192.168.... would be the internal ip address of the server
3. When the user click on the href will he get any of those security messages saying "this page wishes to download etc etc"

faezbhanji 06-11-2007 02:28 PM

Quote:

Originally Posted by tw (Post 385157)
it's easy enough to launch the utility, if that's your concern. just give the user with a pre-made local webpage they can click on that loads the flash application. all the 'capture' button needds to do, then in reload the page and it will rerun the flash. saving is a bit more of a problem, though...

hi tw, launching the charting utility is not the problem, the problem is launching my java app when the user clicks on the "save" button

tw 06-11-2007 04:34 PM

Quote:

Originally Posted by faezbhanji (Post 385172)
hi tw, launching the charting utility is not the problem, the problem is launching my java app when the user clicks on the "save" button

yeah, I know. just exploring possibilities...

you know, you could simply package the java program with the utility. save you having to worry about downloading and launching it.

faezbhanji 06-11-2007 08:34 PM

Quote:

Originally Posted by tw (Post 385228)
you know, you could simply package the java program with the utility. save you having to worry about downloading and launching it.

huh??....how do you mean....please do tell, how would this work? and I need info here because this is uncharted teritory for me.:confused:

bunnz 06-11-2007 09:47 PM

faezbhanji:

If <all> you need to do is launch your java app from the HTML 'Save' button, do this:

Download and install MisFox or More Internet (both are freeware). Using either one, create a new protocol helper with a name such as 'QuickHack'. Assign your java app to the protocol.

Write a link (or make a button) using the URL form as follows:

<A HREF="QuickHack:">Launch My App</A>

Believe it or not, that's all that should be required...

If it doesn't work, you may have to repackage your java app in a Mac app 'shell' for it to be recognized as a legit helper. I just now assigned Dictionary to QuickHack, and it worked fine.

(In my opinion, being able to assign protocol helpers really ought to be part of the OS... but Apple hasn't supplied the means since OS 9... and the two third party utilities have taken up the slack.)

Hope you can get your project to work.

Peter B.

-----

tw 06-12-2007 12:09 AM

Quote:

Originally Posted by faezbhanji (Post 385281)
huh??....how do you mean....please do tell, how would this work? and I need info here because this is uncharted teritory for me.:confused:

all I mean is this - you have a flash utility and a java app; both can be called from web pages. so, make a folder, put the flash utility in it, put the java app in it, and put an html file there that's designed to embed them both. then (once your user has a copy of the folder), all s/he hasto do is double-click on the html file and a browser will open with the java and flash already installed. no need to worry about accessing the file system or running apps from the browser.

you'll need to use the HTML Object element to embed the flash and java in the webpage.

faezbhanji 06-12-2007 06:10 AM

but what about those ugly security messages, won't she get all those messages.
I already have the flash embedded, it came as instructions with the utility on how to do it. How do I embed the java, and how do I then launch the java app when the user clicks on the "save" button for example
and how come you know so much
and how can I become like you :)

faezbhanji 06-12-2007 07:55 AM

Quote:

Originally Posted by bunnz (Post 385293)
Write a link (or make a button) using the URL form as follows:

<A HREF="QuickHack:">Launch My App</A>

-----

hi peter, what about those ugly security messages will the users get them..."page is trying to launch etc etc"

bunnz 06-12-2007 08:22 AM

faezbhanji:

I think you may get a message the <first> time your app is run on the user's machine... thereafter not.

I won't promise (absolutely) that my suggestion will work... but so far, it seems like the easiest possible solution. Worth a try.

Let us know...

Peter B.

-----

tw 06-12-2007 09:25 AM

Quote:

Originally Posted by faezbhanji (Post 385341)
but what about those ugly security messages, won't she get all those messages.
I already have the flash embedded, it came as instructions with the utility on how to do it. How do I embed the java, and how do I then launch the java app when the user clicks on the "save" button for example
and how come you know so much
and how can I become like you :)

lol - believe me, I don't know that much, and being me is sometimes a very mixed blessing... :D

you get the ugly security messages when you download something to the computer, or otherwise open a vulnerability. if the java app is already on the machine, and being run from within a webpage, there is no security risk and so no security warnings.

to embed and use a java app in a web page do something like this...

add this (modified appropriately) to your HTML to launch and embed the java app (this assumes you have it in .jar format, which may not me true; you can call the class directly if you don't want to use jar):

Code:

    <applet id="javaApp" code="path.in.jar.to.main.class" name="name.of.mainroutine" Archive="javaAppName.jar" codebase="." style="...display considerations if necessary...">
    <param name="param1" value="default1-if needed">
    <param name="param2" value="default2-if needed">
  </applet>

basically, you build the webpage so that when a user clicks on one on the buttons, it triggers a javascript command which runs a top-level function in the java app - generally a standard call like document.javaApp.doThisFunc() (look up HTML DOM if you're not familiar with that syntax). you may need to edit your java code, depending, to add an idle loop and public functions that javascript can access. at least, I'm used to java apps that idle in the background waiting for user input; I suppose you could just trigger the app through javascript, but I've never tried that myself.

faezbhanji 06-12-2007 02:35 PM

Quote:

Originally Posted by tw (Post 385369)
lol -
[CODE]
basically, you build the webpage so that when a user clicks on one on the buttons, it triggers a javascript command which runs a top-level function in the java app - generally a standard call like document.javaApp.doThisFunc() (look up HTML DOM if you're not familiar with that syntax). you may need to edit your java code, depending, to add an idle loop and public functions that javascript can access. at least, I'm used to java apps that idle in the background waiting for user input; I suppose you could just trigger the app through javascript, but I've never tried that myself.

So you mean I have been struggling all these days for nothing, when all I had to do was embed the app in my page.....eek! why didn't you just say so;) just kidding...

Ok so I need add an idle loop, to my java app, are you able to tell me more about idle loops or point me in the right direction.

Will the java app quit itself when the user closes the page, or do I have to set it to quit when the user closes the page. If so how would the page know its being closed?

if I'm going to be calling a public function from within document.javaApp.doThisFunc() can I pass it parameters as well when I call it...so document.javaApp.doThisFunc(prm1, prm2, prm3)

On another note, how do I get the position of the top of the document and the left of the document, when I do "window.screenTop" and "window.screenLeft" it gives me the top left position of the whole window, but I want the top left of the document.

Ok it's 4:30am in the morning so I'm going to crawl into bed....embed java app.....:rolleyes:

bunnz 06-12-2007 07:34 PM

Perhaps I should have started a new thread about this, but it seemed relevant here.

This is motivated by my curiosity and my (near complete) ignorance of java... or of java applets as run from browsers.

If a java app which resides locally (and acts locally) can be embedded so readily in a web page, why is there so much fear and loathing of running other _local_ scripts and apps from links in HTML?

Is the java sandbox that much more restricted than the Mac OS in terms of security?

Thanks for any answers... or pointers to explanations.

Peter B.

-----

cwtnospam 06-12-2007 08:32 PM

Quote:

Originally Posted by bunnz (Post 385548)
Is the java sandbox that much more restricted than the Mac OS in terms of security?

I don't believe that embedded Java will write to the user's hard drive. The app can run, but the only place it can write to is the server.

tw 06-13-2007 12:45 AM

Quote:

Originally Posted by faezbhanji (Post 385470)
So you mean I have been struggling all these days for nothing, when all I had to do was embed the app in my page.....eek! why didn't you just say so;) just kidding...

Wittgenstein once said: "if no one ever did anything foolish, nothing intelligent would ever get done." ;)

Quote:

Originally Posted by faezbhanji (Post 385470)
Ok so I need add an idle loop, to my java app, are you able to tell me more about idle loops or point me in the right direction.

Will the java app quit itself when the user closes the page, or do I have to set it to quit when the user closes the page. If so how would the page know its being closed?

I'd try calling it just as you have it written, first, without building an idle loop. the java gets loaded and called once when the page loads - most likely you can just deal with it as a simple function call after that.

the java app automatically unloads when you close the page - no worries...

Quote:

Originally Posted by faezbhanji (Post 385470)
if I'm going to be calling a public function from within document.javaApp.doThisFunc() can I pass it parameters as well when I call it...so document.javaApp.doThisFunc(prm1, prm2, prm3)

yes. if the function is defined in the java to take a parameter, you can pass it just like normal from javascript. the only thing to consider is that (I think) the passed parameters will automatically be converted to text strings. if you're passing numbers or booleans you'll need to coerce them from text.

Quote:

Originally Posted by faezbhanji (Post 385470)
On another note, how do I get the position of the top of the document and the left of the document, when I do "window.screenTop" and "window.screenLeft" it gives me the top left position of the whole window, but I want the top left of the document.

I think you can safely assume that the top left of the document will always be {0,0} (relative to the window, of course - you don't need to account for the window position on the screen). getting the position of elements in DOM is sometimes a little tricky, though, because different browsers use different conventions. (if I remember correctly, IE gives coordinates relative to the top left of the document, even if it's off-screen, while Mozilla gives them relative to the top-left corner of the visible area; or maybe it's the other way around...)

tw 06-13-2007 01:18 AM

Quote:

Originally Posted by bunnz (Post 385548)
If a java app which resides locally (and acts locally) can be embedded so readily in a web page, why is there so much fear and loathing of running other _local_ scripts and apps from links in HTML?

Is the java sandbox that much more restricted than the Mac OS in terms of security?

cwtnospam (as usual) has it right: java isn't designed to interact with the file system as a whole, but rather to do self-contained processing with user interaction.

the fear and loathing, as you put it, of running other scripts and apps from links in HTML is (maybe) natural. I mean, every time you run any application or script on your machine, it's a bit like getting an injection - it goes right into the machine's bloodstream and bypasses a few levels of protection. the paranoia about running things from HTML is that it feels as though anyone in the world could walk up to you and give you an injection, maybe even without you knowing about it. keeping HTML from running local apps is good prophylaxis, and not one that most people want to relax.

faezbhanji 06-13-2007 05:45 AM

Quote:

Originally Posted by tw (Post 385599)
java isn't designed to interact with the file system as a whole, but rather to do self-contained processing with user interaction.

groan.....not again pleeeease...remember my "save" button. My java app that will get launched or in this case which will run in the background captures the image of the chart, converts it to a jpg and then saves it as a file. But it comes up with a dialog box which prompts the user for the name and location for the file. Then if as you say in your quote above, will I still be able to do this?? I don't mind if the user is only able to save to the same folder as where the java app resides.

tw 06-13-2007 05:56 AM

if you can make jave save to a file, it will do it from the web page.

faezbhanji 06-13-2007 06:01 AM

Quote:

Originally Posted by tw (Post 385624)
if you can make jave save to a file, it will do it from the web page.

hi tw, could you be a bit more specific, I don't quite understand what you mean?

tw 06-13-2007 11:05 AM

Quote:

Originally Posted by faezbhanji (Post 385625)
hi tw, could you be a bit more specific, I don't quite understand what you mean?

I'm just saying that the context of java is not going to impact on how it works. you said you had a java app that saved a file. if so, it will do that if you run it as a stand-alone or as a browser object.

you're in the realm here where you have to experiment and work out the kinks you find. I can't help you much with what amounts to debugging issues.

faezbhanji 06-14-2007 01:29 AM

Quote:

Originally Posted by tw (Post 385369)
lol -
to embed and use a java app in a web page do something like this...

Code:

    <applet id="javaApp" code="path.in.jar.to.main.class" name="name.of.mainroutine" Archive="javaAppName.jar" codebase="." style="...display considerations if necessary...">
    <param name="param1" value="default1-if needed">
    <param name="param2" value="default2-if needed">
  </applet>


hi tw, just trying to figure out where in the page I should put your code, I already have the <object> and <embed> tags in my page. They are being used to embed the flash charting utility. Should your code go in between the <object> tags as well, or should it reside outside the object tag.

I tried googling "java application embed webpage" and other variations on this to try and find out for myself, but I couldn't find any examples of places where a java app is actually being embedded, although there seem to be many where there are applets being embedded.

If you could also point me to some further reading material on this I would really appreciate it.

tw 06-14-2007 01:52 AM

Quote:

Originally Posted by faezbhanji (Post 385861)
hi tw, just trying to figure out where in the page I should put your code, I already have the <object> and <embed> tags in my page. They are being used to embed the flash charting utility. Should your code go in between the <object> tags as well, or should it reside outside the object tag.

honestly, you would normally the applet object in your page in the place where you would want it to display (outside of the object or embed tags that you're using for the flash - this is a separate object, yah?). since your java doesn't have a display component, and all you're trying to to is load it into the page so you can run it, you can place it anywhere in the body of the html that you like. you can just use the applet block, or convert it to an object format (see below): no need to enclose those in any other html block.

best thing to do is find someone who is giving the source code for an embedded java app. I first learned how to do this by looking at the source code for MyPhysicsLab, which I needed to adapt for a project I was doing. it's complex programming to start with (though very well done - hats off to Erik Neumann) - you might want to see if there's some other simpler source code out there.

maybe this will help, too - WC3 standards for objects and applets in HTML 4

faezbhanji 06-14-2007 03:12 AM

Hi Tw,
greetings from down-under, could you have a look at the code below, this is what I have put in my page, but the java app doesn't seem to want to run, it should pop-up with the SaveAs dialog, but it doesn't do anything.

Code:

<APPLET name="app1" code="savechart.class" archive="SaveChart.jar" codebase="." >
<param name="inputtop" value="0">
<param name="inputleft" value="0">
<param name="inputwidth" value="700">
<param name="inputheight" value="800">
</APPLET>

Also I'm not sure about the param names and what I should be putting there, the main in java always takes only one parameter which is args[], which in my case has 4 elements, these are then being saved into variables called inputtop, inputleft etc etc which is the reason why I show them here in the param name section I'm not sure if that's what I need to do.

I can launch the jar file from the terminal so that is working fine, which would mean something in my html code above is not right

I'm publishing my java code below, could you please compile it and save it as a jar file and see if you can get it to run.
to run it all you need to do is give the jar file 4 parameters say 0 0 500 700, these are the rectangle of the screen shot it will take of the screen.

Code:

import java.awt.image.BufferedImage;
    import java.io.*;
    import java.awt.*;
    import java.awt.Robot;
    import java.awt.Image.*;
    import com.sun.image.codec.jpeg.*;
   
public class savechart {
 
 
    public savechart(int top, int left, int width, int height) {
      try {
          BufferedImage capture = null;
          Rectangle area = new Rectangle(top, left, width, height);
          Robot robot = new Robot();
          capture = robot.createScreenCapture(area);
          Frame frame = new Frame();
          FileDialog fDialog = new FileDialog(frame,"Save Chart",FileDialog.SAVE);
          fDialog.setFile("*.jpeg");
          fDialog.setVisible(true);
          fDialog.requestFocus();
          String A = fDialog.getDirectory();
          String B = fDialog.getFile();
          String C = System.getProperty("file.seperator");
          String imgFile = fDialog.getDirectory() + fDialog.getFile();
          FileOutputStream out = new FileOutputStream(imgFile);
          JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
          encoder.encode(capture);
          out.flush();
          out.close();
          System.exit(0);
         
      } catch (Exception exc){
          System.out.println("errors ");
          exc.printStackTrace();
      }
     
      finally {
          System.out.println("Error Creating Image");
          System.exit(0);
         
      }
    }
   
    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
      int inputtop = Integer.parseInt(args[0]);
      int inputleft = Integer.parseInt(args[1]);
      int inputwidth = Integer.parseInt(args[2]);
      int inputheight = Integer.parseInt(args[3]);
           
      new savechart(inputtop,inputleft,inputwidth,inputheight);
    }
   
}


tw 06-14-2007 09:57 AM

faez,

you're almost there, I can see that. play with it for a week and see if you can iron out the kinks.

faezbhanji 06-14-2007 10:31 AM

Quote:

Originally Posted by tw (Post 385911)
faez,

you're almost there, I can see that. play with it for a week and see if you can iron out the kinks.

:eek: a week! this is due monday, I'm supposed to be doing a presentation on monday with my boss and the client! I have been struggling with this for the last two weeks....c'mon dude...you gotta help me out here.

I'm assuming my java code is ok and the problem is in my html tags am I right?
Also with those params how am I supposed to use them?

it would be kewl if you help, but if you can't well..then I'm up sh!t creek...:confused:

faezbhanji 06-17-2007 03:42 AM

I have worked on this all weekend, I'm not having much luck getting it to run
if anyone out there can help I would greatly appreciate it.


All times are GMT -5. The time now is 05:51 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.