How do I export contents between xml tags based on names in Extendscript for Indesign? - adobe-indesign

All I'd like to do here is open an InDesign 2018 CC file, pull out text uniquely (here I've chosen to grab content inside XML tag called "Title" from named tag window in the InDesign application side), save it to a txt file, and close the InDesign doc. I'm working in the Extendscript app, using Adobe InDesign CC 2018 (13.064). I just need to push to a txt file only certain named data (textboxes, xmltags, pageitems, etc) the contents based on anything, but via the name of the data holder. But xmltags are the only objects that I can name in the InDesign app apart from layers, and layers won't work for other reasons. So I'm stuck not being able to refer to xml-tagged contents. Please help.
Note:
I get an error with this code saying "Title" isn't defined, and I understand the error, but not sure how to utilize the method XML.toString() without referring to an object that's named inside the InDesign file. So I guess I'm using the wrong method to refer to xml-tagged data already located in a file??
So naturally, I throw out XML.toString() and utilize the commented out code (below) "app.activeDocument.xmlItems.item;" thinking maybe I will get an array of all items that are xml tagged, which is not even specific enough for my goal, but I'm desperate, and I get another newer error regarding the "exportfile" line of code: myArticles.exportFile() is not a function.
My code so far:
app.open(File("C:/Users/Sean/Desktop/New folder/va tech 2.indd"), true);
myArticles = Title.toString();
//THIS ATTEMPT WON'T WORK EITHER AS RPLCMNT FOR LINE ABOVE: myArticles= app.activeDocument.xmlItems.item;
myArticles.exportFile(ExportFormat.textType, new File("/C/Users/Sean/Desktop/New folder/test.txt"), false);
app.documents.everyItem().close(SaveOptions.NO);

var main = function() {
var doc, root, xes, n, nXE, st, xc, strs = [],
f = File ( Folder.desktop+"/contents.txt" );
try {
//NEED TO CHANGE THE URL. Ex: some/url > /Users/user/Desktop/foo.indd
doc = app.open ( File ( "some/url" ) );
}
catch(err){
alert(err.message);
return;
}
if ( !doc ) {
alert("You need an open document" );
return;
}
root = doc.xmlElements[0];
xes = root.evaluateXPathExpression("//Title");
n = xes.length;
while ( n-- ) {
nXE = xes[n];
xc = nXE.xmlContent;
if ( xc instanceof Story ) {
strs.push( xc.contents );
}
}
if ( strs.length ) {
f.open('w');
f.write ( strs.reverse().join("\r") );
f.close();
}
}
var u;
app.doScript ( "main()",u,u,UndoModes.ENTIRE_SCRIPT, "The Script" );

Related

October CMS issue with file attachment during tests

I am using October CMS
I have a model which has some attachOne and attachMany file attachments.
Both of these work fine during normal use.
However I am having difficulty with setting up tests
I have the following code in my test class
$logo = file(__DIR__ . '/assests/images/logo.png');
$file = new System\Models\File;
$file->data = __DIR__ . '/assests/images/Sample-Photo.jpeg';
$file->is_public = 1;
$file->save();
$nhd->developer_logo = (new System\Models\File)->fromData($logo, 'logo.png');
$nhd->development_photos->add($file);
$nhd->save();
$developer_logo_path = $nhd->developer_logo->getPath();
$photo_path = $nhd->development_photos[0]->getPath();
Both the ‘$developer_logo_path’ and ‘$photo_path’ variables successfully return a stored file path ( which actually points to a file which has been saved ) which is what i would except.
However if i then let the test run and execute the normal code. If I put a breakpoint in the component php file at the point after the model referred to in the test code as $nhd has been loaded there is a ‘developer_logo’ attached ( the AttachOne file ) but there are no ‘development_photos’ ( the attachMany file ).
As a result the template also does not output anything when {{component.development_photos}} is used
I also tried using the inline method to add the attachMany file ( $nhd->development_photos = (new System\Models\File)->fromData($photo, ‘logo.png’); ) and this also did not work.
Can anyone help me?

Extendscript in Indesign not creating or writing to file

I am writing a script for Indesign that collects the contents from TextStyleRange objects, adds them to a javascript object, and should then write out a JSON file with the stringified JSON.
I can create the JSON just fine, with the contents from all of the TextStyleRanges in the current document, however the final file never gets written.
Here is a simplified example of what I have:
#include "json2.js";
main();
function main(){
myObj = { "key_1": "value_1", "key_2": "value_2" };
myContents = JSON.stringify(myObj, null, 4);
myFile = new File("~/Documents/myproject/en/translation.json");
myFile.open("w");
myFile.write( myContents );
$.writeln( myContents );
myFile.close();
}
In the VSCode debugger I can correctly see the JSON being output by $.writeln, however the file is not being created on disk. Any ideas what might be going wrong?
I'm using Windows 10, and Adobe Indesign 2022.
I have also tried with file edit but no luck:
myFile.open("e", "????", "????");
Actually it was simply that I needed to create the subfolder first:
var parentFolder = myFile.parent;
if (!parentFolder.exists && !parentFolder.create())
throw new Error("Cannot create file in path " + myFile.fsName);
After adding this to my script the folder and the file were created successfully.

