Create shell script from Extendscript Toolkit - bash

As one of the outputs from an Extendscript, I want to create a shell script which can then be executed by the user. Here is a very basic example:
function createShellScript()
{
var contents = "#!/bin/bash\ndate";
var outputFolder = Folder.selectDialog ("Choose where to save:");
var shFile = new File(outputFolder.absoluteURI + "/shell.sh");
shFile.open("W");
shFile.write(contents);
shFile.close();
}
createShellScript ();
If I take the resulting file (shell.sh), run chmod +x on it to make it exectuable, and then run it, nothing happens.
If, however, I adjust the script above to create the same content but a text file – so it outputs shell.txt open the file, copy the contents into a blank document in a code editor, and save as a .sh file, and then chmod and run it, it works fine.
Why does Extendscript not produce a proper .sh file when using this method?
Thanks for any help.
S

You need to set the line feed characters to unix style. For example, shFile.lineFeed = "Unix";.
function createShellScript()
{
var contents = "#!/bin/bash\ndate";
var outputFolder = Folder.selectDialog ("Choose where to save:");
var shFile = new File(outputFolder.absoluteURI + "/shell.sh");
shFile.open("W");
shFile.lineFeed = "Unix";
shFile.write(contents);
shFile.close();
}
createShellScript ();

Related

how to "batch replace" for a lot of files in emeditor

yes,I know "Batch Replace All",but
my replace list is so big,has 200+ items, i will use it in a big folder which has a lot of files.
now,emediter's behave is that use item1 in all files,then use item2 in all files.....
expected: use item1....item200 in file1,and use item1...item200 in file2
how to do it ?
I attempt use the macros(openfile,replace,closefile),but it is very slowly because open a file need 50ms
emeditor 19.9.1
var fso = new ActiveXObject("Scripting.FileSystemObject");
var files = [];
var filesHasModified = [];
function showFolderFileList(folderspec) {
var f = fso.GetFolder(folderspec);
if(/\\(doc|lib|\.git|\.idea|\.vs|dll)$/gi.test(folderspec)){
return;
}
// recurse subfolders
var subfolders = new Enumerator(f.SubFolders);
for(; !subfolders.atEnd(); subfolders.moveNext()) {
showFolderFileList((subfolders.item()).path);
}
// display all file path names.
var fc = new Enumerator(f.files);
for (; !fc.atEnd(); fc.moveNext()) {
var file = fc.item();
if(/\.(jmx|config|cs|Config|tt|ttinclude|txt|yml|java|bak|xml|cshtml|sh|yaml|js|json|md|properties)$/gi.test(file)){
files.push(file);
}
}
}
showFolderFileList('D:\\Sources.git2');
for( i in files)
{
var file = files[i];
editor.OpenFile(file);
document.selection.Replace("http://172xx","http://offline.esb.xx",eeReplaceAll,0);
document.selection.Replace("http://172xx1","http://offline.esb.xx",eeReplaceAll,0);
document.selection.Replace("http://172xx2","http://offline.esb.xx",eeReplaceAll,0);
//....200 more
if(document.Saved)
{
document.close();
}
else
{
filesHasModified.push(file);
document.Save(file);
document.close();
}
}
editor.NewFile();
document.selection.Text = filesHasModified.join("\r\n");
I will optimize Batch Replace in Files in a future version. Meanwhile, please try (normal) Batch Replace with the Search All Documents in the Group option. This is how you can do:
Update EmEditor to v19.2.2 (or later).
Open EmEditor, select Tools menu - Customize - Tab page, and select None from the When Not Fit drop-down list, and select Fix to specified with from the Width drop-down list.
Open all (or some) files with EmEditor. An easy way is to drag and drop multiple files from Windows Explorer into EmEditor.
In EmEditor, press Ctrl + H to show Replace dialog box, click Batch >>, set the Search All Open Documents in the Group, make sure the batch list is updated, and click the Batch Replace All button.
The (normal) Batch Replace will work as you expect (use item1....item200 in file1, and use item1...item200 in file2, etc.).

How to copy with default copier from cmd?

I need to copy files from one folder to another from cmd and show the progress as in the user interface.
The copy and xcopy commands work but do not show progress and robocopy shows the progress but not the user interface.
Explorer.exe has some parameters, but none that works to copy.
Do you know any way to do what I need?
Thanks in advance...
There is no built-in command that does this, you have to use Windows Scripting Host or a 3rd-party application.
<?xml version="1.0" ?><job><script language="JScript"><![CDATA[
var SHA = new ActiveXObject("Shell.Application");
var FSO = WScript.CreateObject("Scripting.FileSystemObject");
if (WScript.Arguments.length != 2)
{
WScript.Echo("Usage: <SourceSpec> <DestinationPath>");
WScript.Quit(1);
}
var src = FSO.GetAbsolutePathName(WScript.Arguments(0)), dst = FSO.GetAbsolutePathName(WScript.Arguments(1));
var folder = SHA.NameSpace(dst);
var FOF_NOCONFIRMATION=0x0010;
var FOF_NOCONFIRMMKDIR=0x0200;
folder.CopyHere(src, FOF_NOCONFIRMMKDIR);
]]></script></job>
Save as shellcopy.wsf in %WinDir%.
Then in cmd.exe you can do shellcopy *.txt c:\backup etc.

Can I use Basil.js to get images from a URL?

I am a novice user at ExtendScript, Basil.js & Javascript.
After seeing the project http://basiljs.ch/gallery/ending-the-depression-through-amazon/ I wondered if it is possible to simply get the Basil.js Script/ Extendscript/Indesign to grab images from a webpage and insert it directly into a indesign document.
I am a beginner so if someone had a complete script, from directory, functions... with example url, import, save, place.... ?
This is in the hope that I could simply put an address into the script and it would harvest all images on the webpage and put them into a indesign document?
I tried their example http://basiljs.ch/tutorials/getting-images-from-urls/
but when I copy that and run it in extend script it is just searching my computer...
UPDATE*
#includepath "~/Documents/;%USERPROFILE%Documents";
#include "basil/basil.js";
function draw() {
var url = "https://raw.github.com/basiljs/basil.js/master/lib/basil.png";
// download the url to a default location, filename according to url:
// -> "the project folder" + data/download/basil.png
//b.download(url);
// download url to a specific location in the project folder:
// -> "the project folder" + data/download_images_files/basil_logo.png
// b.download(url, "download_images_files/basil_logo.png");
// download url to a specific location e.g. to your desktop
// -> ~/Desktop/basil_logo.png
var newFile = new File("~/Desktop/basil_logo.png");
b.download(url, newFile);
}
b.go();
... It finds in Error in Basil's Core.js #
var runDrawOnce = function() {
app.doScript(function() {
if (typeof glob.draw === 'function') {
glob.draw();
}
}, ScriptLanguage.javascript, undef, UndoModes.ENTIRE_SCRIPT);
};
Console says...
Using basil.js 1.08 ...
sh ~/Documents/basiljs/bundle/lib/download.sh /Users/jamestk/Desktop (Address here, I don't have the sufficient reputation on stack overflow)
Basil.js Error -> b.shellExecute(): Error: sh: /Users/jamestk/Documents/basiljs/bundle/lib/download.sh: No such file or directory
The easiest way would probably be to us the b.shellExecute(cmd) command to run a shell script that downloads your images.
You could use wget from shell to download all images from an url, see this question: How do I use Wget to download all Images into a single Folder
You should make sure then to save the images in the documents data directory. After that, you could get all images by using the Folder object's getFiles() method:
var myImagesArray = Folder('~/path/to/my/folder').getFiles();
Finally you could use that array in a loop to place all the images into your document.

JXA: How to get 2 terminal scripts to run in the same window

Why does this open two different windows and run each script in a different window? Is there a way to tell it to use the same window?
var Terminal = Application('Terminal');
Terminal.activate();
var window = Terminal.windows[0]
Terminal.doScript('ls',window);
Terminal.doScript('cd Desktop',window);
Thanks
You need to put the second parameter inside a "dictionary" using named parameters. This works:
var Terminal = Application('Terminal');
Terminal.activate();
var targetWindow = Terminal.windows[0];
Terminal.doScript('ls', {in: targetWindow});
Terminal.doScript('cd Desktop', {in: targetWindow});
Have fun! Michael / Hamburg

Indesign server data merge

I am trying to do a data merge using Indesign Server. test.indd in the script below already has all the merge fields assigned. I only need this script to open the file, do the merge, and save the merged file. The file that gets saved is the original test.indd file, and not the merged file. I'm not sure how to access the merged file.
var myDocument = app.open(File("/C/inetpub/wwwroot/datamerge/test.indd"));
with (app.dataMergeOptions) {
linkImages = true;
removeBlankLines = false;
createNewDocument = true;
documentSize = 100;
} // (end of dataMergeOptions)
myDocument.dataMergeProperties.mergeRecords();
myDocument.save(new File("/C/inetpub/wwwroot/datamerge/mergedOutput.indd"));
myDocument.close ();
If someone can have a look and let me know what I'm missing. Or direct me in the direction I should be going.
Your dataMergeOptions tell the application to create a new document but you are saving the document in your myDocument variable which is your template. You need to either get a hold of the newly created document and save that or remove the createNewDocument option.

Resources