Access is denied writing to file from ExtendScript
Copy link to clipboard
Copied
I'm trying to write an ExtendScript for Photoshop 2024. I am using `app.system()` to execute other programs. I can't figure out how to get a script like that to write a file. I can't get a `>` redirection to write a file nor can I get a Python script to write a file.
For example, `app.system("echo hi > tests.txt; & pause")` will produce the following output:
```console
Access is denied.
Press any key to continue . . .
```
Whereas, `app.system("echo hi; & pause")` will produce this:
```console
hi;
Press any key to continue . . .
```
I've tried all manner of paths including ones in `Folder.temp` and they all fail similarly.
I've even tried opening the file first in ExtendScript via `f.open("w")` before running my script, and that fails with `The process cannot access the file because it is being used by another process.`
Explore related tutorials & articles
Copy link to clipboard
Copied
What are you trying to do, what exact kind of file are you trying to create, …?
Copy link to clipboard
Copied
app.system("cd /d %temp% & echo hi > tests.txt; & pause")
The console process is launched from the app folder 'C:\Program Files\Adobe\Adobe Photoshop 2024' If you do not have administrator rights, then it is not surprising that you receive an error message 🤷
Copy link to clipboard
Copied
I found that this still doesn't help. The problem turned out to be that if the file was on the C: drive, it couldn't be written to no matter if I launched the script or Photoshop as an administrator. I ended up using `subst` to remap anything on the C: drive to some other drive. I used the following functions to do this:
function mapNextAvailableDriveLetter(path) {
var driveLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (var i = 0; i < driveLetters.length; i++) {
var result = app.system("subst " + driveLetters[i] + ': "' + path + '"');
if (result === 0) {
return driveLetters[i];
}
}
alert("Unable to map a drive letter to " + path);
exit();
}
And to remove that drive mapping:
function removeSubstDriveLetters() {
var driveLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var cmd = "@echo off";
for (var i = 0; i < driveLetters.length; i++) {
cmd = cmd + " & subst " + driveLetters[i] + ": /D >nul 2>nul";
}
app.system(cmd);
}
There is more discussion over at StackOverflow: permissions - Access is denied writing to file from ExtendScript - Stack Overflow
This is so that I can have external tools generate an output and I can read via ExtendScript. It also allows me to have ExtendScript create batch/Python scripts that I can then call using
app.system()
to do things outside of Photoshop.
Copy link to clipboard
Copied
I'm a Mac user and the whole ugly command window thing on Windows drives me nuts, so I have started using Windows Scripting Host and Powershell for my scripts to both bypass this (Microsoft has no taste, but I certainly do) and to tap into the features of Powershell.
The syntax can be tricky but its much more elegant and works as well as anything else on Windows- clunky but usable.
/*
Utility Pack Scripts created by David M. Converse ©2018-21
This script is a sample include file for scripters that contains a
template function to run command line actions in Windows
without the command line window showing. This function takes
either a native Powershell function or command prompt code,
writes it into a vbs file, and executes the vbs file to launch Powershell.
Syntax is critical, note all of the escaped characters!
Last modified 6/3/2021
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
PS Example to create a zip file:
(requires Powershell 5 or later)
//list of files to compress
var inputName = '\'C:\\Users\\test\\Desktop\\foo.txt\', \'C:\\Users\\test\\Desktop\\bar.txt\'';
//name of zip archive
var archName = '\'C:\\Users\\test\\Desktop\\archive.zip\'';
//assembled command
var psParam = 'Compress-Archive -LiteralPath '+ inputName +' -DestinationPath ' + archName;
//send command to function
psCommand(psParam);
*/
/*
Command Example to call the DNG Converter and convert files to DNG:
(requires Adobe DNG Converter in default install path)
//path to DNG Converter
var appPath = '\'C:\\Program Files\\Adobe\\Adobe DNG Converter\\Adobe DNG Converter.exe\'';
//command line arguments
var args = '-c -p1';
//files to process, note escaped quotes
var filePaths = ' \""C:\\Users\\test\\Desktop\\img001.CR2\"" \""C:\\Users\\test\\Desktop\\img002.CR2\""';
//assembled command
var psParam = 'Start-Process -FilePath '+ appPath + ' -ArgumentList \'' + args + filePaths;
//send command to function
psCommand(psParam);
*/
//pass the command
psCommand = function(psParam){
try{
//PS command encapsulated in vbs
var commandStr = 'CreateObject("Wscript.Shell").Run "powershell -NoLogo -NoProfile -Command ' + psParam + '\'", 0, True';
//create temp vbs file in ~\AppData\Local\Temp\
//use forward slash for Extendscript paths
var vbsTempFile = new File(Folder.temp + '/vbstemp.vbs');
//open for write
vbsTempFile.open ('w');
//write command to vbs file
vbsTempFile.write(commandStr);
//close file
vbsTempFile.close();
//execute vbs file
File(vbsTempFile).execute();
//adjust time as required before vbs file is deleted
$.sleep(1500);
//delete vbs file
File(vbsTempFile).remove();
}
catch(e){
//alert on error
alert(e + ' ' + e.line);
}
}

