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

Equivalent of the Place() method in UXP

Explorer ,
Nov 20, 2023 Nov 20, 2023

Copy link to clipboard

Copied

Hello everyone,

 

I recently tried to update my knowledge in indesign scripting by learning InDesign UXP plugins. It's a bit complicated but most of it work fine.

 

Unfortunately I am facing an issue without a solution despite my extensive research. I cannot find an equivalent to the 'Place()' function we had in regular scripting. It seems to no longer be available for UXP plugins.

 

I'm trying to import a "Referenced Text Adobe InDesign" file but I have no solution. Anyone has an idea or a more complete documentation?

 

I used to code it that way :

var file = new File(chemin + "/temp" + Math.random(999999999) + ".txt") // creating a temp file with my references
file.encoding = 'ASCII-MAC';
file.open('w');
file.write("<ASCII-MAC>\n");
file.write(myReferencedText); // a variable defined earlier
file.close();
myPara.place(file) // the paragraphe to replace

 I managed to create the TXT file, but not importing it in my story (like a cmd+D on macos).

 

Cheers everyone,

Nicolas

TOPICS
UXP Scripting

Views

547

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

correct answers 2 Correct answers

Community Expert , Nov 21, 2023 Nov 21, 2023

Perhaps the problem is "File", not "place". You need to use fs or localFileSystem for file access in UXP.

 

Alternatively, it is possible to execute ExtendScript from UXP.

const placeText = `
var chemin = '~/Desktop' ;
var file = new File(chemin + '/temp' + Math.random(999999999) + '.txt') // creating a temp file with my references
file.encoding = 'ASCII-MAC';
file.open('w');
file.write('<ASCII-MAC>\\n');
file.write('a variable defined earlier\\n'); // a variable defined earlier
file.close();

v
...

Votes

Translate

Translate
Community Expert , Nov 21, 2023 Nov 21, 2023

A UXP script like this was successful. It seems likely that you have an invalid path set in filePathExport.

const fs = require('fs') ;
const { localFileSystem } = require('uxp').storage ;
const { app } = require('indesign') ;

const filePathExport = '/Users/user_name/Desktop/temp.txt' ;
const encodedContent = 'a variable defined earlier\\n' ;
await fs.writeFile(filePathExport, encodedContent, {encoding: 'utf-8'}) ; // writeFile returns Promise<number> - the length of contents written to the file
...

Votes

Translate

Translate
Community Expert ,
Nov 20, 2023 Nov 20, 2023

Copy link to clipboard

Copied

Can you place this file manually?

 

In your case, you are executing place() on the myPara object - so it should behave as normal?

 

But I'm neither JS expert nor UXP, so I might be completely wrong...

 

Check samples that are included with InDesign:

RobertTkaczyk_0-1700522509251.png

 

Not for text but looks pretty "standard" - ImageCatalog.idjs:

 

RobertTkaczyk_1-1700522545911.png

 

Votes

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
Community Expert ,
Nov 21, 2023 Nov 21, 2023

Copy link to clipboard

Copied

Perhaps the problem is "File", not "place". You need to use fs or localFileSystem for file access in UXP.

 

Alternatively, it is possible to execute ExtendScript from UXP.

const placeText = `
var chemin = '~/Desktop' ;
var file = new File(chemin + '/temp' + Math.random(999999999) + '.txt') // creating a temp file with my references
file.encoding = 'ASCII-MAC';
file.open('w');
file.write('<ASCII-MAC>\\n');
file.write('a variable defined earlier\\n'); // a variable defined earlier
file.close();

var myPara = app.activeDocument.selection[0].paragraphs[0];
myPara.place(file) // the paragraphe to replace
` ;

const { app, ScriptLanguage, UndoModes } = require('indesign');
app.doScript(placeText, ScriptLanguage.JAVASCRIPT, [], UndoModes.ENTIRE_SCRIPT);

Votes

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
Explorer ,
Nov 21, 2023 Nov 21, 2023

Copy link to clipboard

Copied

Hello Robert and Sttk3,

 

Thanks for your answers, these are two good leads to explore.

The "place()" function seems indeed to be working, but not in my case.

I managed to get an error message with the UXP debugger : "Unable to create the link resource from the given URI."

It is maby a permission issue, while I have modified the manifest in this way:

"localFileSystem" : "fullAccess"

 

I used these (simplified) lines of code:

const { storage } = require("uxp");
const fs = require('fs')

const file = await fs.writeFile(filePathExport, encodedContent, { encoding: "utf-8" }); // "encodedContent" is the file content with the header needed
let selectionLength = selection.length - 2
selection.characters.itemByRange(0, selectionLength).place(file) // selection is the paragraph to replace.

 

If I can't do it with native UXP, I'll use Sttk3 method. It's a good news to be able to use "regular script" inside UXP.

 

Thanks again,

Nicolas

Votes

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
Community Expert ,
Nov 21, 2023 Nov 21, 2023

Copy link to clipboard

Copied

A UXP script like this was successful. It seems likely that you have an invalid path set in filePathExport.

const fs = require('fs') ;
const { localFileSystem } = require('uxp').storage ;
const { app } = require('indesign') ;

const filePathExport = '/Users/user_name/Desktop/temp.txt' ;
const encodedContent = 'a variable defined earlier\\n' ;
await fs.writeFile(filePathExport, encodedContent, {encoding: 'utf-8'}) ; // writeFile returns Promise<number> - the length of contents written to the file
const file = await localFileSystem.getEntryWithUrl(filePathExport) ;
const selection = app.activeDocument.selection[0] ;
const selectionLength = selection.length - 2 ;
selection.characters.itemByRange(0, selectionLength).place(file) ; // selection is the paragraph to replace.

 

Votes

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
Explorer ,
Nov 21, 2023 Nov 21, 2023

Copy link to clipboard

Copied

WOW! It worked perfectly, thanks a lot!

 

I understand now how to use "localFileSystem", I used it this way:

const fsProvider = require('uxp').storage.localFileSystem;

 And I get a little bit more this method "getEntryWithUrl" and how to use it.

 

A big thanks again, for the solution and the knowledge!

 

All the best,

Nicolas

Votes

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
Community Expert ,
Nov 21, 2023 Nov 21, 2023

Copy link to clipboard

Copied

getEntryWithUrl creates an Entry from a path string. Entry is an object like ExtendScript's File and Folder together, so think of it as an alternative to new File(pathString).

Votes

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
Explorer ,
Nov 22, 2023 Nov 22, 2023

Copy link to clipboard

Copied

LATEST

It's much clearer now, thanks again.

Votes

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