Good thing, that the beamer
frame collects its body in a token list like the amsmath
environments. Thus we can simply examine the token list where the contents are stored. However, we have to hack the beamer frame for that. All the arguments to the frame, in this case {First Slide}
, are counted as well. Brace groups count as one token and other oddities apply, see the documentation of \seq_set_split
.
\documentclass{beamer}
\usepackage{xparse}
\ExplSyntaxOn
\cs_generate_variant:Nn \seq_set_split:Nnn { Nno }
\NewDocumentCommand \setwordcount { m m }
{
% split the frame contents at spaces
\seq_set_split:Nno \l_tmpa_seq { ~ } { \the#2 }
% remove all mentions of the counter variable
\seq_remove_all:Nn \l_tmpa_seq { #1 }
% set counter
\tl_gset:Nx #1 { \seq_count:N \l_tmpa_seq }
}
\ExplSyntaxOff
\makeatletter
\def\beamer@frameenv{%
\def\beamer@process@envbody{\endgroup%
\setwordcount\insertwordcount\beamer@envbody% Count number of space delimited tokens in the input
\expandafter\expandafter\expandafter\beamer@framecommand\expandafter\beamer@frameoptions\expandafter{\the\beamer@envbody}}%
\global\beamer@envbody{}\def\beamer@begin@stack{b}%
\begingroup
\let\frame\beamer@collect@@body
\def\beamer@process@envbody{\frame}%
\beamer@process@envbody%
}
\makeatother
\begin{document}
\begin{frame}{First Slide}
This is my first slide.
\insertwordcount
\end{frame}
\end{document}

It may be better to count words with l3regex
... but maybe not. Who knows? This is recursive in subgroups and doesn't count control sequences but math material is still tricky.
This produces similar output but with count 7.
\documentclass{beamer}
\usepackage{xparse}
\ExplSyntaxOn
\cs_generate_variant:Nn \regex_count:nnN { noN }
\NewDocumentCommand \setwordcount { m m }
{
% regex match words
\regex_count:noN { \w+ } { \the#2 } \l_tmpa_int
% set counter
\tl_gset:Nx #1 { \int_to_arabic:n \l_tmpa_int }
}
\ExplSyntaxOff
\makeatletter
\def\beamer@frameenv{%
\def\beamer@process@envbody{\endgroup%
\setwordcount\insertwordcount\beamer@envbody% Count number of space delimited tokens in the input
\expandafter\expandafter\expandafter\beamer@framecommand\expandafter\beamer@frameoptions\expandafter{\the\beamer@envbody}}%
\global\beamer@envbody{}\def\beamer@begin@stack{b}%
\begingroup
\let\frame\beamer@collect@@body
\def\beamer@process@envbody{\frame}%
\beamer@process@envbody%
}
\makeatother
\begin{document}
\begin{frame}{First Slide}
This is my first slide.
\insertwordcount
\end{frame}
\end{document}
\documentclass{beamer}\usepackage{lipsum}\begin{document}
\frame{\lipsum[10]}\end{document}
. Again how is this remotely related to the question? This is pure OCD. Not every question needs a fake MWE for justification\begin{frame}
and pass each frame through texcount). Voting to reopen.