Using AI to help write code, or actually just write it for you.

All sorts of amusements and nonsense unrelated to xTalk
Post Reply
User avatar
OpenXTalkPaul
Posts: 1485
Joined: Sat Sep 11, 2021 4:19 pm
Contact:

Using AI to help write code, or actually just write it for you.

Post by OpenXTalkPaul »

Here's the transcript from a chat I just had with some AI:

Paul McClernan:
Write some RunRev transcript that asks for the users name

AI:
Here is a sample RunRev transcript that asks for the user's name:

Code: Select all

on mouseUp
  -- Ask for the user's name
  put "What is your name?" into message
  answer message
  if it is empty then exit mouseUp
  put it into userName
  -- Show a greeting message
  put "Hello, " & userName & "!" into greeting
  answer greeting
end mouseUp
This script displays a message asking the user for their name using the put and answer commands. It then stores the user's response in a variable called userName. Finally, it displays a greeting message using the put and answer commands, which includes the user's name.

You can customize the script by adding additional functionality or by modifying the message and greeting to suit your needs. For example, you can use the userName variable to personalize the greeting or to store the user's name for later use. You can find more information about RunRev and how to use it to create interactive applications in the RunRev documentation or through online tutorials and resources.



Paul McClernan:
change the word message to tMessage because it is a transcript keyword

AI:
Here is the revised RunRev transcript that asks for the user's name, with the word "message" replaced with "tMessage" to avoid using a transcript keyword:

Code: Select all

on mouseUp
  -- Ask for the user's name
  put "What is your name?" into tMessage
  answer tMessage
  if it is empty then exit mouseUp
  put it into userName
  -- Show a greeting message
  put "Hello, " & userName & "!" into greeting
  answer greeting
end mouseUp
This script works in the same way as the previous version, but it uses the variable "tMessage" instead of "message" to avoid using a transcript keyword. This ensures that the script will run correctly in RunRev.

You can customize the script by adding additional functionality or by modifying the tMessage and greeting to suit your needs. For example, you can use the userName variable to personalize the greeting or to store the user's name for later use. You can find more information about RunRev and how to use it to create interactive applications in the RunRev documentation or through online tutorials and resources.

Paul McClernan:
no 'answer' should be ask because user input is being requested

AI:
Here is the revised RunRev transcript that asks for the user's name, with the answer command replaced with ask to request user input:

Code: Select all

on mouseUp
  -- Ask for the user's name
  put "What is your name?" into tMessage
  ask tMessage
  if it is empty then exit mouseUp
  put it into userName
  -- Show a greeting message
  put "Hello, " & userName & "!" into greeting
  answer greeting
end mouseUp
This script works in the same way as the previous versions, but it uses the ask command instead of answer to request user input. The ask command displays a message and allows the user to enter a response, which is stored in the it variable.

You can customize the script by adding additional functionality or by modifying the tMessage and greeting to suit your needs. For example, you can use the userName variable to personalize the greeting or to store the user's name for later use. You can find more information about RunRev and how to use it to create interactive applications in the RunRev documentation or through online tutorials and resources.

------------------ SNIP --------------------

So in three quickly typed requests and revised responses I had code that did exactly what I wanted.
Granted this is a very simple bit of code that I'm asking the AI to write, but obviously this is instantly useful for people who don't really like to type. And I'm impressed that it was able to write code in xTalk dialects. I had started by telling it to write in HyperTalk and MetaTalk or xTalk script and it made wrote useable, perhaps not 100% correctly but close, syntax each time. It is simply amazing.
User avatar
OpenXTalkPaul
Posts: 1485
Joined: Sat Sep 11, 2021 4:19 pm
Contact:

Re: Using AI to help write code, or actually just write it for you.

Post by OpenXTalkPaul »

MORE...

Paul McClernan:
Now translate that code into JavaScript code

AI:
Here is the revised JavaScript code that asks for the user's name, based on the RunRev transcript provided:

Code: Select all

function askName() {
  // Ask for the user's name
  var tMessage = "What is your name?";
  var userName = prompt(tMessage);
  if (!userName) {
    return;
  }
  // Show a greeting message
  var greeting = "Hello, " + userName + "!";
  alert(greeting);
}
This code defines a function called askName that asks for the user's name using the prompt function and stores the response in a variable called userName. It then displays a greeting message using the alert function, which includes the user's name.

