Area de-PHPizzata  —  This area is free from PHP crapware

21 giugno 2009

F. Stella, Vita e gite

F. Stella, Vita e gite

Etichette:

19 giugno 2009

Read a UTF-encoded file using Applescript

Recently I had to shuffle a lot with XML, using applescript together with “TestXSLT” by Marc Liyanage.
Everything was fine while I was reading ISO-8859-1 encoded files and outputting UTF-8 (directly saved by TestXSLT), while some trouble was getting out when the input file was Unicode.

Unfortunately TestXSLT only allows command “set XML code to…” passing the text of the XML, rather than file path, then we need to read correctly Unicode text from inside Applescript.

Here it is my original handler for text reading:

on readFile(filepath)
set foo to (open for access (POSIX file filepath))
set txt to (read foo for (get eof foo))
close access foo
return txt
end readFile


I had to change it slightly in order to read UTF-16 Unicode:

on readFile(filepath)
set foo to (open for access (POSIX file filepath))
set txt to (read foo for (get eof foo) as Unicode text)
close access foo
return txt
end readFile


And this is for UTF-8:

on readFile(filepath)
set foo to (open for access (POSIX file filepath))
set txt to (read foo for (get eof foo) as «class utf8»)
close access foo
return txt
end readFile

Etichette: ,

17 giugno 2009

How-to use "addProgressListener" in XULRunner…

I originally posted this in mozilla.dev.tech.xul newsgroup, server news.mozilla.org, on Nov, 23rd 2006.

/*
First, we’ll associate the progress listener for a "browser"
to a listener object:
*/

browserObject.addProgressListener( listObj,
Components.interfaces.nsIWebProgress.NOTIFY_STATE_WINDOW );

// Remember to define the object, something like this:

listObj = new Object();

listObj.wpl = Components.interfaces.nsIWebProgressListener;

listObj.QueryInterface = function( aIID ) {
if (aIID.equals(listObj.wpl) ||
aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
aIID.equals(Components.interfaces.nsISupports)) return this;
throw Components.results.NS_NOINTERFACE;
}

listObj.onStateChange = function(aProgress, aRequest, aFlag, aStatus) {
if (aFlag & listObj.wpl.STATE_START) {
// This fires when the load event is initiated
} else {
if (aFlag & listObj.wpl.STATE_STOP) {
if ( aFlag & listObj.wpl.STATE_IS_WINDOW ) {
// This fires when ALL load finish
}
if ( aFlag & listObj.wpl.STATE_IS_NETWORK ) {
// Fires when ALL load are REALLY over,
// do something "final" here
} else {
// This fires when a load finishes
}
}
}
return 0;
}

// This fires when the location bar changes i.e load event is confirmed
// or when the user switches tabs
listObj.onLocationChange = function(aProgress, aRequest, aURI) {
// do whatever you want to do
return 0;
}

// For definitions of the remaining functions see XulPlanet.com
listObj.onProgressChange    = function() { return 0 };
listObj.onStatusChange      = function() { return 0 };
listObj.onSecurityChange    = function() { return 0 };
listObj.onLinkIconAvailable = function() { return 0 };

/*
I use the progress listener to trap the end of a local HTML
"template" file loading, and run XSLT transormations.
In addition I need to evaluate the browser contentDocument DOM
in order to clean "target" attributes on links, and so on.

HTH
*/

Etichette:

16 giugno 2009

16 Giugno 1988

Oggi, 21 anni fa, moriva Andrea Pazienza.



Non c'è molto altro da dire, se non Grazie, Paz!

That’s All Folks!