2010-09-09

"Shell" in REALbasic to access command-line

REAL Studio 2010 offers the "Shell" class to call command-line tools.

Any command you would call in the "Terminal" program in Mac OS X or Linux, or the "Command Prompt" program in Windows, you can programmatically call from your REALbasic code. Any text that would otherwise appear at the console in those programs is returned to your code as a String.

Here's a simple example. If you have installed a Java implementation on your computer (already built into Mac OS X), the "java" command on the console will invoke the Java Virtual Machine. Adding "-version" generates a few lines of text describing what version of the Java platform is installed. Executing "java -version" makes a fine demo of the Shell class. Put the following code on a button's Action event handler.


Dim s As New Shell
dim commandLine as String = "java -version"
s.execute (commandLine)
msgbox s.result
s.Close


You should see a dialog like this:

2010-09-08

Format command

I built a little project to let a programmer new to REALbasic experiment with the "Format" command in REAL Studio 2010 Release 3.1.

http://www.gridsgonewild.com/real_studio/

2010-09-05

Database connection example code for REALbasic.

Here's example REALbasic code for making a connection to REAL Server 2009. The docs have similar code, but this is a bit more complete.

You can copy this code into a PushButton's Action event handler for REAL Studio 2010. Also works in a Web Application if you are using the newly announced Web Edition, in which case you put this code in the Clicked event handler of a Button.

This example assumes a TextField on your form named "dbStatus".

You can make some helper methods to assist with chores such as logging problems, but this code will at least get you started.


dim db as new REALSQLServerDatabase

db.Host="YourDomainNameOrIpAddressGoesHere"
db.port=4430
db.Encryption=128 // Always encrypt your connection when on an untrusted network.
db.UserName="YourUserNameGoesHere" // "admin"
db.Password="YourPasswordGoesHere"
db.DatabaseName = "YourDatabaseNameGoesHere"

if( db.Connect ) then
self.dbStatus.Text = "Success connecting to db server"
else // Error connecting
self.dbStatus.Text = "Error occurred when connecting to database server. Code: " + str(db.ErrorCode) + " " + db.ErrorMessage
end if

dim sql as String = "select * from YourTableGoesHere;"
dim rs as RecordSet = db.SQLSelect( sql)
if(db.Error) then
self.dbStatus.Text = "Error when running a SELECT. Code: " + str(db.ErrorCode) + " " + db.ErrorMessage
else // Else normal.
if( rs = nil ) then
self.dbStatus.Text = "ERROR - RecordSet is nil"
else
self.dbStatus.Text = "Record count = " + str( rs.RecordCount ) // Replace with more useful code.
end if
end if

db.Close

// ------------------------------------------
Exception
if( db <> nil) then
if( db.IsConnected ) then
db.Close
end if
end if

I wish I could delete a blog entry.

'nuf said.

2010-08-17

Released "ePubChecker", my first REAL Studio app.

As a full-blown app, ePubChecker makes a nice demonstration of REAL Studio 2010, with Window menu, About box, etc.



Validates any EPUB book file against the "epubcheck" Java library from Google/Adobe. This validation is handy for any such author, and is required by Apple for iBook submissions.

Unfortunately, I was only able to release on Mac OS X. I'm having technical difficulties with both the Windows and Linux versions. In Windows I appear to have a problem with pathnames in the file system; I should be able to resolve that easily. In Linux, the app immediately quits before any gui appears; not sure what I'm doing wrong there, and debugging has proven futile.

Update: The problem with my Windows edition was neither REALbasic nor my code. The problem was not even with Java which I call to run the "epubcheck" library from Google/Adobe. It turns out that the library itself does not like 8.3 short name paths, à la DOS.

C:\Users\ADMINI~1\Desktop\HOUNDS~1.EPU

C:\Users\Administrator\Desktop\hounds.epub


Short name paths are what we get when calling "ShellPath" on a FolderItem in REALbasic in Windows OS. But when I fed that short name to the epubcheck library, the library reported no such file found. Switching to "AbsolutePath" in REALbasic made epubcheck happy. I suspect the I/O calls the Google/Adobe make are not 8.3 savvy. So for Mac OS X &Linux I call "ShellPath", but for Windows I call "AbsolutePath". Problem solved.


Lesson learned:

Test on all 4* platforms as I develop, rather than waiting to the end.

*REAL Studio 2010 Release 3 now deploys to four platforms: Mac OS X Carbon, Mac OS X Cocoa (still in beta), Windows, and Linux.

iDisk does not like files from FAT (DOS) disks

I often use the iDisk feature on my MobileMe account to move files between computers, to servers, etc.

Unfortunately, I encountered some frustrating errors recently when dragging files to my iDisk. Despite different files and folders, zipping, and even re-naming them, I repeatedly got errors saying the copy could not be completed as a file already existed with that name. Eventually I realized I was dragging directly from a thumb drive (flash memory device) rather than my hard disk. The thumb drive was formatted in Windows-compatible FAT format. Turns out that iDisk does not like copying from that format, at least on a Mac.

Workaround: I moved the items to my Mac's hard drive (in HFS+ format). Then copied to iDisk without a problem.

2010-08-11

Generate a character in REALbasic

As of REAL Studio 2010, the best way to programmatically generate a character is by calling the method "Chr" on a TextEncoding. You may get a particular TextEncoding by name through Encodings. You pass the decimal number of the code point of the needed character.

There are various encodings, but the normal one within REALbasic is UTF-8.

Example, to generate the Tab character using code point 9:

Encodings.UTF8.Chr(9)

To generate a bullet (•):

Encodings.UTF8.Chr(8226)

To generate an ankh (☥):

Encodings.UTF8.Chr(9765)

Beware of the built-in command "Chr" which you call directly in your code without any class prefix. That command is outmoded, and only works with the first 128 characters of ASCII. "Chr" fails with accented characters, bullets, or any of the million other Unicode characters. Make a habit of calling "Encodings.UTF8.Chr" instead.

Tip: For you Mac users, UnicodeChecker is a wonderful tool for looking up any Unicode character. This program is a nice GUI wrapper around a database of the Unicode characters. You can search by name such as "bullet". Free of cost.