views:

530

answers:

2

Is there a way to specify a working directory to the Start-Job command?

Use-case:

I'm in a directory, and I want to open a file using Emacs for editing. If I do this directly, it will block PowerShell until I close Emacs. But using Start-Job attempts to run Emacs from my home directory, thus having Emacs open a new file instead of the one I wanted.

I tried to specify the full path using $pwd, but variables in the script block are not resolved until they're executing in the Start-Job context. So some way to force resolving the variables in the shell context would also be an acceptable answer to this.

So, here's what I've tried, just for completeness:

Start-Job { emacs RandomFile.txt }
Start-Job { emacs "$pwd/RandomFile.txt" }
+1  A: 

A possible solution would be to create a "kicker-script":

Start-Job -filepath .\emacs.ps1 -ArgumentList $workingdir, "RandomFile.txt"

Your script would look like this:

Set-Location $args[0]
emacs $args[1]

Hope this helps.

Filburt
This worked out really well. The -ArgumentList is exactly the kind of thing I was looking for.
jdmichal
A: 

Just for completeness, here's the final script I implemented based on Filburt's answer, community-wiki style:

function Start-Emacs ( [string]$file )
{
    Start-Job -ArgumentList "$pwd\$file" { emacs $args[0] }
}
jdmichal

related questions