Recently quite confused, always feel not low, look at the source code or books when some anxious, well, maybe to the bottleneck… First a static, recall at the beginning of learning programming, one of the starting point is: want to play with the script PhotoShop, over time actually forgot some mainstream language now are almost contact, object-oriented understanding is also very deep, see PS, in thinking: change the mood!
The environment
Scripting language: JavaScript IDE environment: Idea (optional, Notepad is also available) PS version: 18.0.0Copy the code
1. Use of scripts
1. HelloWolrd start
---->[Hello.js]-----------------
Alert("Hello World");
Copy the code
2. Customize shortcut keys
2. PhotoShop from an object-oriented perspective
0. Object: app
Look at the whole app as an object, and it has a bunch of methods and properties that you can use to get information and manipulate images. So let’s look at a couple of objects in the app object, just to get a feel for it
App {// the entire PS software object name,// the software name version,// documents:{},// the software version of documents:{},// the information of the opened file, fonts:{},// the system font recentFiles:{},// all open document objects (array) ActiveDocument :{}// activeDocument object}Copy the code
1. Obtain name and version number + Operation prompt tone:app.beep()
---->[info.js]-----------------
var msg = app.name + "The \ r \ n version."+ app.version; alert(msg); App.beep () // Play the prompt tone effect: ding dongCopy the code
2. Information about the opened file:app.documents
var docs = app.documents;
var msg = "";
for (var i = 0; i < docs.length; i++) {
msg +=
"Name:" + docs[i].name + "\r\n" +
Width/pixel: + docs[i].width + "\r\n" +
"High/pixel :" + docs[i].height + "\r\n";
}
alert(msg);
Copy the code
3. List fonts:app.fonts
var fonts = app.fonts;
var msg = "";
for (var i = 0; i < fonts.length; i++) {
msg += "The serial number." + (i + 1) + " 字体:" + fonts[i].name + "\r\n";
}
alert(msg);
Copy the code
4. Recently opened files:app.recentFiles
var cFiles = app.recentFiles;
var msg = "";
for (var i = 0; i < cFiles.length; i++) {
msg += (i + 1) + ":" + cFiles[i].name + "\r\n";
}
alert(msg);
Copy the code
5. Currently active document objects:app.activeDocument
var doc = app.activeDocument;
msg = "Name:" + doc.name + "\r\n" +
Width/pixel: + doc.width + "\r\n" +
"High/pixel :" + doc.height + "\r\n";
alert(msg);
Copy the code
3. File operation related
1. Open image :” Open (File)”
var img = File("J:\\Java\\Android\\TolyGithub\\TolyTest\\toly_test\\src\\main\\res\\mipmap-xxhdpi\\bg_10.jpg");
var ok = confirm("Open the picture?");
if (ok) {
open(img)
}
Copy the code
2. Open all the pictures in the folder
var imgFolder = File("J:\\Java\\Android\\TolyGithub\\TolyTest\\toly_test\\src\\main\\res\\mipmap-xxhdpi");
var files = imgFolder.getFiles();
for (var i = 0; i <files.length; i++) {
var file = files[i];
if(file instanceof File) { open(file); }}Copy the code
3. Copy the current document and open it
var doc = app.activeDocument; // Current file object doc.duplicate(app.activeDocument.name +)"-copy", 1); // Copy the current document and open itCopy the code
4. Save files as:app.activeDocument.saveAs
var doc = app.activeDocument; Var outPath = new File()G: \ \ "Photo \ \ little dragon. PNG"); var options = PNGSaveOptions; // Save the PNG mode. Var asCopy =true; Var extensionType = extension.lowercase; SaveAs (outPath, options, asCopy, extensionType); alert("Save done")
Copy the code
5. Compress and Save images (web)
The direct saved PNG is 11.1m and the Web export is 0.3k, which is quite valuable, though a bit of a paltry effect.
Var doc = app.activeDocument; Var path = new File()G: \ \ "Photo \ \ little dragon. GIF"); var eop = new ExportOptionsSaveForWeb(); // Transparecy = is used in more than 620 countries in web image exportfalse; // Whether transparency is normal eop.includeprofile =true; Lossy = 0; // Check whether the built-in color profile is included. Eop. colors = 256; / / color quantity eop. ColorReduction = ColorReductionType. SELECTIVE; / / / / color depth reduction algorithm: can choose eop. Formate = SaveDocumentType.COM PUSERVEGIF; // Export format FIF ep. ditherAmount = 0; Eop. dither = dither.noise; // Palette = palette.LOCALADAPTIVE; ExportDocument (path, exportType.saveForWeb, eop); alert("Save complete :"+ path); Var doc = app.activeDocument; Var filePath = new File()G: \ \ "Photo \ \ little dragon. Jpeg." "); var eop = new ExportOptionsSaveForWeb(); // Eop. quality = 60; // Image quality doc.exportDocument(filePath, exportType.saveForWeb, eop); alert("Save complete :"+ filePath); Var doc = app.activeDocument; Var filePath = new File()"G: \ \ Photo \ \ dragon less - compressed PNG"); var eop = new ExportOptionsSaveForWeb(); // Web image export configuration eop.png8 =true; // PNG 8 color doc. ExportDocument (filePath, exportType.saveForWeb, eop); alert("Save complete :" + filePath);
Copy the code
Four, some simple operations
1. Create a new file
Var width = 1080; Var height = 1920; / / var high resolution = 72; // Resolution var fileName="phone"; Var mode = newDocumentMode.rgb; Var fillColor = documentfill.transparent; Var pixelAspectRatio=1; App.documents. Add (width, height, resolution, fileName, mode, fillColor, pixelAspectRatio);Copy the code
2. Add the text layer
Var doc = app.documents. Add (500, 200); var artLayer = doc.artLayers.add(); artLayer.kind = LayerKind.TEXT; var textItem = artLayer.textItem; // Text entry textitem.contents ="Zhang Feng Jie Te Li"; // Text content textitem. size = 40; artLayer.translate(0, 100);Copy the code
3. Get all layers of the current file
Var / / access layer object the layers = app. The activeDocument. ArtLayers; var msg ="";
for (var i = 0; i < layers.length; i++) {
var layer = layers[i];
msg += i + "-- Layer name: + layer.name + "\r\n";
}
alert(msg);
Copy the code
4. Get the layer size
var layer = app.activeDocument.activeLayer; // Active layer alert(layer.bounds);Copy the code
5. Currently active Layer: Color Balance
var layer = app.activeDocument.activeLayer; / / activity layer layer. AdjustColorBalance ([- 39-81, 0], [52] - 44, 50 -, 0, 0 and 3,true); // Shadows, midtones, highlightsCopy the code
6. Foreground and background color modification
var ok = confirm("Do I set the foreground and background colors randomly?");
if(ok) {/ / the foreground app. ForegroundColor. RGB, red = math.h random () * 255; app.foregroundColor.rgb.green = Math.random() * 255; app.foregroundColor.rgb.blue = Math.random() * 255; / / background color app. BackgroundColor. RGB, red = math.h random () * 255; app.backgroundColor.rgb.green = Math.random() * 255; app.backgroundColor.rgb.blue = Math.random() * 255; }Copy the code
That’s all we need to do for this introduction. We just need to realize that Photoshop itself is an app object, just like the browser window object.
Start scattered off — — — — — — — — — — — — — — — — — — — — — — — the people come in, please — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
1. What is the code?
I don’t know if anyone has really seriously considered this problem, or like me, up is to use Java to follow the knock HelloWorld and output HelloWorld, the teacher said proudly: see, this is your first line of code. And then all the way to API…
This question to a programmer is like asking what is breathing. What is breathing? | - breathing, is refers to the process of gas exchange between the body and the environment What is the code? | - code is programmers write with development tools support the language source files, | - by character, symbol, or is a set of signal element in a discrete form clear rules of the said information system code is two words, of course also translated (code), I think the translation was just right Generation: Substitution, which means it's not a real number: the character in China is a measuring tool, it's a symbol for numbers and the whole process is like a Chinese person in a German restaurant, saying in Chinese:"Give me a glass of water, please."", which was then translated by the air particles, changed the way the air molecules vibrated, and transmitted to the German ears:"Geben Sie mir ein Glas Wasser."Then the Germans give you a glass of water. A Chinese doesn't need to know German, he can give orders to be carried out. The code is:"Give me a glass of water, please.", instead of:"Geben Sie mir ein Glas Wasser."
"Air Translation Particle"This is the locale in which the code is produced."IDE"[1]. Code is not for computers to see, code is for people to see [2]. The computer/virtual machine can recognize and run the compiled binary code [3]. The execution function of the program requires proper compilationCopy the code
2. Why code
This question is as meaningless to a programmer as asking why we breathe. Why do people breathe? | - A: don't breathe to suppress dead bai | - B: respiration can make sugar, lipid and protein in the oxidative decomposition of organic matter, such as to produce ATP, provide energy life activities need | - C: Respiration: First stage C6H12O6(glucose)=4[[H]{(reduced hydrogen)+ 2C3H403 (pyruvate)+2ATP Second stage 2C3H4O3 (pyruvate)+ 6H2O (water)= ==20[H] (reduced hydrogen)+ 6CO2(carbon dioxide)+2ATP Third stage 24 [H] (hydrogen reduction) o2 (oxygen) = 12 + 6 h2o (water) + A lot of energy and atp total reactive C6H12O6 + 6 h2o + o2 - - - > 6 h2o + co2 + 12 (38 atp) code is A lot of energy? | - A: Don't write code was starved to death | -- B: write code can make idea, architecture, function and so on in the brain thinking reality into products, have the money, providing life activities need materials | - C: Write code: the first stage: the strip gradually end not regret, for the yi eliminate people gaunt = hundreds of abuse + as the first love the second stage: alone on the high-rise, look off the horizon road the second stage: Code is a means to let the computer execute the will of the person. In fact, you can also not write code, you can directly hit 0101, just like that Chinese person directly said:"Geben Sie mir ein Glas Wasser."In order to better enable the computer to implement the will of the human, we start from the solution of the problem by: Lines of the execution of instructions to complete the task to (processes) to the problem of abstract into class, let the interaction between objects to accomplish tasks (object-oriented) this is equivalent to a plank brick, then can build architecture, building a house, the house also has its own style But also the internal operating mechanism is reasonable, then large programs not only can run smoothly, It also extends new functionality to large software like PhotoShop, which provides a GUI interface, that is, code -- codeless designers don't need to be able to type code to use it, and their aesthetic and design expertise is more compatible with PhotoShop: Code --> compiler --> binary --> the computer relies on the compiler to produce binary files, the computer can execute the code code it does not know --> computer --> graphical interface --> the designer relies on the computer to produce the graphical interface, The designer can execute code it doesn't know code --> computer --> PhotoShop object --> script relies on computer to produce PhotoShop object, script can execute code it doesn't knowCopy the code
PS has a very good GUI interface, and I don't think the script is better than the designer when it comes to design. The advantage of code is efficient, not afraid of tiring, accurate, careful, low cost, grasp these points, the script should still have a place to useCopy the code
IN general, the problem we need to deal with is called IN data, and the result is called OUT data after processing. The ultimate purpose of data code is to solve the problem (transform data), and the vast majority of it is finally implemented to people’s senses, that is, OUT data is consumed, thus generating value
IN ancient times, the artist will be an ugly woman (IN data) into a beauty (OUT data), the customer saw very satisfied, complete the work now PS will be an ugly woman (IN data) into a beauty (OUT data), the customer saw very satisfied, complete the work. The essential difference between the two lies in the difference in the way of solving problems, that is, the handling method of data. Code efficiency, practical, easy to use, accurate, meticulous, low cost decided its competitiveness is unmatched. But at the same time, its obscurity leaves the masses in the dust
Why write code? — You want to do it without writing code? Want to fly? Want to stand by the sun? Can you still…. What a load of nonsense, we’ll have to read it, and stay tuned for the next official article that will cover the API in more detail (or maybe not)…