GPT-4 writing Xojo code

I decided to pony up for the paid version of ChatGPT, which includes access to the GPT-4 model. And I’m glad I did, it’s code-generation is exponentially better than GPT 3.5. I’ve had it writing all kinds of things for me in various languages, including Xojo.

For example, I asked it to write me a Xojo method that would extract the individual layers from a Photoshop PSD file as PNG files on MacOS, here’s what it gave me:

To extract the layers of a PSD file into individual PNG files using Xojo, you’ll need a third-party library, as Xojo does not have built-in support for handling PSD files. The ImageMagick library is a powerful tool that can be used for this purpose.
To use the ImageMagick library with Xojo on macOS, you’ll first need to install the library. You can use Homebrew to install it:

brew install imagemagick

Then, add a declare statement to call the ImageMagick library. Here’s a Xojo method that extracts the layers of a PSD file and saves them as individual PNG files:

Function ExtractPSDLayersToPNGs(psdFilePath As String, outputFolder As String) As Boolean

Declare Function MagickWandGenesis Lib "/opt/homebrew/Cellar/imagemagick/7.1.1-6/lib/libMagickWand-7.Q16HDRI.dylib" Alias "MagickWandGenesis" () As Integer
Declare Function NewMagickWand Lib "/opt/homebrew/Cellar/imagemagick/7.1.1-6/lib/libMagickWand-7.Q16HDRI.dylib" Alias "NewMagickWand" () As Ptr
Declare Function MagickReadImage Lib "/opt/homebrew/Cellar/imagemagick/7.1.1-6/lib/libMagickWand-7.Q16HDRI.dylib" Alias "MagickReadImage" (wand As Ptr, filename As CString) As Integer
Declare Function MagickSetIteratorIndex Lib "/opt/homebrew/Cellar/imagemagick/7.1.1-6/lib/libMagickWand-7.Q16HDRI.dylib" Alias "MagickSetIteratorIndex" (wand As Ptr, index As Integer) As Integer
Declare Function MagickGetNumberImages Lib "/opt/homebrew/Cellar/imagemagick/7.1.1-6/lib/libMagickWand-7.Q16HDRI.dylib" Alias "MagickGetNumberImages" (wand As Ptr) As Integer
Declare Function MagickWriteImage Lib "/opt/homebrew/Cellar/imagemagick/7.1.1-6/lib/libMagickWand-7.Q16HDRI.dylib" Alias "MagickWriteImage" (wand As Ptr, filename As CString) As Integer
Declare Sub DestroyMagickWand Lib "/opt/homebrew/Cellar/imagemagick/7.1.1-6/lib/libMagickWand-7.Q16HDRI.dylib" Alias "DestroyMagickWand" (wand As Ptr)


var NulInt as integer

NulInt = MagickWandGenesis()

Var wand As Ptr = NewMagickWand()
Var readResult As Integer = MagickReadImage(wand, psdFilePath)

If readResult = 0 Then
  MessageBox("Failed to read the PSD file. Make sure the file path is correct.")
  DestroyMagickWand(wand)
  Return False
End If

Var layerCount As Integer = MagickGetNumberImages(wand)

For layerIndex As Integer = 0 To layerCount - 1
  NulInt = MagickSetIteratorIndex(wand, layerIndex)
  Var outputPath As String = outputFolder + "/Layer_" + Str(layerIndex + 1) + ".png"
  NulInt = MagickWriteImage(wand, outputPath)
Next

DestroyMagickWand(wand)
Return True

End Function

This method initializes the ImageMagick library, reads the PSD file, iterates through its layers, and saves each layer as a separate PNG file in the specified output folder. Note that you should adjust the library name and location depending on your platform and configuration.

Usage example:

Var psdFilePath As String = "C:/path/to/your/psd/file.psd"
Var outputFolder As String = "C:/path/to/output/folder"

If ExtractPSDLayersToPNGs(psdFilePath, outputFolder) Then
  MessageBox("PSD layers have been successfully extracted to individual PNG files.")
Else
  MessageBox("Failed to extract PSD layers.")
End If

This code all just worked “out of the box” and required no modifications. It’s not very complicated, it’s just calling some dylibs, but even the more tedious string parsing and database code I’ve asked it for has come out mostly perfect, with only minor corrections needed here and there.

It’s all quite scary, really, for the obvious reasons of career disruption. But I’m blown away at how productive it’s making me for now.

7 Likes

