I have been asked to turn a script that is in a button into a agent. Only problem is, the script uses a NotesUIDocument object to do all it’s work.
I can’t really change the original script, as it is importing images into a rich text feild and it needs the uidoc to do that.
Is there a way that I can simulate the button click in an agent? I know it can be done in javascript, but can Java or any of the other choices do this?
Thanks in advance.
Subject: Simulate Clicking
I’m a little fuzzy on what’s going on here. Under what circumstances will this agent run? Is the aim to move your code to a scheduled agent, which runs behind the scenes without an open Notes client running it?
Subject: RE: Simulate Clicking
Basically, right now, we have a button that takes some feilds in form and makes some charts in excel with them. Once the charts are made they are exported to gifs then imported into the form. It happend automatically and the user never sees excel.
This runs great, but it was suggest that it would be better if you could just select the forms that you want updated from a few and click one button to update all the charts in the various forms.
So, I just copy and pasted the code into an agent and hit run. Problem is, I use a NotesUIDocument object in the code to access and manipulate the RTF that hold the graphs.
You can not use NotesUIDocuments in agents, it says so in the doumentation. Thus why I’m looking to see if I can just simulate the button click.
Subject: RE: Simulate Clicking
You can use NotesUIDocument in an agent. I do it all the time (well, not all the time, but frequently enough). But the context in which the agent is triggered is key. As long as there’s a Notes document open and in the foreground of the Notes client, it’ll work. Your problem here, then, is that there isn’t an open document. It sounds like you’re selecting a batch of documents in a view or folder and running the agent against them. It’s the lack of an open document that’s the problem, not simulating a specific form event.
You’ve got two options here. One is to loop through the documents and use the NotesUIWorkspace.EditDocument method to open each document individually, thereby creating a NotesUIDocument you can get your hands on, and applying your existing code to it. The other is to modify your code to use back-end classes exclusively. Without knowing exactly how your code works, I can’t say which is better or even if one or the other is really possible, but those are the directions I’d take.
Subject: RE: Simulate Clicking
As M Riggsby pointed out, you can use NotesUIDocuments in agents. I also do it from time to time. I think what might be confusing you is a partial memory of this passage from the NotesUIWorkspace documentation:
“You cannot use the UI classes in a background agent, an agent called through an API, or an agent called by the NotesAgent Run method. Only workstation users can run scripts that access UI objects.”
Note, however, that it specifically details the conditions where agents cannot be used. 
At any rate, knowing more about your situation I’d just be repeating what M Riggsby said except that I’m currently at the “path of least resistance” stage on my current project so my initial thought would be to lean towards his first suggestion: walk through all the documents in a view and get a handle to the NotesUIDocument so I could then use most of the existing code as is.
Subject: RE: Simulate Clicking
First, you can call an agent from an action button using
@Command([ToolsRunMacro];“agentname”).
You are going to have to modify the script so that it operates on “NotesDocument” objects not a “NotesUIDocument” object. You can create a “NotesDocumentCollection” of documents selected in a view using the “UnprocessedDocuments” property of “NotesDatabase”. Then process each document in the collection.
Subject: RE: Simulate Clicking
Can I import a gif into a rich text feild if I use a NotesDocument instead of a NotesUIDocument? Maybe I just don’t know the method call for that.
Subject: RE: Simulate Clicking
I belive you’d use the EmbedObject method of the NotesRichTextItem class, although I’m not sure that will actually work.
Subject: Simulate Clicking
For the most part, you should be able to simply move the code out of the button, into an agent, and just call the agent with the button. There might be some changes necessary to how you get your handle to the current document but that’s it. Then you’ll have the added bonus of not having your code hiding in a button on a form somewhere. 