I decided to take the plunge and see what this was all about. I posed a 
moderately difficult LC question to ChatGPT, one I knew that beginning 
livecoders (or even myself) might find challenging. Result: some things 
surprised me, both good and bad, but overall I think the approach has 
potential… with lots of cautions and caveats. Since I may overrun any character 
limits imposed on these posts, I’ll add additional comments at the end. 

ME: write a function in livecode to return a files date of creation

Here is a function in LiveCode that will return the date of creation of a file:

on getFileCreationDate pFilePath
  put the creationdate of file pFilePath into tCreationDate
  return tCreationDate
end getFileCreationDate

You can call the function by passing in the file path as a parameter, like this:

put getFileCreationDate("/path/to/file.txt")

This will return the creation date of the file in the format "dd/mm/yyyy 
hh:mm:ss".
Please note that this function only works on Mac and Windows.

ME: Livecode does not recognise the keyword "creationdate". Can you try using 
the "detailed" option of the "files" function to find the creation date?

Yes, you're correct that the "creationdate" property is not available in 
LiveCode. However, you can use the "files" function with the "detailed" option 
to get information about a file, including the date of creation. Here's an 
example function that uses the "files" function to return the date of creation 
of a file:

on getFileCreationDate pFilePath
   put the files(pFilePath, "detailed") into tFileInfo
   put item 5 of tFileInfo into tCreationDate
   return tCreationDate
end getFileCreationDate

You can call the function by passing in the file path as a parameter, like this:

put getFileCreationDate("/path/to/file.txt")
This will return the creation date of the file in the format "yyyy-mm-dd 
hh:mm:ss".
Please note that this will work on all platforms.

ME: the files function in livecode returns a list of all of the files in a 
directory. Can you select the one you are looking for, which is file.txt?

Yes, you're correct that the "files" function in LiveCode returns a list of all 
files in a directory, so you need to filter the list to get the file you're 
looking for. Here's an example function that uses the "filter" function to get 
the information of the file you are looking for:

on getFileCreationDate pFilePath
   put the files(pFilePath, "detailed") into tFilesList
   put filter(tFilesList, "name", "file.txt") into tFileInfo
   put item 5 of tFileInfo into tCreationDate
   return tCreationDate
end getFileCreationDate

You can call the function by passing in the directory path as a parameter, like 
this:

put getFileCreationDate("/path/to/directory")

This will return the creation date of the file "file.txt" in the format 
"yyyy-mm-dd hh:mm:ss" if it exists in the directory. If the file doesn't exist 
in the directory it will return an empty string.

ME: livecode uses the "function" keyword to define a function. Could you 
rewrite this using the function keyword?

Sure, here's an example function that uses the "filter" function to get the 
information of the file you are looking for and defined with the "function" 
keyword:

function getFileCreationDate pFilePath
   local tFilesList, tFileInfo, tCreationDate
   put the files(pFilePath, "detailed") into tFilesList
   put filter(tFilesList, "name", "file.txt") into tFileInfo
   put item 5 of tFileInfo into tCreationDate
   return tCreationDate
end getFileCreationDate

You can call the function by passing in the directory path as a parameter, like 
this:

put getFileCreationDate("/path/to/directory")

This will return the creation date of the file "file.txt" in the format 
"yyyy-mm-dd hh:mm:ss" if it exists in the directory. If the file doesn't exist 
in the directory it will return an empty string.

ME: In livecode the filter command uses the form: filter lines of <source> 
matching wildcard <wildcard> into <container>. Could you rewrite your filter 
command appropriately?

Yes, you're correct that in LiveCode the filter command uses the form "filter 
lines of <source> matching wildcard <wildcard> into <container>". Here's an 
example function that uses the filter command with this form to get the 
information of the file you are looking for:

function getFileCreationDate pFilePath
   local tFileInfo, tCreationDate
   filter lines of (the files(pFilePath, "detailed")) matching wildcard 
"file.txt" into tFileInfo
   put item 5 of tFileInfo into tCreationDate
   return tCreationDate
end getFileCreationDate

You can call the function by passing in the directory path as a parameter, like 
this:

