• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
2

Extendscript sample function for calling command line functions

LEGEND ,
Oct 08, 2020 Oct 08, 2020

Copy link to clipboard

Copied

Microsoft Windows has a great deal of functionality that can be accessed via the command line. Adobe Bridge has an Extendscript function for running commands but Photoshop does not. Additionally, using the built-in Bridge command opens the ugly command window which can confuse users.

 

Below is sample code to run both traditional command line and Powershell commands. This script creates a temporary .vbs file which calls Powershell to run commands. You'll need to supply actual file names from your system.

 

This code works in both Bridge and Photoshop and may work in other Adobe apps such as Illustrator and InDesign.

Please note that syntax is critical since this code is glue for multiple different scripting languages!

 

This is released as-is, you are on your own with it. Its Apache-licensed so you can use it per those open source terms. Save the attached text file as a ".jsxinc" file or simply copy/paste into your script.

 

Needless to say, this is Windows-only.

 

/*
Utility Pack Scripts created by David M. Converse ©2018-20

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!

This code is tested on Windows 10 with Bridge 2020 and Photoshop 21.2.4

Last modified 10/8/2020

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
function psCommand(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);
}
}

TOPICS
Actions and scripting , Windows

Views

845

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
no replies

Have something to add?

Join the conversation
Adobe