Process a .gcode/txt file to extract data into a .csv file with Applescript

This will finally be changed in macOS Tahoe! Can you believe that.

The alert text and the icon are now left-aligned again.

Well I wish they made that dialog wider, but at least Apple restored the most important part. The shameful disgusting centered alignment is no more.

The sad part is that the original code was still in there, but they didn’t put in an option switch or something. I wrote a NSAlert script that can add a variety of accessory views, and while testing if I did something weird like adding a text field with a certain range of characters and five buttons or so, it would revert to the old style. Nothing stable enough to use, but it was there.

You can make the dialog wider by adding an accessory view, but the buttons are still a mess.

The following uses your original gcode sample and my answer in reply #11:

• The script already names the csv file the same as the gcode file, although it adds a “[fields]” suffix. That and a file URL can be added before writing to the csv file by using something like:

   set newf to csvFolder & theName & " [fields].csv" -- just save to pre-set folder
   set fileURL to ((current application's NSURL's fileURLWithPath:(POSIX path of (imageFolder & theName & ".png")))'s URLByStandardizingPath's absoluteString) as text
   set found to {"project_name," & (theName & extension), "image_URL," & fileURL} & found

• By getting the words of an entry like “enable_support = 0” or “nozzle_diameter=0.4”, if the the last word is a number then that word will be the complete number, including the decimal.
• Renaming certain settings can be done in the repeat statement where it is looking for matches to the extractedSettings, for example a property can be set up with a list of lists for the old and new settings:

property settingRenames : {¬
   {"adaptive_pressure_advance_model", "APAM"}, ¬
   {"machine_start_gcode", "MSG"}} -- {oldName, newName}

Add a handler for the rename:

to renameSetting(setting) -- rename select settings
   repeat with anItem in settingRenames
     set {old, new} to anItem
     if setting is old then return new
   end repeat
   return missing value
end renameSetting

And call it where the settings are extracted:

   tell (text items of setting) to if second item is in extractedSettings then -- check for setting
      set candidate to my renameSetting(second item) -- check for rename
      if candidate is not missing value then set second item to candidate
      set AppleScript's text item delimiters to "," -- csv separator
      set end of found to (rest of it as text) -- add to found list
   end if

You still have a few hundred lines to go to pass my Menubar Timer, but try not to get too carried away.

Thank you @red_menace ! Wonderfull, I’ll try these now!

I tried to refactor the code by adding your example to rename certain results, but I can’t.

Does this only work for the setting name, or also for the setting value? Because it’s the setting value that I want to rename.

For example, “print_compatible_printers = Flashforge Adventurer 5M”; I would like to rename “Flashforge Adventurer 5M” to “AD5M.”

Ah, I thought you were talking about the key, instead of the value. In that case, the settingRenames property can be used for the values that you want to change, for example:

property settingRenames : {¬
   {"Flashforge Adventurer 5M", "AD5M"}}

From there, the part where the settings are extracted would be changed to rename the third item:

   tell (text items of setting) to if second item is in extractedSettings then -- check for value
      set candidate to my renameSetting(third item) -- check for rename
      if candidate is not missing value then set third item to candidate
      set AppleScript's text item delimiters to "," -- csv separator
      set end of found to (rest of it as text) -- add to found list
   end if

Your example gcode file has the value as “Flashforge Adventurer 5M 0.4 Nozzle”, so the rename handler was changed to:

to renameSetting(setting) -- rename select settings
	repeat with anItem in settingRenames
		set {old, new} to anItem
		if old is in setting then
			-- return new -- this statement replaces the entire value
			set {tempTID, text item delimiters} to {text item delimiters, old} -- these statements
			set {itemList, text item delimiters} to {text items of setting, new} -- only replace the
			set {setting, AppleScript's text item delimiters} to {itemList as text, tempTID} -- matching portion
			return setting
		end if
	end repeat
	return missing value
end renameSetting

This will rename only the matching part of the value (and every occurrence, so watch out for duplicates), but if you want to replace the entire value with the new one, uncomment the return new statement. The following statements up to and including return setting will be skipped, but can be removed if you want.

1 Like

Would you mind sharing a link to your Github?

@jimmyhartington This project is not yet published, it will be published when I have finished the project and it is viable with all the information on “how to use it”…

1 Like

Thanks a lot! Work fine now, it’s perfect!

Now I just have to solve the problem of the boolean values ​​that I have to transform into “Yes” & “No”, and then the project will be completely finished on the code side.

If a string or number evaluates to a 0 or 1, you can do something like:

set bool to "1" -- true or false also works
set this to item ((bool as integer) + 1) of {"no", "yes"}
1 Like

Thank you,

I guess it should be placed in this block : :confused:

	# extract settings in config block
	set found to {}
	repeat with setting in paragraphs 2 thru -2 of (findSubstring of input from "; CONFIG_BLOCK_START" to "; CONFIG_BLOCK_END")
		set text item delimiters to {"; ", " = "} -- bits to exclude
		tell (text items of setting) to if second item is in extractedSettings then -- check for settings
			set candidate to my renameSetting(third item) -- check for rename
			if candidate is not missing value then set third item to candidate
			set AppleScript's text item delimiters to "," -- csv separator
			set end of found to (rest of it as text) -- add if setting is in extracted list
		end if
	end repeat

Yes, it would be when checking for an extracted setting value to rename. If a value wasn’t renamed (no match in the settingRenames list), then check for a binary value, so something like:

   tell (text items of setting) to if second item is in extractedSettings then -- check for extracted setting
      set candidate to my renameSetting(third item) -- check for value to rename
      if candidate is not missing value then -- text value
         set third item to candidate
      else -- binary value
         try -- skip if it doesn't evaluate to 0 or 1
            set third item to item ((third item as integer) + 1) of {"no", "yes"}
         end try
      end if
      set AppleScript's text item delimiters to "," -- csv separator
      set end of found to (rest of it as text) -- add to found list
   end if
1 Like

It works perfectly but as I suspected, when a value contains a “0” like for example “nozzle_diameter = 0.4”, it turns into “nozzle_diameter = no”.

OK, this specifically looks for a value of “0” or “1”:

   if candidate is not missing value then -- text value
      set third item to candidate
   else -- binary value (only "0" or "1")
      if third item is in {"0", "1"} then set third item to item ((third item as integer) + 1) of {"no", "yes"}
   end if
1 Like

Thanks a lot! Work like a charm!

Now, I’m concentrate to the Illustrator Template. By the way, on this subject, do you think I can automate the task with Illustrator using AppleScript?

There are a few topics with the Illustrator tag, but I’ve never used it.

Ok, thank you! I’ll do my research on the subject. :wink:

Thank you so much for all your time and help! Remember, if you need any graphic works (avatar, ads, icons, etc.), I’m here for you!