请求完善AppleScript实现Gcode文件转CSV数据提取脚本
方案选择与AppleScript脚本完善
工具选择建议
AppleScript完全能搞定你的需求——它在macOS上处理本地文件、系统交互(比如文件选择)天生顺手,不需要额外装工具,对初学者友好。如果以后要做更复杂的文本处理,也可以结合sed/awk这类命令行工具,但当前需求用纯AppleScript足够。
完整AppleScript脚本(包含步骤4-6)
-- 步骤1:选择待处理的.gcode文件 set targetFile to choose file with prompt "选择要处理的.gcode文件" of type {"gcode"} set targetPath to POSIX path of targetFile set fileName to name of (info for targetFile) set backupPath to text 1 thru -6 of targetPath & "_backup.gcode" -- 生成备份文件名 -- 步骤2:创建文件备份(保留你已完成的逻辑) do shell script "cp " & quoted form of targetPath & " " & quoted form of backupPath -- 步骤3:过滤无分隔符的运动指令行(保留你已完成的逻辑,可按需调整) set fileContent to read targetFile as «class utf8» set allLines to paragraphs of fileContent set filteredLines to {} repeat with aLine in allLines -- 过滤掉以G0/G1开头且没有;注释的运动指令行,可根据实际指令调整 if ((aLine starts with "G0" or aLine starts with "G1") and not (aLine contains ";")) then continue else set end of filteredLines to aLine end if end repeat set filteredContent to join filteredLines using linefeed -- 步骤4:初始化CSV数据结构 set csvData to {} set end of csvData to "类型,键,值" -- CSV表头 -- 步骤5:提取缩略图内容 set thumbnailStart to ";; thumbnail begin" set thumbnailEnd to ";; thumbnail end" set inThumbnail to false repeat with aLine in filteredLines if aLine starts with thumbnailStart then set inThumbnail to true -- 提取缩略图元数据(比如尺寸参数) set thumbnailMeta to text (length of thumbnailStart + 2) thru end of aLine set end of csvData to "缩略图,元数据," & quoted form of thumbnailMeta else if aLine starts with thumbnailEnd then set inThumbnail to false else if inThumbnail then -- 逐行记录缩略图的base64内容,也可合并为单个值,按需调整 set end of csvData to "缩略图,内容片段," & quoted form of aLine end if end repeat -- 步骤6:提取指定打印设置(示例列出几项,自行补充剩余16+项) set targetSettings to {"sparse_infill_density", "layer_height", "wall_thickness", "print_speed", "nozzle_temperature", "bed_temperature"} repeat with aLine in filteredLines if aLine starts with ";" then -- 去除开头的;和首尾空格 set cleanLine to trimWhitespace(text 2 thru end of aLine) -- 分割键值对(适配gcode中key = value的格式) if cleanLine contains "=" then set keyValuePair to splitString(cleanLine, "=") set settingKey to trimWhitespace(item 1 of keyValuePair) set settingValue to trimWhitespace(item 2 of keyValuePair) -- 匹配目标设置则写入CSV if settingKey is in targetSettings then set end of csvData to "打印设置," & quoted form of settingKey & "," & quoted form of settingValue end if end if end if end repeat -- 生成CSV文件并写入内容 set csvPath to text 1 thru -6 of targetPath & "_settings.csv" set csvContent to join csvData using linefeed set csvFile to open for access file csvPath with write permission write csvContent to csvFile as «class utf8» close access csvFile -- 处理完成提示 display dialog "处理完成!已生成CSV文件:" & csvPath buttons {"确定"} default button 1 -- 辅助函数:去除字符串首尾空格 on trimWhitespace(inputStr) set AppleScript's text item delimiters to {" "} set trimmed to text items of inputStr set AppleScript's text item delimiters to "" return trimmed as string end trimWhitespace -- 辅助函数:按分隔符分割字符串 on splitString(inputStr, delimiter) set AppleScript's text item delimiters to delimiter set splitItems to text items of inputStr set AppleScript's text item delimiters to "" return splitItems end splitString -- 辅助函数:合并列表为字符串 on join(listItems, delimiter) set AppleScript's text item delimiters to delimiter set joinedStr to listItems as string set AppleScript's text item delimiters to "" return joinedStr end join
使用注意事项
- 把
targetSettings列表补充完整,确保设置名称和你的gcode文件中的键名完全一致 - 如果运动指令的判断逻辑不符合你的gcode格式,修改步骤3中
if ((aLine starts with "G0" or aLine starts with "G1") and not (aLine contains ";"))的条件 - 运行脚本后,原gcode文件同目录会生成
_backup.gcode备份文件和_settings.csv结果文件
内容的提问来源于stack exchange,提问作者GilbertOOl
相关产品推荐
相关产品推荐

