# Get write18 to accept / expand command

I am trying to use write18 to crop a pdf. The code is

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}

\usepackage{graphicx}

\newcommand{\filename}{}
\newcommand{\croppedfilename}{}

\begin{document}

\makeatletter
\renewcommand{\filename}{mypath/myimage.pdf}
\filename@parse{mypath/myimage.pdf}
\renewcommand{\croppedfilename}{\filename@area\filename@base.cropped.\filename@ext}
\makeatother

filename: \filename

croppedfilename: \croppedfilename

\immediate\write18{pdfcrop mypath/myimage.pdf \croppedfilename}%

\immediate\write18{pdfcrop \filename \croppedfilename}%

\the\pdfshellescape

\end{document}


Here \the\pdfshellescape is 1.

The line \immediate\write18{pdfcrop mypath/myimage.pdf \croppedfilename} does produce my cropped pdf, but \immediate\write18{pdfcrop \filename \croppedfilename} does not. Of course I need the second version in order to put it into a command.

runsystem(pdfcrop mypath/myimage.pdf mypath/myimage.cropped.pdf)...executed.

Notice the lack of a space in the second line. What's happening is the usual TeX behaviour of skipping spaces after macro names: in your case \filename. Thus you need to make sure a space is 'seen' there:
\immediate\write18{pdfcrop \filename\space\croppedfilename}