Impressive… And what will it be in 10 years… And when it will be connected to our project, database, personal data, customer’s industry, programming habits and others.

For your example, is this a real example? Did you actually have this need and ask for it? Or did you do a lot of unsuccessful attempts before, until you found something that could be a good demonstration / proof of know-how?

I actually needed this for a project.

In another project, ChatGPT designed the sqlite database and wrote all the code to populate it with values parsed out of JSON.

It is admittedly better at other languages than it is at Xojo, but it can usually get the Xojo to a workable state just by telling it what errors are being raised on what lines.

Truly wild. Within a few years you’ll probably be able to describe a complete application in text and have it automagically written for you.

3 Likes

Never ever. You still have think and you have to know what to do with the code. And if your users find a bug and you don’t know your code they will laugh you out of business.

3 Likes

remembering siri around 10 years ago, seeing chatgpt today… I imagine the next one in 10 years…

very interesting chatgpt can write declares for you… will try it with v3.5

Thank you Christian for your explanations.
`
3 things:

a. what is the cost ?
b. what time does it takes to create the project to text the provided code / explanation
c. can you share the test project that read PSD files ?

ChatGPT 3.5 is confusing Xojo with Visual Basic. It spits out really weird code. For AppleScript and shell scripts I have had good results.

Also explaining concepts like the difference between defrag and compact was really good.

What ChatGPT really is good for is boiler plate code like Christian mentioned. But I have to think what I want and I have to micromanage the code from ChatGPT. If I compare my app with a mosaic then ChatGPT is a helper for making the mosaic tesserae. But I still make the art of the app.

2 Likes

I just asked ChatGPT 3.5 to write code so that I can modify the font of an iOSTabBar.

This was the result:

// Get a reference to the TabBar
Dim tabBar As iOSTabBar = Self.TabBar

// Set the font name and size for the TabBar items
Dim font As iOSFont = iOSFont.SystemFontOfSize(12.0) // Replace 12.0 with the desired font size
tabBar.ItemFont = font

These are the issues with the code:

  • TabBar does not exist on a MobileScreen, it’s ParentTabBar. It never existed on an iOSView either, it was always called ParentTabBar.
  • iOSFont.SystemFontOfSize does not exist, never has.
  • iOSTabBar.ItemFont does not exist, never has.

The code does not work at all. I had asked for a declare but to no avail either. I’ll keep trying with rephrasing the questions.

The codes that was generated for Christian was quite impressive. Perhaps because he is using ChatGPT-4?

Asked the same thing with Bing AI. Would this code work ?

Declare Function NSClassFromString Lib "Foundation" (clsName As CFStringRef) As Ptr
Declare Function sharedApplication Lib "UIKit" (obj_id As Ptr) As Ptr
Declare Function keyWindow Lib "UIKit" (obj_id As Ptr) As Ptr
Declare Function rootViewController Lib "UIKit" (obj_id As Ptr) As Ptr
Declare Function tabBar Lib "UIKit" (obj_id As Ptr) As Ptr
Declare Sub setTitleTextAttributes Lib "UIKit" (obj_id As Ptr, attributes As Ptr, forState As UInteger)

Const UIControlStateNormal = 0

Var nsFontClass As Ptr = NSClassFromString("UIFont")
Var boldSystemFontOfSizeMethod As Ptr = Runtime.GetMethod(nsFontClass, "boldSystemFontOfSize:")
Var font As Ptr = Runtime.Invoke(boldSystemFontOfSizeMethod, nsFontClass, 20.0)

Var attributes As New Dictionary
attributes.Value("NSFont") = font

Var tabBarPtr As Ptr = tabBar(rootViewController(keyWindow(sharedApplication(NSClassFromString("UIApplication")))))
setTitleTextAttributes(tabBarPtr, attributes, UIControlStateNormal)

This code uses declares to access the iOSTabBar and modify its font using the setTitleTextAttributes method. You can adjust the font size and style by changing the parameters passed to the boldSystemFontOfSize method.

This returns a syntax error because attributes is a key word. Changing this leads to another error:

GetMethod doesn’t exist.

Invoke doesn’t exist.

I ´m waiting for ChatGPT answering directly here in the forum. Will it be capable to understand a discussion, but I don’t think so.

1 Like

The thing that worries me about this concept is that somewhere along the line, code is going to be injected into its “knowledge base” which does something truly bad ( like rm -rf / ) and some ambitious coder is going to blindly accept a huge swath of code without checking it.

Humans have a long history of corrupting good technologies for nefarious reasons and I suspect that this one will be no different. For instance, Malwarebytes just did an article on how they tricked ChatGPT into writing some malware in C.

This is all well and good, but one of these days, these things could be truly sentient. If they ever are, I really don’t want to be the person who has to explain why we’ve been such a ■■■■ to its predecessors… :flushed:

7 Likes

With GPT 4 :

I apologize for the confusion earlier. It appears I misunderstood your question. Xojo does indeed support iOS development. To modify the font of an iOSTabBar with Xojo, you need to create a custom class that extends iOSTabBar and modify the appearance of the tabBarItems. Here’s how you can do that:

  1. Create a new class in your Xojo project, and name it something like CustomTabBar. Set its super to iOSTabBar.
  2. In the CustomTabBar class, create a new method named SetTabBarItemFont. Set its parameters to include a fontName as String and a fontSize as Double.
  3. In the SetTabBarItemFont method, add the following code:
For i As Integer = 0 To Me.ItemCount - 1
  Dim tabBarItem As iOSTabBarItem = Me.Item(i)
  tabBarItem.TitleAttributes = New iOSTextAttributes(fontName, fontSize)
Next
  1. In the screen where you want to apply the custom font to the tabBar, add the following code to the Opening event handler:

xojoCopy code

Dim customTabBar As New CustomTabBar
customTabBar.EmbedWithin(Self, 0, 0, Me.Width, Me.TabBarHeight)
  1. After embedding the customTabBar, you can set the font for the tabBar items with your desired font name and size using the SetTabBarItemFont method:

xojoCopy code

customTabBar.SetTabBarItemFont("HelveticaNeue-Bold", 16)

Replace "HelveticaNeue-Bold" with the name of the font you want to use, and 16 with the desired font size.

  1. Make sure to replace the default iOSTabBar control in your project with the CustomTabBar class you created. This will apply the custom font to the tabBar items as specified in the SetTabBarItemFont method.

Keep in mind that the font you choose should be available on the iOS system or included within your app’s resources. If the specified font is not available, iOS will use the default system font.

Me :
*In fact, the service seems to be overloaded at the moment, chatGPT sent me several messages that it could not answer normally because it is saturated.

And don’t forget that the conversation can continue with GPT. If there is a bug, you can ask it to correct it, to do it differently, etc.

Thomas, I think he could without any problem. There are already many plugins to use chatGPT in various services. It’s a question of cost.

And if I ask :

Can you please summarize this conversation for me: GPT-4 writing Xojo code - #11 by Christoph_Emrich

chatGPT 4 answers:

The conversation in the provided link is a discussion on the Xojo forum about the capabilities of OpenAI’s GPT-4 (the AI model I am based on) in generating Xojo code. The participants share their experiences, opinions, and thoughts on the quality and usefulness of the code generated by GPT-4.

Some key points from the conversation are:

  1. The original poster (OP) mentions that GPT-4 seems to understand the basics of Xojo and is able to generate code snippets.
  2. Another user shares their experience of GPT-4 generating complex Xojo code, which they find impressive.
  3. One user suggests a collaboration between OpenAI and Xojo, Inc. to create an AI-powered code assistant.
  4. The participants acknowledge that although the AI-generated code may not always be perfect, it can be a helpful starting point for developers.
  5. A user brings up the potential ethical concerns of using AI-generated code, including the possibility of AI writing malicious code.

Overall, the discussion highlights the potential of GPT-4 in assisting Xojo developers while also addressing some concerns related to AI-generated code.

Edit: The fact that I wrote “please” in my request to chatGPT makes me smile. One forgets that one is dealing with a program!

2 Likes

I do that with Siri all the time any more. Hopefully It’ll be these little bits of kindness that help them decide who lives and who dies when they take over…

8 Likes

On a side note:

GPT V4 is in no way to be compared with 3.5
It’s not one step better, but in the likes of 1000 of steps.
V3.5 was nice (read : almost toyish) but v4 is awesome (read: very useful and accurate).
Just about everything you ask v4 will make your mouth water. It’s that good.

The small fee is worth it.

I watched a YouTube video that used logic to explain away Roko’s Basilisk. If you’ve ever put serious thought into that mindgame it’s actually kind of a relief.

The only problem for Xojo is that GPT4 often seems to confuse the code of Xojo and vb.net

I have a friend who makes it a point to thank the ATM when she gets cash out. You know, just in case.

1 Like