You can call the askName function in your JavaScript code by calling askName(). You can customize the function by adding additional functionality or by modifying the tMessage and greeting to suit your needs. For example, you can use the userName variable to personalize the greeting or to store the user's name for later use. You can find more information about JavaScript and how to use it to create interactive applications in online tutorials and resources.


-------------------------------
So it can even transpile from xTalk to JavaScript, no problem.
User avatar
OpenXTalkPaul
Posts: 1485
Joined: Sat Sep 11, 2021 4:19 pm
Contact:

Re: Using AI to help write code, or actually just write it for you.

Post by OpenXTalkPaul »

It didn't know the difference between an xTalk script and Extension Builder language variant, so I started to teach it the differences. Here's the results after a few lines of teaching it some of the differences.

Code: Select all

handler askName() returns string
  -- Declare variables
  variable tMessage as string
  variable userName as string
  variable greeting as string
  
  -- Ask for the user's name
  put "What is your name?" into tMessage
  put tMessage into userName
  if userName is empty then exit askName
  
  -- Log the user's name
  log userName
  
  -- Show a greeting message
  put "Hello, " & userName & "!" into greeting
  log greeting
  return greeting
end handler
There's no Ask & Answer in Extension Builder so I just told it to just log the variables 'greeting' and 'userName', but that Extension Builder handler looks correct.
User avatar
richmond62
Posts: 2617
Joined: Sun Sep 12, 2021 11:03 am
Location: Bulgaria
Contact:

Re: Using AI to help write code, or actually just write it for you.

Post by richmond62 »

Um . . .

Any chance you post a link to that AI so others can 'have a go'?

Oh, I did notice the reference to RunRev rather than LiveCode . . . I wonder if that AI can cope with stuff introduced after the name change.
https://richmondmathewson.owlstown.net/
User avatar
OpenXTalkPaul
Posts: 1485
Joined: Sat Sep 11, 2021 4:19 pm
Contact:

Re: Using AI to help write code, or actually just write it for you.

Post by OpenXTalkPaul »

richmond62 wrote: Tue Dec 20, 2022 7:45 am Um . . .

Any chance you post a link to that AI so others can 'have a go'?

Oh, I did notice the reference to RunRev rather than LiveCode . . . I wonder if that AI can cope with stuff introduced after the name change.
Oh right here ya go:
https://chat.openai.com/

I've basically taught it to write Extension Builder language in just a few hours of teaching it the language, including writing foreign language binding strings for Objective C stuff. Still needs to learn more but It's pretty quickly become like having an intern or junior developer working for me doing a lot of the boiler-plate tedious work for me.
User avatar
OpenXTalkPaul
Posts: 1485
Joined: Sat Sep 11, 2021 4:19 pm
Contact:

Re: Using AI to help write code, or actually just write it for you.

Post by OpenXTalkPaul »

Here' some sample Extension Module that it just spit out for me, probably needs some actual human work on it some for it to work as expected, but this looks like a really good start, maybe 80% of the way to audio recording to a file:

Code: Select all

library org.openxtalk.library.avaudiorecorder

metadata version is "1.0"
metadata author is "OpenXTalk.org"
metadata title is "OpenXTalk.org AVAudioRecorder"
metadata userVisible is "true"

use com.livecode.foreign
use com.livecode.objc
use com.livecode.string

private type AVAudioRecorder as ObjcId

private foreign handler objC_AVAudioRecorderAlloc() returns AVAudioRecorder binds to "objc:AVAudioRecorder.+alloc"
private foreign handler objC_AVAudioRecorderInitWithURL(in pRecorder as AVAudioRecorder, in pURL as ObjcId, in pFormat as ObjcId, in pError as ObjcId) returns AVAudioRecorder binds to "objc:AVAudioRecorder.+initWithURL:format:error:"
private foreign handler objC_AVAudioRecorderPrepareToRecord(in pRecorder as AVAudioRecorder) returns Boolean binds to "objc:AVAudioRecorder.+prepareToRecord"
private foreign handler objC_AVAudioRecorderRecord(in pRecorder as AVAudioRecorder) returns Boolean binds to "objc:AVAudioRecorder.+record"
private foreign handler objC_AVAudioRecorderStop(in pRecorder as AVAudioRecorder) returns nothing binds to "objc:AVAudioRecorder.+stop"