put getFileCreationDate("/path/to/directory")

This will return the creation date of the file "file.txt" in the format 
"yyyy-mm-dd hh:mm:ss" if it exists in the directory. If the file doesn't exist 
in the directory it will return an empty string.

ME: ok, but the date returned is in seconds since midnight Jan 1, 1970. Could 
you convert it to MM/DD/YYYY?

Yes, you're correct that the date returned is in seconds since midnight Jan 1, 
1970. You can use the "date" function to convert it to the desired format 
"MM/DD/YYYY". Here's an example function that converts the date returned by the 
"files" function to the format "MM/DD/YYYY":

function getFileCreationDate pFilePath
   local tFileInfo, tCreationDate, tFormattedDate
   filter lines of (the files(pFilePath, "detailed"))  matching wildcard 
"file.txt" into tFileInfo
   put item 5 of tFileInfo into tCreationDate
   put date(tCreationDate, "MM/DD/YYYY") into tFormattedDate
   return tFormattedDate
end getFileCreationDate

You can call the function by passing in the directory path as a parameter, like 
this:

put getFileCreationDate("/path/to/directory")

This will return the creation date of the file "file.txt" in the format 
"MM/DD/YYYY" if it exists in the directory. If the file doesn't exist in the 
directory it will return an empty string.

ME: Livecode uses the command "convert" to convert between date formats. Could 
you try using the "convert" command?

Yes, you're correct that in LiveCode you can use the "convert" command to 
convert between date formats. Here's an example function that uses the 
"convert" command to convert the date returned by the "files" function to the 
format "MM/DD/YYYY":

function getFileCreationDate pFilePath
   local tFileInfo, tCreationDate, tFormattedDate
   filter lines of (the files(pFilePath, "detailed"))  matching wildcard 
"file.txt" into tFileInfo
   put item 5 of tFileInfo into tCreationDate
   put convert(tCreationDate, "seconds", "MM/DD/YYYY") into tFormattedDate
   return tFormattedDate
end getFileCreationDate

You can call the function by passing in the directory path as a parameter, like 
this:

put getFileCreationDate("/path/to/directory")

This will return the creation date of the file "file.txt" in the format 
"MM/DD/YYYY" if it exists in the directory. If the file doesn't exist in the 
directory it will return an empty string.

ME: Livecode uses a more English like syntax. The convert command takes the 
form: convert <date> from <format> to <format> and does not require an output 
variable. Could you try converting using that form?

Yes, you're correct that in LiveCode the "convert" command takes the form 
"convert <date> from <format> to <format>", and that it does not require an 
output variable. Here's an example function that uses the "convert" command in 
this form, to convert the date returned by the "files" function to the format 
"MM/DD/YYYY":

function getFileCreationDate pFilePath
   local tFileInfo, tCreationDate
   filter lines of (the files(pFilePath, "detailed"))  matching wildcard 
"file.txt" into tFileInfo
   put item 5 of tFileInfo into tCreationDate
   return convert tCreationDate from "seconds" to "MM/DD/YYYY"
end getFileCreationDate

You can call the function by passing in the directory path as a parameter, like 
this:

put getFileCreationDate("/path/to/directory")
This will return the creation date of the file "file.txt" in the format 
"MM/DD/YYYY" if it exists in the directory. If the file doesn't exist in the 
directory it will return an empty string.

ME: The return command in livecode only allows values, not expressions. Could 
you make the conversion in tCreationDate first, then return the value?

Yes, you're correct that in LiveCode the "return" command only allows values, 
not expressions. Here's an example function that converts the date returned by 
the "files" function to the format "MM/DD/YYYY" before returning it:

function getFileCreationDate pFilePath
   local tFileInfo, tCreationDate
   filter lines of (the files(pFilePath, "detailed"))  matching wildcard 
"file.txt" into tFileInfo
   put item 5 of tFileInfo into tCreationDate
   put convert tCreationDate from "seconds" to "MM/DD/YYYY" into tCreationDate
   return tCreationDate
end getFileCreationDate

You can call the function by passing in the directory path as a parameter, like 
this:

put getFileCreationDate("/path/to/directory")

This will return the creation date of the file "file.txt" in the format 
"MM/DD/YYYY" if it exists in the directory. If the file doesn't exist in the 
directory it will return an empty string.

ME: Almost, you can just say "convert tCreationDate from seconds to short 
date". You do not need to put it into anything since it converts "in place". 
Otherwise correct. Do you want to make that change?

Yes, you're correct that in LiveCode, you can use the convert command in the 
format "Convert tCreationDate from seconds to short date" which will convert 
the value in place. I apologize for the confusion, here's the corrected version 
of the function

function getFileCreationDate pFilePath
   local tFileInfo, tCreationDate
   filter lines of (the files(pFilePath, "detailed"))  matching wildcard 
"file.txt" into tFileInfo
   put item 5 of tFileInfo into tCreationDate
   convert tCreationDate from "seconds" to "short date"
   return tCreationDate
end getFileCreationDate

You can call the function by passing in the directory path as a parameter, like 
this:

put getFileCreationDate("/path/to/directory")

This will return the creation date of the file "file.txt" in the format 
"MM/DD/YYYY" if it exists in the directory. If the file doesn't exist in the 
directory it will return an empty string.

ME: Wonderful. One final point. In livecode wildcards must contain wildcard 
characters. Could you rewrite the filter command to include at least 1 wildcard 
character in the matching string?

Yes, you're correct that in LiveCode wildcards must contain wildcard characters 
such as "*" or "?" to match any number of characters or any single character 
respectively. Here's an example function that uses the filter command with a 
wildcard character in the matching string:

function getFileCreationDate pFilePath
   local tFileInfo, tCreationDate
   filter lines of (the files(pFilePath, "detailed"))  matching wildcard 
"file.txt*" into tFileInfo
   put item 5 of tFileInfo into tCreationDate
   convert tCreationDate from "seconds" to "short date"
   return tCreationDate
end getFileCreationDate

You can call the function by passing in the directory path as a parameter, like 
this:

put getFileCreationDate("/path/to/directory")

This will return the creation date of any file that starts with "file.txt" in 
the directory in the format "MM/DD/YYYY" if it exists in the directory. If the 
file doesn't exist in the directory it will return an empty string.

ME: Wonderful. Thank you. I hope this was useful for you too.

You're welcome! I'm glad that I could help. I am a language model, so I don't 
have personal preferences or emotions, but it's my purpose to provide helpful 
and accurate information. If you have any other questions, feel free to ask.


ME: Cheers
Don't hesitate to reach out if you need anything else. Have a great day!

Things that surprised and sometimes delighted me.

First, I was unimpressed with its first attempt. I don't think beginners would 
learn anything from this. 1. It was not structured like a proper LC function 
(for example, no "function" keyword) 2. It just "made up" stuff when it did not 
know what to do (like creating the keyword "creationdate"). Mind you, it would 
be absolute heaven if retrieving a file date in LC were that simple :) 

But, when I suggested using the "detailed" option of the files command I was 
pleasantly surprised that it structured the code in the appropriate way and 
even selected item 5 as the file creation date. Pretty deep.

But it was still passing a parameter to the function that was a path to the 
file, not a path to the directory. When I pointed out that files() returns a 
list of all files in the directory it finally "caught on", and changed the 
parameter to a directory path and embedded the filename parameter in the 
function. I actually did not tell it to do that so that seemed rather clever to 
me. But better would have been to pass 2 parameters: 1 for the directory and 
another for the specific filename. And there were still a lot of syntactic 
issues that it struggled with: such as the proper format of a "filter" command, 
how to "convert" dates from one format to another, how to return a value in a 
function, and even using the "function" keyword itself. 

By the end of it we had a working function (although I still had to remove the 
quotes around "seconds" and "short date" in the convert command). But it took a 
lot of work to get there. 

Like all of you I will be keeping an eye on this technology to see how it 
improves and where best it can fit into our own work. I plan to retry this 
example in a week or two to see of ChatGPT has learned anything in the process. 

Mark


_______________________________________________
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode

Reply via email to