
    local function AddItemByNameToSelectedTracks(PATH,titleUndo);
	   
	   ---------------------------------------------------------
	   local function no_undo();reaper.defer(function()end);end;
	   ---------------------------------------------------------
	   
	   
	   local in_file = io.open(PATH,"r");
	   if not in_file then goto RemoveScript end;
	   in_file:close();
	   
	   
	   do;
		 -----------------------------------------------------
		  local CountSelTrack = reaper.CountSelectedTracks(0);
		  if CountSelTrack == 0 then; --no_undo() return end;
			 local CountTrack = reaper.CountTracks(0);
			 if CountTrack == 0 then;
				no_undo() return;
			 else;
				reaper.InsertTrackAtIndex(CountTrack,false);
				local Track = reaper.GetTrack(0,CountTrack);
				reaper.SetMediaTrackInfo_Value(Track,"I_SELECTED",1);
				CountSelTrack = reaper.CountSelectedTracks(0);
			 end;
		  end;
		  
		  
		  reaper.Undo_BeginBlock();
		  reaper.PreventUIRefresh(1);
	    
		  local CursorPosition = reaper.GetCursorPosition();
		  reaper.SelectAllMediaItems(0,0);
		  
		  local source = reaper.PCM_Source_CreateFromFile(PATH);
		  local NumChannels = reaper.GetMediaSourceNumChannels(source);
		  if NumChannels > 0 then;
			 for i = 1,CountSelTrack do;
				local Track_Sel = reaper.GetSelectedTrack(0,i-1);
				local NewItem = reaper.CreateNewMIDIItemInProj(Track_Sel,CursorPosition,CursorPosition+0.5,false);
				local Take = reaper.GetActiveTake(NewItem);
				local retval,lengthIsQN = reaper.GetMediaSourceLength(source);
				if lengthIsQN then;
				    retval = reaper.TimeMap2_QNToTime(-1,retval);
				end;
				reaper.SetMediaItemInfo_Value(NewItem,'D_LENGTH',retval);
				reaper.GetSetMediaItemTakeInfo_String(Take,"P_NAME",PATH:match(".+[/\\](.+)"),1);
				reaper.SetMediaItemInfo_Value(NewItem,'B_UISEL',1);
				reaper.SetMediaItemTake_Source(Take,source);
				reaper.SetEditCurPos(CursorPosition+retval,true,false);
			 end;
		  end;
		  reaper.Main_OnCommand(40047,0); -- build missing peaks
		  reaper.UpdateArrange();
		  
		  reaper.PreventUIRefresh(-1);
		  reaper.Undo_EndBlock(titleUndo,-1);
		  -----------------------------------
	   end;
	   
	   
	   ----------------
	   ::RemoveScript::
	   if not in_file then;
		  local MB = reaper.MB(
				  'Eng:\nThere is no file on this path !\n'..
				  'Delete the script and create a new one using\n'..
				  '"Archie_Item; Smart Template Add item by name to selected tracks.lua"\n\n'..
				  'TO REMOVE THE SCRIPT ?\n\n\n'..
				  'Rus:\nОтсутствует файл по данному пути !\n'..
				  'Удалите скрипт и создайте новый с помощью\n'..
				  '"Archie_Item;  Smart Template Add item by name to selected tracks.lua"\n\n'..
				  'УДАЛИТЬ СКРИПТ ?'
				  ,"Woops",1);
		  if MB == 2 then no_undo() return end;
		  
		  local ScriptPath = ({reaper.get_action_context()})[2];
		    
		  reaper.AddRemoveReaScript(false,0,ScriptPath,true);
		  os.remove(ScriptPath);
		  no_undo();
	   end;
	   ------------------------
    end;
    AddItemByNameToSelectedTracks('D:/songs/mena.mp3','Add media file(item) by name (يدق الباب) on track(s)');