public handler StartRecording(in pFilename as String, in pFormat as String) returns Boolean
   variable tError as ObjcId
   variable tURL as ObjcId
   variable tRecorder as AVAudioRecorder
   variable tFormat as ObjcId
   put objC_NSURLFileURLWithPath(pFilename) into tURL
   put objC_AVAudioFormatAudioFormatWithSettings(pFormat) into tFormat
   put objC_AVAudioRecorderAlloc() into tRecorder
   put objC_AVAudioRecorderInitWithURL(tRecorder,tURL,tFormat,tError) into tRecorder
   return objC_AVAudioRecorderPrepareToRecord(tRecorder)
end handler

public handler StopRecording(in pRecorder as AVAudioRecorder) returns nothing
   objC_AVAudioRecorderStop(pRecorder)
end handler

public handler Record(in pRecorder as AVAudioRecorder) returns Boolean
   return objC_AVAudioRecorderRecord(pRecorder)
end handler

end library
User avatar
OpenXTalkPaul
Posts: 1485
Joined: Sat Sep 11, 2021 4:19 pm
Contact:

Re: Using AI to help write code, or actually just write it for you.

Post by OpenXTalkPaul »

Here's a simple sound player Extension for macOS that uses NSSound (old sound player API), working code (after a few very minor corrections) almost entirely written by an AI that just started to learn Extension Builder language last night:

Code: Select all

library org.openxtalk.library.macosaudio

use com.livecode.foreign
use com.livecode.objc

metadata version is "0.0.1"
metadata author is "OpenXTalk.org"
metadata title is "MacOS Audio Tools"
-- metadata svgicon is "M66.43,52.07c-0.71" -- this is an SVG Path string to use a the modules icon

private foreign handler objC_NSSoundAlloc() returns ObjcRetainedId binds to "objc:NSSound.+alloc"
private foreign handler objC_NSSoundInitWithContentsOfFile(in pNSSound as ObjcId, in pFilename as ObjcId, in pByRef as CBool) returns ObjcRetainedId binds to "objc:NSSound.-initWithContentsOfFile:byReference:"
private foreign handler objC_NSSoundPlay(in pNSSound as ObjcId) returns CBool binds to "objc:NSSound.-play"
private foreign handler objC_NSSoundStop(in pNSSound as ObjcId) returns nothing binds to "objc:NSSound.-stop"
private foreign handler objC_NSSoundIsPlaying(in pNSSound as ObjcId) returns CBool binds to "objc:NSSound.-isPlaying"
private foreign handler objC_NSSoundGetVolume(in pNSSound as ObjcId) returns CDouble binds to "objc:NSSound.-volume"
private foreign handler objC_NSSoundSetVolume(in pNSSound as ObjcId, in pVolume as CDouble) returns nothing binds to "objc:NSSound.-setVolume:"

variable mSound as ObjcObject

public handler NSSoundPlay(in pPosixFilepath as String) returns optional any
      -- variable mSound as ObjcObject
      variable tURL as ObjcId
      variable tBool as Boolean
      variable tDouble as Real

      unsafe
      if pPosixFilepath is not nothing and pPosixFilepath is not the empty string then
         log "String was passed so we assume that it's a POSIX filepath"
      else
         put "/System/Library/Sounds/Sosumi.aiff" into pPosixFilepath
      end if
       put objC_NSSoundAlloc() into mSound
       put objC_NSSoundInitWithContentsOfFile(mSound,StringToNSString(pPosixFilepath), false) into mSound
       put objC_NSSoundPlay(mSound) into tBool
       log tBool
     end unsafe
end handler

end library
Once the module is loaded you can call public handler NSSoundPlay from your xTalk scripts like so:

Code: Select all

NSSoundPlay "/System/Library/Sounds/Blow.aiff"
NSSoundPlay tPathToSomeSoundFile

It even made binding strings for some of the other related foreign functions as well, and I didn't even ask it to!

FYI
variable mSound as ObjcObject needed to be a module scoped variable otherwise the sound stops playing as soon as the handler ends and the system auto-removes the player object from memory (wether its done playing the sound or not). So now that it is module scoped it needs a handler to stop the playback (or just continues until it finishes playing the sound). I may finish wrapping this tiny API, but this was mostly just a test. Next thing on that track would be to wrap AVAudioRecorder, which is far more useful IMO.
Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests