Apple Script for converting GRBL code for use with Mach 3

After a bit of research I have a nice apple script that will convert GRBL laser code generated in Lightburn to code that is useable in Mach 3. It is required that you have M11Px Sx and M10 set up and working in Mach 3. The script was written by Chat GPT as I am not much of a programmer.

You must also manually add the GRBL profile to your LightBurn set up and define your machine in the LightBurn settings.

GRBL uses G0 moves to turn the laser on and off.

This script adds the M10 Command before every G0 move and the M11Px code after every M0 move. Right now I am working on getting the script to read the laser power command from the comment line and write it after the M11Px command. I have not yet been successful in this.

It may also be possible that the Sxx after the M11Px command might not be needed but I have to test this.

Since I am not able to attach the script file I have pasted the entire script below. Feel free to copy and past this into the Apple Script editor. Or if you are a programmer and would like to convert it to a windows format please feel free to do so.

– Choose the file to be modified using the Finder

set chosenFile to choose file with prompt “Choose a file to modify:”

set fileReference to open for access chosenFile with write permission

– Initialize variables

set modifiedLines to {}

– Define the text to be inserted

set insertLineAbove to “M10P2”

set insertLineBelow to “M11P2 S”

– Read and process the file line by line

try

– Read the file line by line

set eofReached to false

repeat until eofReached

try

– Read the current line

set currentLine to read fileReference until linefeed

– Check if the line contains “G0”

if currentLine contains “G0” then

– Add M10P2 one line above the G0 command

set end of modifiedLines to insertLineAbove

set end of modifiedLines to currentLine

– Add M11P2 S25 one line below the G0 command

set end of modifiedLines to insertLineBelow

else

– Keep the line unchanged

set end of modifiedLines to currentLine

end if

on error

– End of file reached

set eofReached to true

end try

end repeat

– Close the file for reading

close access fileReference

– Add M30 at the end of the modified lines

set end of modifiedLines to “M30”

– Combine the modified lines into a single string with line breaks

set modifiedText to (modifiedLines as text)

– Write the modified text back to the file

set fileReference to open for access chosenFile with write permission

set eof of fileReference to 0

write modifiedText to fileReference

close access fileReference

display dialog “File successfully modified and saved.” buttons {“OK”} default button “OK”

on error errMsg

– Handle any errors that occur

try

close access fileReference

end try

display dialog "Error: " & errMsg buttons {“OK”} default button “OK”

end try

Let me know your thoughts. If you have any ideas on how to get the script to write the Sxx command from the layer comment line please let me know. I have been trying to get Chat GPT to do this but I’ve not yet been successful.