This is my attempt to automate the solution from Use cleverref to display page range of included .pdf files. This seems to work fine, unless the file name I pass to \AddLinkToFile is contained in a macro. It seems to be an expansion issue with the two uses of \g@addto@macro.
What is the \expandafter magic required here?
Code:
\documentclass{article}
\usepackage{xparse}
\usepackage{graphicx}
\usepackage{pdfpages}
\usepackage{xcolor}
\usepackage{lipsum}
\usepackage{xstring}
\usepackage{etoolbox}
%\usepackage{fancyhdr}
\usepackage[export]{adjustbox}% https://tex.stackexchange.com/questions/6073/scale-resize-large-images-graphics-that-exceed-page-margins
\usepackage[all,nodeanchor={south east}, color=magenta, opacity=1]{background}
\usepackage{hyperref}
\usepackage{cleveref}
%% Use background package to add actual page numbers in bottom right hand of margin
\newcommand{\FileNameToDisplayInMargin}{}
\SetBgContents{\tikz \node at (0,0) {Page \thepage\rotatebox{90}{\hspace*{2.0em}\ttfamily\FileNameToDisplayInMargin}};}
\SetBgPosition{current page.south east}
\SetBgHshift{-0.5em}
\SetBgVshift{0.5ex}
\SetBgAngle{0.0}% Select rotation
\SetBgScale{1.5}% Select scale factor
\makeatletter
\newcommand*{\NotePage}[1]{%
\hfill\tikz[overlay] \path ++(1.0cm,0) node[anchor=west, color=red] {#1};%
}
\newcommand{@ListOfIncludes}{\pagestyle{empty}}% Build list of files to include
\AtEndDocument{@ListOfIncludes}%
\NewDocumentCommand{\AddLinkToFile}{%
m% #1 = text to display
m% #2 = file to open upon clicking
}{%
\edef\ExpandedFileName{#2}%
\par
\IfFileExists{"#2"}{%
File: \href{run:\ExpandedFileName}{\textcolor{blue}{#1}}%
\IfEndWith{#2}{.pdf}{% .pdf files has a page range
%% https://tex.stackexchange.com/questions/198091/get-number-of-pages-of-external-pdf
\pdfximage{#2}%
\edef\LastPage{\the\pdflastximagepages}%
\ifnum\LastPage=1
\NotePage{\Cpageref{#2 1}}% Only a single page in this .pdf
\else
%% Page range is page "1" to \LastPage of this .pdf
\NotePage{\Cpagerefrange{#2 1}{#2 \LastPage}}%
\fi
%% Add to list so that this file is included in this document
\g@addto@macro@ListOfIncludes{\IncludePdfFile{#2}}%
}{% non .pdf files are only a single page.
\NotePage{\Cpageref{#2}}%
%% Add to list so that this file is included in this document
\g@addto@macro@ListOfIncludes{\IncludeImageFile{#2}}%
}%
}{%
File: \href{run:\ExpandedFileName}{\textcolor{orange}{#1}}%
\typeout{**** Warning: Failed to link file "\ExpandedFileName".}%
}%
%%\show@ListOfIncludes
}%
\makeatother
\newcounter{CurrentPageNumber}
\newcommand*{\LabelThisPage}[1]{%
% #1 = label prefix
\thispagestyle{empty}%
\stepcounter{CurrentPageNumber}%
\phantomsection%
\label{#1 \arabic{CurrentPageNumber}}%
}%
\newcommand{\IncludePdfFile}[1]{%
% #1 = .pdf file name (with path)
\edef\ExpandedFileName{#1}%
\ifcsdef{\ExpandedFileName Previously Linked}{}{%
\newpage%
\setcounter{CurrentPageNumber}{0}%
\edef\FileNameToDisplayInMargin{File = \ExpandedFileName}%
\includepdf[pagecommand={\LabelThisPage{#1}}, pages=1-last]{\ExpandedFileName}%
\csdef{\ExpandedFileName Previously Linked}{}% No need to re-include this file
}%
}%
\newcommand{\IncludeImageFile}[1]{%
% #1 = image file name (with path) non .pdf)
%% For non .pdf file we can place multiple images per page, so no \newpage here
\phantomsection%
\label{#1}%
\def\FileNameToDisplayInMargin{File = #1}%
\noindent\includegraphics[max width=\linewidth,keepaspectratio=true]{#1}%
}%
\begin{document}
%%% This does not:
%\def\FileName{kant.pdf}
%\AddLinkToFile{Kant 1}{\FileName}
%\def\FileName{lipsum1.pdf}
%\AddLinkToFile{Lipsum Single Page}{\FileName}
%\def\FileName{../images/EiffelWide.jpg}
%\AddLinkToFile{Eiffel 1}{\FileName}
%\def\FileName{lipsum.pdf}
%\AddLinkToFile{Lipsum}{\FileName}
%\def\FileName{kant.pdf}
%\AddLinkToFile{Kant 1 Duplicate Link}{kant.pdf}%
%\def\FileName{}
%% This works:
\AddLinkToFile{Kant 1}{kant.pdf}
\AddLinkToFile{Lipsum Single Page}{lipsum1.pdf}
\AddLinkToFile{Eiffel 1}{../images/EiffelWide.jpg}
\AddLinkToFile{Lipsum}{lipsum.pdf}
\AddLinkToFile{Kant 1 Duplicate Link}{kant.pdf}%
\end{document}