Skip to main content

Posts

Showing posts with the label Programming

Using C# to run WSL process (python script) on Windows

Every times when Nvidia updates its drivers, the task with Tensorflow is more or least impacted. Quote from TF's website: TensorFlow 2.10 was the last TensorFlow release that supported GPU on native-Windows. Starting with TensorFlow 2.11, you will need to install TensorFlow in WSL2, or install tensorflow or tensorflow-cpu and, optionally, try the TensorFlow-DirectML-Plugin. PyTorch works fine with CUDA. With some modifications on environment variables to use the toolkit 12.1, it is easy to setup for running PyTorch projects on Windows with GPU. To attempt to run TF project on WSL2 with GPU, there comes an idea to create a C# project to run a WSL-python-script process on Windows. Create a C# WinForm project with Visual Studio. Set the project with x64 platform (replace with "Any CPU")! (Otherwise, the running process will be in 32-bit, on which, WSL is not installed.) The code looks like this ( Please refer to the original code ): using (var proc...

A Simple Sample to Make Color List in InDesign

Here is a simple sample to make color list in InDesign. It is very simple, because, there are no left-right page handling, no color values for cyan, magenta, yellow and black, no page numbers, no comments, and no drawing the border of the color-box. And I don't have much time to reply any question. Please try it on your own. Here is the code: main(); function main() { if (app.documents.length == 0) { var myDocument = app.documents.add(); basicSetup(myDocument); myDocument.textFrames.item(0).remove(); makeSwatch(myDocument); } return 0; } function basicSetup(myDocument) { myDocSetup(myDocument); myMasterSpreads(myDocument); } function myDocSetup(myDocument) { with (myDocument.documentPreferences) { pageHeight = "29.8cm"; pageWidth = "21.6cm"; pageOrientation = PageOrientation.portrait; facingPages == true; documentBleedUniformSize = true; documentBleedTopOffset = "3p"; documentSlugUniformSize = true; slugTopOffset = ...

InDesign Scripting with ScriptUI: Add a TestIt Button to Play Around

Based on my previous article " InDesign Scripting with ScriptUI: How to Move Cursor and Select Text ", let's add a button to play around. The original code looks like this: // --- Last Part of UI (No Panel) --------------------------------------------- function AddCloseButton(w) { w.grpButtons = w.add('group'); w.btnClose = w.grpButtons.add('button {text: "Close"}'); w.btnClose.onClick = function () { w.close(); } } // ------------------------------------------------------- Let me add a button to select a piece of text. Below in in the listener function "TestIt", I don't add the boundary checking . You may try and modify the variables "start" and "end" to go over the boundary. And see what happens. // --- Last Part of UI (No Panel) --------------------------------------------- function AddCloseButton(w) { w.grpButtons = w.add('group'); w.grpButtons.orientation = "column"; ...

InDesign Scripting with ScriptUI: How to Move Cursor and Select Text

Based on the code pattern in my  previous article , here I’d like to show the methods to move the cursor and to select text with button click on ScriptUI. Using buttons to move cursor and to select text is useful when working with a script, which has a UI. You don’t need to move your mouse running all over the whole screen very often for doing your tasks. Most of the time, you can concentrate your mouse movement just around the UI of your script. The code is divided into 4 parts: Part 1 , from line 10 to line 28: This is the coding pattern for the modeless dialog. Part 2 , from line 37 to line 57: The main UI is created in this part. Part 3 , three groups of buttons are created here, together with the functions, which are attached to the button click event. Group 1: from line 60 to line 67, the code setups the buttons for moving cursor. from line 69 to line 118, the event listener "MoveCursor" is defined. Group 2: from line 121 to line 128, the code s...

A Programming Pattern to Create a Modeless Dialog with Adobe ScriptUI