reading data from multiple tabs in excel

I have some data in multiple tabs in excel and those need to be read using cypress. Is there a way to do it? The only suggested way to read data from excel is to convert to JSON file. So, I referred and wrote this in ./cypress/support/command.js file with an intention that each tab will be stored as JSON file.
Cypress.Commands.add('dataFile', (excelFile) => {
let XLSX = require('xlsx');
let fs = require('fs');
let XLBook = XLSX.readFile('cypress/fixtures/' + excelFile);
let XLTabs = XLBook.SheetName;
for(let i = 0; i < XLTabs.length; i++)
{
let XLTabName = XLTabs[i].toString();
let XLData = XLSX.utils.sheet_to_json(XLBook.Sheets[XLTabs[i]);
fs.writeFile('./cypress/fixtures/jSONData.json', JSON.stringify(XLData , null, 2), (e: string) => {
if(e) {
console.log(e);
}
});
}
});
I used the below command in my test to use it.
cy.dataFile('File1.xlsx');
But, the test was highlighting this error:
Property 'dataFile' does not exist on type 'cy & EventEmitter'.ts(2339)
Please read Types for custom commands to resolve the error
Property 'dataFile' does not exist on type 'cy & EventEmitter'.ts(2339)
But you cannot use a custom command, because both let XLSX = require('xlsx') and let fs = require('fs') must be used in a task.
Please see this answer Read excel files in Cypress for the broad strokes.

JXA: Create a mailbox in Apple Mail

I am trying to create a sub-mailbox in Apple Mail using JavaScript.
I have the following code snippet (parent is a reference to the mailbox in which I want the new mailbox):
var mb = mail.Mailbox({name: "SubFolder"});
parent.mailboxes.push(mb);
The events log shows:
app = Application("Mail")
app.mailboxes.byName("Local").mailboxes.byName("Archive").mailboxes.push(app.Mailbox({"name":"SubFolder"}))
--> Error -10000: AppleEvent handler failed.
What am I doing wrong?
Thanks,
Craig.
Code now:
var mb = mail.Mailbox({name: "Local/Archive/Test Archive/SubFolder"})
logger.logDebug("mb = '" + Automation.getDisplayString(mb) + "'.");
mail.mailboxes.push(mb) // create the subfolder
This works as long as there are no spaces in the path.
I tried to force the space using \\ in front of it, but then you get "Test\ Archive" as the name.
So how do I get a space in the name to work?
Thanks.
To create a sub-folder, you need a name like a posix path --> "/theMasterMailbox/subMailBox1/subMailBox2/subMailBox3".
So, you need:
A loop to put the name of each parent folder into an array.
Use join('/') to join the elements of an array into a string.
Use mail.mailboxes.push(mb) instead of parent.mailboxes.push(mb)
Here's a sample script which creates a mailbox named "SubFolder" in the selected folder (the mailbox):
mail = Application('com.apple.Mail')
parent = mail.messageViewers()[0].selectedMailboxes()[0]
mboxNames = [parent.name()]
thisFolder = parent
try {
while (true) { // loop while exists the parent folder
mboxNames.unshift(thisFolder.container().name()) // add the name of the parent folder to the beginning of an array
thisFolder = thisFolder.container() // get the parent of thisFolder
}
} catch (e) {} // do nothing on error, because thisFolder is the top folder
mboxNames.push("SubFolder") // append the name of the new subFolder to the array
mBoxPath = mboxNames.join('/') // get a string (the names separated by "/")
mb = mail.Mailbox({name:mBoxPath})
mail.mailboxes.push(mb) // create the subfolder

downloading supporting documents from www folder in shiny application

Since the dataset that drives the app is huge, In my shiny application, I am creating a glossary page. However, I could not cover all the details. Some of them I like to provide as downloadable word or pdf files. Assuming the name of my file is
estimates.doc
which i have saved in www folder of my app, my code within the UI of the app is as follows:
library(shiny)
ui <- navbarPage("TITLE HERE",
tabPanel("GLOSSARY",
p("Click", a(href = "~/www/Estimation_Procedure.doc", "here"), "to download.")
)
)
server <- function(session, input, output) {}
shinyApp(ui, server)
I am not sure how to include the downloadHandler here...i am getting the message "NOT FOUND".
You can use a downloadHandler and downloadLink.
library(shiny)
ui <- navbarPage("TITLE HERE",
tabPanel("GLOSSARY",
downloadLink(outputId = 'myFile' , label = 'to download' )
)
)
server <- function(session, input, output) {
output$myFile <- downloadHandler(
# generate file name
filename = function() {
'Estimation_Procedure.doc'
} ,
# set file content
content = function(file) {
file.copy('www/Estimation_Procedure.doc' , file)
}
)
}
shinyApp(ui, server)

Resources