docs.sheetjs.com/docz/docs/03-demos/02-extendscript.md
2022-08-13 18:01:26 -04:00

5.5 KiB

sidebar_position
2

Adobe Apps

import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';

Photoshop, InDesign and other Adobe Creative Suite applications offer extension support. Over the years there have been a few different JavaScript platforms:

  • "ExtendScript": This uses an old JavaScript dialect but is supported in older versions of Creative Suite and Creative Cloud.

  • "CEP": This was recommended in CS6 but eventually deprecated.

  • "UXP": This is the current Adobe recommendation for new CC extensions.

This demo intends to cover the SheetJS-related parts. General setup as well as general Adobe considerations are not covered here. A basic familiarity with extension development is assumed.

ExtendScript Scripts

Installation is straightforward: download a script and move it to your project directory.

Reading Files

XLSX.readFile can directly accept an absolute URI:

var workbook = XLSX.readFile("~/Documents/test.xlsx");

The path can be user-configurable using File.openDialog:

/* Show File Picker */
var thisFile = File.openDialog("Select a spreadsheet");
if(!thisFile) { alert("File not found!"); return; }

/* Read file from disk */
var workbook = XLSX.readFile(thisFile.absoluteURI);
Complete Example (click to hide)

In this example, the script will show a dialog to select a file. After reading the file, the workbook Author property will be extracted and the Photoshop doc author (activeDocument.info.author) will be changed accordingly.

This demo was verified in Photoshop CS6 64-bit on Windows 10.

#target photoshop
#include "xlsx.extendscript.js";

function main_parse() {
  /* Show File Picker */
  var thisFile = File.openDialog("Select a spreadsheet");
  if(!thisFile) { alert("File not found!"); return; }

  /* Read file from disk */
  var workbook = XLSX.readFile(thisFile.absoluteURI);

  /* Get Workbook Author */
  var Props = workbook.Props; if(!Props) { alert("Missing Author!"); return; }
  var Author = Props.Author; if(!Author) { alert("Missing Author!"); return; }

  /* Change Document Author to Workbook Author */
  var info = activeDocument.info;
  alert("Changing Author from |" + info.author + "| to |" + Author + "|");
  info.author = Author;
}

main_parse();
  1. Download the test workbook.

  2. Download the following scripts:

and place in the scripts directory. For CS6 Windows 10 the path is typically

C:\Program Files\Adobe\Adobe Photoshop CS6 (64 Bit)\Presets\Scripts

  1. Restart Photoshop and open a file (or create a new one)

  2. File > Scripts > parse and select the test workbook

  3. An alert will confirm that the file was read and the author will be changed:

"Changing Author" popup

  1. File > File Info... should show the updated Author field!

Writing Files

XLSX.writeFile can directly accept an absolute URI:

XLSX.writeFile(workbook, "~/Documents/test.xlsx");

The path can be user-configurable using File.saveDialog:

/* Show File Picker */
var thisFile = File.saveDialog("Select an output file", "*.xlsx;*.xls");
if(!thisFile) { alert("File not found!"); return; }

/* Write file to disk */
XLSX.writeFile(workbook, thisFile.absoluteURI);
Complete Example (click to hide)

In this example, the script will show a dialog to select an output file. Once selected, the library will create a new workbook with one worksheet. Cell A1 will be "Author" and cell B1 will be the active Photoshop document Author. The PS author is available as activeDocument.info.author.

This demo was verified in Photoshop CS6 64-bit on Windows 10.

#target photoshop
#include "xlsx.extendscript.js";

function main_write() {
  /* Show File Picker */
  var thisFile = File.saveDialog("Select an output file", "*.xlsx;*.xls");
  if(!thisFile) { alert("File not found!"); return; }

  /* Create new Worksheet */
  var ws = XLSX.utils.aoa_to_sheet([
    ["Author", activeDocument.info.author]
  ]);

  /* Create new Workbook and add worksheet */
  var wb = XLSX.utils.book_new();
  XLSX.utils.book_append_sheet(wb, ws, "Sheet1");

  /* Write file to disk */
  XLSX.writeFile(wb, thisFile.absoluteURI);
  alert("Created File " + thisFile.absoluteURI);
}

main_write();
  1. Download the following scripts:

and place in the scripts directory. For CS6 Windows 10 the path is typically

C:\Program Files\Adobe\Adobe Photoshop CS6 (64 Bit)\Presets\Scripts

  1. Restart Photoshop and open a file (or create a new one)

  2. File > File Info ... and confirm there is an Author. If not, set to SheetJS

  3. File > Scripts > write and use the popup to select the Documents folder. Enter SheetJSPSTest.xlsx and hit "Save"

  4. An alert will confirm that the file was created:

"Created File" popup

  1. Open the generated SheetJSPSTest.xlsx file and compare to Photoshop author

CEP

The standalone scripts can be added to CEP extension HTML

UXP

UXP officially recommends require and NodeJS Modules for third party support.

Use the "Frameworks" instructions to download.