I'd like to create a dialog box to input some special characters onto InDesign without running the dialog every time, after I clicked on some button to input characters. A modeless dialog is the choice. But I got many crashes. Finally, with the sample from Adobe, I got a stable programming pattern, which works with Adobe CC. Here is the code: #targetengine session; // the above line is necessary to create a modeless dialog with // var win = new Window("palette", ...); // // refer to the sample program from Adobe: "SnpCreateDialog.jsx". // Which is usually located at the folder (Windows): // C:\Program Files (x86)\Adobe\Adobe ExtendScript Toolkit CC\SDK\Samples\javascript function AModelessDialog() { this.windowRef = null; } function setupWindow() { var ww = new Window("palette", "A Modeless Dialog"); addComponents(ww); return ww; } AModelessDialog.prototype.run = function() { var win = setupWindow(); this.windowRef = win; w...

Try to Add a Language Support to Prism

I try to add a language support to Prism, and I find a way to do so easly. The design of Prism allows a programmer to develop its functionality with the development environment of browsers. It also supports the development with Node.js. I don't have experience with the browser environment. That why I try to use Node.js first. Using Node.js to develop the plugin and language support allows me work with IDE without lost my familiar debugging steps: break points, watchs, call stacks, ... . I use Visual Studio Code as my IDE. Create a folder, Copy prism.js into the folder, (the prism.js is downloaded for the website of Prism) From the menu: File > Open Folder, Now the prism.js is in the Explorer of Visual Studio Code. Add a new javascript source call app.js, with the code: var loadedModule = require('./prism'); var pyCode = 'print \"Done.\"'; // a python code var result = Prism.tokenize(pyCode, Prism.languages.python); var...

Tool to Convert Source Code to HTML Encoded Text in C#

It will be convenient to have a tool converting the source code to HTML encoded text, before I can post it on my blog article. The following C# code is my tool to do the job: public FormMain() { InitializeComponent(); } private void buttonExit_Click(object sender, EventArgs e) { Close(); } private void buttonRun_Click(object sender, EventArgs e) { string src = textBoxSrc.Text; string html = System.Net.WebUtility.HtmlEncode(src); textBoxHTML.Text = html; textBoxHTML.SelectAll(); textBoxHTML.Copy(); } private void textBoxSrc_MouseClick(object sender, MouseEventArgs e) { textBoxSrc.Text = ""; } The UI: Usage: Click on the textBoxSrc, the text in textBoxSrc will be cleaned. (line 26) Copy the source code to the textBoxSrc. Click the buttonRun. The converted text will be displayed on the textBoxHTML, in the same time, the text will also be copied to the clipboard for further use. (line 13 ~ 21) You may use Visual Studio Community to create the p...

Visual Studio + Node.js: Javascript Development

In the beginning, I just planned to install a code-highlighter plugin for my Blog. After the testing on Syntaxhighlighter and Prism, I decided to add a language support of PostScript. The string data of PostScript has "(" and ")" as the deliminator: string begin and string end. According the red book: "A literal text string consists of an arbitrary number of characters enclosed in ( and ). Any characters may appear in the string other than (, ), and \, which must be treated specially. Balanced pairs of parentheses in the string require no special treatment." There are some more lines to describe about \, the "escape". I just leave them in the book. So, the issue is, I need to figure out, how to use regex to retrieve the PostScript strings from the source code to give them a color. I need to run and debug my code together with Prism (maybe Syntaxhighlighter) and to see if I can find some solution. So, I dig into finding a Javascrip...

Flicker free Drawing with WinForm in C#

Just create a C# WinForm Project, Name the form "FormMain" and add a button "buttonRun". Use the code below then you can test it. Main idea is: Draw some image on a bitmap first, then "paste" the bitmap on the graphics of the form. This will reduce the "flicking" of the window. Code: ================================== public partial class FormMain : Form { bool isDrawing = false; Bitmap gBmp = null; Graphics gGraphImg = null; // this variables are just for the Curve int gX = 350, gY = 200; int offX = 180, offY = 100; public FormMain() { InitializeComponent(); CreateBitmapAndGraphic(); } // gBmp & gGraph will be created, when the form is initialized or resized. private void CreateBitmapAndGraphic() { if (gBmp != null) { gBmp.Dispose(); // Dispose it to prevent memory usage too high, when redraw too many times. } gBmp =...