To close a specific instance of TouchDesigner (or any other application like TouchPlayer) based on the file it has opened, we need to modify the approach slightly. The key is to ensure the script checks the command line arguments of the running processes accurately. TouchDesigner instances might not expose the filename in the MainModule.FileName property directly. Instead, we can inspect the command line arguments to determine which instance to close.
Here’s an updated version of the PowerShell script that should work:
$targetFilePath = "C:\Users\Florence Nightingale\Documents\Florence Nightingale Living Portrait\240712_IdleOnly.toe"
# Get the target file name
$targetFileName = [System.IO.Path]::GetFileName($targetFilePath)
# Get all instances of TouchDesigner (or touchplayer)
$processes = Get-Process -Name "touchplayer"
foreach ($process in $processes) {
# Get the command line for the process
$commandLine = (Get-WmiObject Win32_Process -Filter "ProcessId = $($process.Id)").CommandLine
# Check if the command line contains the target file path
if ($commandLine -like "*$targetFileName*") {
Stop-Process -Id $process.Id -Force
Write-Output "Closed process $($process.Id) with command line: $commandLine"
Write-Output "Skipped process $($process.Id) with command line: $commandLine"
-
Extract Target File Name:
$targetFileName = [System.IO.Path]::GetFileName($targetFilePath) extracts the file name from the full path.
-
Get All Instances of the Program:
$processes = Get-Process -Name "touchplayer" gets all running instances of TouchPlayer.
-
Inspect Command Line Arguments:
- For each process, retrieve its command line arguments using
Get-WmiObject.
- Check if the command line contains the target file name.
-
Close the Specific Instance:
- If the command line contains the target file name, the process is stopped.
-
Save the Script:
- Save the above script as
close_touchplayer_by_file.ps1.
-
Create a Batch File to Run the PowerShell Script:
- Open Notepad.
- Write the following:
powershell.exe -File "C:\path\to\your\close_touchplayer_by_file.ps1"
- Save it as
run_close_touchplayer_script.bat.
-
Create a Task in Task Scheduler:
- Open Task Scheduler.
- Create a new task with the necessary triggers and actions.
- In the Actions tab, set it to start
run_close_touchplayer_script.bat.
This approach ensures that you target the specific instance of TouchPlayer (or any other application) that opened the specified file, based on the command line arguments.