Can G-code scripts be run automatically on inserting an SD card when using Marlin Firmware? When running Marlin Firmware, is it possible to run G-code scripts/series of commands automatically when you insert the SD card?



I'm running Marlin on a 3D printer board using an ATmega 2560 based board with a reprap discount LCD controller with an SD card slot. I would like to do this without the need to add another computer/board, so native from the board running the Marlin Firmware.
I am not entirely sure if this is what you are after, or if it will work, but from  this post on Printing From SD Card when printer is turned On


  While an LCD controller is not needed for auto#.g to work, if you have an LCD controller you can use the MENU_ADDAUTOSTART option to add a menu command that will run the auto0.g, auto1.g, auto2.g whenever you want.


This (final) post ended up closing the thread so it appears to answer the OP's question - however, whether it will answer your question is another matter.



This post, on the Marlin forum, replicates your question and asks about purely insertion of an SD card on a printer which is already powered on. 

A reply was received and apparently it should be possible by making a code change in ultralcd.ccp:


  It does not seem like an auto print, when inserting a SD-card, is to
  be found in the current program code of Marlin.
  
  You might consider making a feature request here: [github.com]
  
  I do not think that it would require much work to implement a
  "card.autoprintfile()" function in Marlin. It seems to me that a call
  to such a function could happen at line 5172 right after:

else
LCD_MESSAGEPGM(MSG_SD_INSERTED);

  
  in the file ultralcd.ccp


A subsequent feature request has been made on Marlin's GitHub, see [FR] Can G-code scripts be run automatically on inserting an SD card when using Marlin Firmware?

However, from this reply, it appears that a feature request is already pending:


  I'm not in favor of this as a general feature. We already have a
  feature request to be able to run custom G-code in response to various
  events, with SD card insertion being one of those events. Would that
  feature serve your needs?


However, as yet, I do not have a link to this other feature request. Will update as and when.



As an aside, in might be worth mentioning that the SDFat related code could be the place to code this in, see if SD card removed, how to flag and restart?, although after looking at the Marlin codebase, in particular SdFatUtils.cpp, this would appear to be a red herring.