local cou = 0
		local cou2 = 0	
    local function no_undo()reaper.defer(function()end)end;
    -------------------------------------------------------
    
SC = reaper.CountSelectedMediaItems(0);    





if (SC == 0) then
	reaper.Main_OnCommand(40296, 0) -- Unselect all items
	reaper.Main_OnCommand(40421, 0) -- Unselect all items

SCC =  reaper.CountSelectedMediaItems(0);    
reaper.osara_outputMessage("")    
 CountSelItem= SCC
else
 CountSelItem= SC
end

    if CountSelItem == 0 then no_undo() return end;
    
    
    reaper.Undo_BeginBlock();
    reaper.PreventUIRefresh(1);
    
    for i = CountSelItem-1,0,-1 do;
        
        local SelItem = reaper.GetSelectedMediaItem(0,i);
		take = reaper.GetActiveTake(SelItem )
      if (take ~= nil) and   ( reaper.TakeIsMIDI(take)) then
--            reaper.SetMediaItemInfo_Value(SelItem,"B_UISEL",0);
        reaper.SetMediaItemSelected(SelItem , false)
cou = cou +1
else
        reaper.SetMediaItemSelected(SelItem , true)
cou2 = cou2 +1
        end;
    end;
if cou2 == 1 then
midier = " AudioItem has selected"
else
midier = " AudioItems have selected"
end


reaper.osara_outputMessage( cou2 .. midier)    
    reaper.PreventUIRefresh(-1);
    reaper.Undo_EndBlock('Unselect items if track not selected',-1);
    
    reaper.UpdateArrange();
