Change path of file before running a command in automator

I created a service in automator to open a file via quicklook, using the below code

qlmanage -p "$@"

I'm able to open the selected file via quicklook. But I want to open a different file located in another directory whilst selecting a file with same name.

Screenshot of automator

From the screenshot you'll see that the path of the selected file is /Users/kingamada/kut.stl

I want to change the path before the qlmanage -p "$@" command, so that even when selecting kut.stl in different folder, it's kut.stl in the specified directory that will be passed to qlmanage -p "$@".


The path provided to “run shell script” is literally just the text of the path. This means that you can modify it using your scripting language’s standard text munging tools.

I would probably do this in one of two ways:

  1. Just modify the file path in the same step as running quicklook. This has the advantage of keeping everything right next to each other in a single easily-visible step.
  2. Or, modify the file path in a separate “run shell script” step. This has the advantage of keeping each step separate, which itself has the advantage of allowing different tools for each step.

In bash, you can get the file name from a file path using $(basename $variable). For example:

$trueDirectory = "/Users/kingamada"
$filename = $(basename "$@")
qlmanage -p "$trueDirectory/$filename"

If you choose to do this in two steps, say, a Perl script to modify the file path first, then your Bash script to open quicklook, just output the modified file path to standard output.

use File::Basename;
$trueDirectory = "/Users/kingamada";
$chosenPath = shift;
$filename = basename($chosenPath)
print "$trueDirectory/$filename";

If you need to keep track of multiple variables through your automation, take a look at the “Set Value of Variable” action.

If you need to ask the user for multiple things, look at the “Run AppleScript” action as well as the various user interaction actions.