wilmens™

by William W. Mensah

An even better solution….

  • Saturday Sep 27,2008 11:52 AM
  • By wmensah
  • In myDiary

So in my last blog the solution was to write a python script to perform the search and replace on the gtk_basepaths in the 2 files and then have a batch file call python and pass the script to it. That is great, but only as long as the user already has python installed on the computer. If not, then that could be a problem because there will be no way to execute the python script. And you don’t really want to ask the user to please go and download python before you run the installer because it depends on python…that’s kind of silly.

Ok so here’s the better solution:

Since the user is supposed to be running Windows, the system on which the installer is going to be run on should be able to execute .vbs files (visual basic script) via command prompt (ah, something we already have :) ). So if you write the search and replace algorithm in vbs you can execute this script with the batch file and the world will be safe.

The algorithm for such a visual basic script can be found here:

Here’s the script:


Const ForReading = 1
Const ForWriting = 2

strFileName = Wscript.Arguments(0)
strOldText = Wscript.Arguments(1)
strNewText = Wscript.Arguments(2)

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFileName, ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, strOldText, strNewText)

Set objFile = objFSO.OpenTextFile(strFileName, ForWriting)
objFile.WriteLine strNewText
objFile.Close

?

For a better understanding of how the script works, read the explanation provided at the link above. In my case I had to change a few things within the script to get it to work the way I wanted but I’ll come back to that later.?

Ok, so the script can be executed like so:

cscript replace.vbs "C:\Scripts\Text.txt" "Jim " "James "

where replace.vbs is the name of the script, C:\Scripts\Text.txt is the file which contains the text to be replaced, “Jim” is the text to find and “James” is the replacement.?

Since I wanted to replace <gtk_basepath> with the value of the GTK_BASEPATH environment variable and then change C:\GTK to C:\\GTK for instance (assuming C:\GTK is the value of the environment variable) I changed the line:

strNewText = Wscript.Arguments(2)

to read:

strNewText = Replace(Wscript.Arguments(2), "\", "\\")

and then from the batch file I called the vb script like so:

start replace.vbs <file_to_replace> %GTK_BASEPATH%

note: in command prompt, %GTK_BASEPATH% returns the value of the environment variable.

that did the trick.

Problem Solved!

  • Wednesday Sep 24,2008 09:04 PM
  • By wmensah
  • In myDiary

I finally got everything working just the way I want it. Below is a summary of the problem and the solution to it

Problem:

GTK 2.0 Runtime Environment gets installed in 2 different directories based on which version of Windows you’re running. In my case, Vista and XP

In Vista, it was installed to: C:\Program Files (x86)\Common Files\

In XP, it was installed to C:\

As a result the GTK_BASEPATH environment had 2 different values for each operating system – not such a good thing.

Unfortunately for my python application which requires pango modules to run, the pango.modules and gdk-pixbuf.loaders files had paths to the necessary dll files hard-coded in them.

This is a portion of my pango.modules file

# Pango Modules file

# Automatically generated file, do not edit

#

# ModulesPath = C:\GTK\lib\pango\1.6.0\modules

#

“C:\\GTK\\lib\\pango\\1.6.0\\modules\\pango-arabic-fc.dll” ArabicScriptEngineFc PangoEngineShape PangoRenderFc arabic:* nko:*

“C:\\GTK\\lib\\pango\\1.6.0\\modules\\pango-arabic-lang.dll” ArabicScriptEngineLang PangoEngineLang PangoRenderNone arabic:*

Notice how C:\\GTK has been hard-coded. This created a problem whenever the application was installed and run on a different computer other than mine (on which the hard-coded paths made sense). I suppose the executable file pango-querymodules.exe is supposed to fix this but I had weird results instead. So my solution?….

Solution:

  • Manually go inside these two files: pango.modules and gdk-pixbuf.loaders and replace every instance of GTK’s base path with some string of your choice ( I used <gtk_basepath> for mine).
  • Create a python file to access the two files that has hard-coded paths in them: pango.modules and gdk-pixbuf.loaders and perform a search and replace <gtk_basepath> with the value of the GTK_BASEPATH environment variable. You can do something like:

import os
replacestring = os.environ['GTK_BASEPATH']

where replacestring is a variable that will be used to replace every instance of <gtk_basepath> in both files (watch out for ‘\’ and ‘\\’)

  • Finally create a batch file to execute this python file after installation is complete. You can use the same batch file to delete the python file after that when done because you wont need it anymore.?

My batch file looked like this:

START PYTHON postscript.py

DEL postscript.py

And that’s it, pango.modules and gdk-pixbuf.loaders should now have the correct paths to GTK no matter where it is installed.

I hope this helps someone.