Custom function

Hi,

Is is possible to create a custom function in ss like scrapeableFile.resolveRelativeURL(). Or in other words creating my own object / class so i can call it in scripts without having to build my own jar file.

A second desire is to apply the sutil.nullToEmptyString function to all session variables. Is this possible? Now i have to add the sutil.nullToEmptyString function many times in my scripting. Applying globally to all session variables would save me some work. In other words return an empty string if a session variable does not exists instead of a null value.

Third question is it possible to clear all session variables that contain some text (like sessionsession.clearAllSessionVariables()). For example clear all session variables that contain the word "temp". I am assuming that it is still not possible to loop through all session variables. If this is possible i would very much like to know how.

Kind regards
Nebu

You can add a custom library,

You can add a custom library, and instructions are here.

There are ways to loop session variables, but I think the question might be due to a misconception. Most of the time, session variables are used to navigate between pages, or to return information back to a web-service, and when used that way you don't need to see them all at once. If you're looking to prep your data for output, I'd point you to work with the data in a dataRecord or dataSet. If you could tell me a little more what you're trying to do, I should be able to come up with more specific direction.

1. I am aware of the

1. I am aware of the possibility to add custom libraries. I even use them. But in development it is very unpractical to have a lib that needs to be recreated in another program and export and imported it in ss each time it is updated. It would be far more efficient to be able to add some classes in ss itself. This way i could alter the class in ss and if it is not possible to reload the class on the fly only a restart of ss would be required.

2. Could you reply on my question to return an empty string for undefined variables instead of a null value. Null values require more programming to deal with. In my case emty strings would work much better and require less programming. Now i apply sutil.nullToEmptyString to almost all variables in my scripts.

3. ( loop session variables) Below a schematic example of what i am doing.

* Scrapeablefile (Resultspage)
  + pattern (extract list of objects)
    - script (goto object page: run script after each pattern match)
      * Scrapeablefile (Object)
        + pattern (extract photos section)
          - script (regex extract multiple photos: run script after pattern is applied)
            - script (save each photo: execute script for each photo)
        .. run other patterns and Scrapeablefiles
        - script (save photos and other data and empty all object related session data: after file is scraped)

I am using session variables so that the information stored in a variable is available accross different scripts and scrapeablefiles. I also use dataset and datarecords but i store them in session variables.

When adding a breakpoint all session variables are listed. I would still love to know how i can get them myself.

I believe everything you want

I believe everything you want to do is possible in the latest alpha. Most of the changes that allow what you want have been added since version 6.0, or at least easy ways to do them were added since then.

1 - This is actually possible. For interpreted Java we are using BeanShell. You can read more about the various functions on the BeanShell site. I occasionally reference the Manual. If you are using version 6.0 or previous of screen-scraper you can do it using Scripted Objects (see the BeanShell site for information on that). If you are on the latest alpha you can actually create a class in the script. Note that it doesn't support generics, annotations, synchronized, or final. You can get around the synchronized issue by using the locks from the java.util.concurrent.locks package (if you need locking for anything).

class SomeClass
{
  private int x = 0;
  public SomeClass()
  {
    // Constructor...
  }

  public void setX(int newX)
  {
    // Note that with BeanShell there are some quirks here.  You'll always want to use the "this" reference when setting a value or it'll create a new variable and assign to it rather than assigning to the data member of the class
    this.x = newX;

    // Bad would be (this won't work as expected):
    // x = newX;
  }

  public int getX()
  {
    // When referencing the value instead of assigning to it you don't need the "this" reference.  I prefer to still use it just so I don't run into problems
    return x;
  }
}

SomeClass object = new SomeClass();

log.info(object.getX());
object.setX(6);
log.info(object.getX());

// You can get the object reference out later.  Granted the class definition isn't available in other scripts, so you can't create the object of that type in another script.  One way around this is to create a factory class and define an object of it to store in the session.  Then in other scripts you can call the method on that object to create instances of objects you don't have a class definition for in the current script.
session.setVariable("MY_OBJECT", object);

2 - This can be done. You'll need the latest alpha. It's using a feature that was recently added but isn't documented on the site yet.

import com.screenscraper.events.*;
import com.screenscraper.events.session.*;

EventHandler returnEmptyStringForNullVariables = new EventHandler()
{
  /**
   * You don't have to define this method, it's more for debugging.  When errors occur in the handler it'll log the name
   * as returned by this method, or if this isn't defined it'll return a generic name
   */

  public String getHandlerName()
  {
    return "Return empty string instead of null for session variables";
  }

  /**
   * Called when the event is fired
   *
   * @param fireTime The time the event was triggered.
   * @param data The current data
   *
   * @return The value of the response.  Depending on the fireTime value, the response means different things or may  be ignored
   */

  public Object handleEvent(EventFireTime fireTime, SessionEventData data)
  {
    Object value = data.getVariableValue();
    if(value == null)
    {
      value = "";
    }
    // In the case of returning a session variable, the return value is the value to actually return for the variable
    return value;
  }
};
session.addEventCallback(SessionEventFireTime.SessionVariableRetrieved, returnEmptyStringForNullVariables);

3 - There is a method you can use for this.

// Since version 6.0;
Map variables = session.getAllVariables();

// Version 6.0 and previous
Iterator iterator = session.getScrapingSessionState().getSessionVariableKeysIterator();
while(iterator.hasNext())
{
  String key = iterator.next();
  log.info(key + " => " + session.getVariable(key));
}

Wow, big improvement IMO. I

Wow, big improvement IMO. I need several methods to be rewritten from coldfusion (web language like php, but based on java) so i can directly use them in screen scraper. Since my knowledge of java is limited is it possible to get a quote for rewritting my scripts. I have about 22 scripts (+/- 600 rows of coding) written in coldfusion that would need to be recoded. A simple script looks something like this.

<cffunction name="textStripHTMLWithDelimiter">
        <cfargument name="in">
        <cfset in = REReplacenocase(in,'(</li.*?>|</div*?>|<br*?>|</p*?>)','. ','all')>
        <cfset in = REReplace(in,'<[^>]*>','','all')>
        <cfset in = ReplaceNoCase(in,'&nbsp;',' ','all')>
        <cfreturn in>
</cffunction>

My goal is to be able to apply these methods / functions directly to variables in ss. Ideally these methods would extend the sutils class. So i call a method like:

sutil.textStripHTMLWithDelimiter(session.getv("MYVAR"));

Anyway let me hear what you guys can do.