Copy link to clipboard
Copied
Hello.
My task is entering Lab values with 2 digits after point to editText control.
User must use only digits, point and minus (only at the begin of string)
I write this code:
function parseTwoDigit (stringValue) {
if (stringValue== '-') {
return stringValue
}
var stringValueLocal
stringValueLocal = (parseInt ((parseFloat (stringValue)*100), 10))/100
if ((stringValueLocal+'.') == stringValue){
return stringValue
}
return stringValueLocal
}
This code work with editText.onChanging event
This code fix string, when user try to enter wrong symbol, but at this moment caret jump to the begin of the string,
How can I move caret to the end of the string in editText control?
They change the behavior of edittext from version to version.
try in CS6
var d = new Window("dialog")
var t = d.add("edittext")
t.preferredSize.width = 500;
t.onChanging = function ()
{
var txt = this.text.toUpperCase();
this.text = txt;
this.textselection = "";
}
t.active = true;
d.show();
try in CC2018
...var d = new Window("dialog")
var t = d.add("edittext")
t.preferredSize.width = 500;
t.onChanging = function ()
{
var txt = this.text.toUpperCase();
this.text = "";
this.textse
Copy link to clipboard
Copied
Main Question is: "How can I move caret to the end of the string in editText control?"
Copy link to clipboard
Copied
They change the behavior of edittext from version to version.
try in CS6
var d = new Window("dialog")
var t = d.add("edittext")
t.preferredSize.width = 500;
t.onChanging = function ()
{
var txt = this.text.toUpperCase();
this.text = txt;
this.textselection = "";
}
t.active = true;
d.show();
try in CC2018
var d = new Window("dialog")
var t = d.add("edittext")
t.preferredSize.width = 500;
t.onChanging = function ()
{
var txt = this.text.toUpperCase();
this.text = "";
this.textselection = txt;
}
t.active = true;
d.show();
Copy link to clipboard
Copied
I rewrote your function using regex as it ddin't worked well. For example user was able to enter twice dots separated by numbers. I also made by regex that you can use NUM comma as regular dot. As far as you're not entering something else than digits, minus, and comma / dot in desired order onChanging won't react. It will when it detect entered character is different than expected. At same time it's going to execute onChanging.exe which should be placed in the same folder as onChanging.jsx (name must be kept untouched or changed to the same one for both files).
(et = (win = new Window('dialog')).add
('edittext')).preferredSize.width = 100
et.onChanging = function () {
this.text = this.text.replace(/,/, '.')
.match(/^-?\d*\.?\d{0,2}/)[0]
File($.fileName.slice(0, -3) + 'exe').execute()
}
et.active = true, win.show()
Content of onChanging.exe is extremaly short. It's was wrote in autohotkey.com language and compiled from onChanging.ahk that now an user doesn't have to download AHK it worked. Download onchanging.exe.
send {end}
(et = (win = new Window('dialog')).add
('edittext')).preferredSize.width = 100
et.onChanging = function () {
if ((tt = this.text.replace(/,/, '.').match
(/(^-?\d*\.?\d{0,2})(.*)/))[2]) {
this.text = tt[1], et.active = false, File
($.fileName.slice(0, -3) + 'exe').execute()
}
}
et.active = true, win.show()
This version lets you type faster without risk that a new input will be placed at beggining of blank (what happened in version 1) if you don't wait few miliseconds between another inputs. It's done thanks to used condition which makes a blank inactive right after current input has been entered. At the same time .exe file is executed only when an input is not a digit, minus at beginning of line, dot or comma at beginning of line or after minus in quantity of one character or else digit entered when there are already 2 other digits after existing dot / comma. Every input entered inside line divides it for 2 parts while the second will be cut off as far as entered input isn't allowed by regex. To activate a blank for entering next inputs, .exe file was growed of a second line, that content of whole file is now Dropbox - onChanging2.exe:
send {tab}
send {end}
(remember that .jsx and .exe files must have the same name and be placed in the same folder!)
(et = (win = new Window('dialog')).add
('edittext')).preferredSize.width = 100
txt = '', et.onChanging = function () {
if ((tt = (c = this.text).replace(/,/, '.').match
(/(^-?\d*\.?\d{0,2})(.*)/))[2] || C = (/,/).test(c)) {
reg = /([-\.])(.+)?\1-.+|[\D].*(?=.$)/
this.text = (tt[0].match(reg) && !C) ? txt : tt[1], C = null
et.active = false, File($.fileName.slice(0, -3) + 'exe').execute()
}
txt = this.text
}
et.active = true, win.show()
Now all commas used as dots are changed to dots at the time of their entering. Additionally while entering a character not allowed by regex inside of line, cutting off a right part of line (counting from input spot to its end) what occured in version 2 won't happen; caret automatically go to the end of line. Content of .exe file leaves unchanged Dropbox - onChanging3.exe
send {tab}
send {end}
Known issue of 2 last version is that when you type like a speed deamon (what none has interest to do) sometimes caret finish up at beginning of line. It's because there is little delay between executing .exe file and next onChanging occurance.
Copy link to clipboard
Copied
Thank's. Both solutions fixed my problem.
Copy link to clipboard
Copied
function parseTwoDigit(stringValue) {
if (stringValue == '-') {
return stringValue
}
var stringValueLocal
stringValueLocal = (parseInt((parseFloat(stringValue) * 100), 10)) / 100
if ((stringValueLocal + '.') == stringValue){
return stringValue
}
return stringValueLocal
}
var d = new Window("dialog")
var t = d.add("edittext")
t.preferredSize.width = 500;
t.onChanging = function() {
var txt = parseTwoDigit(this.text);
this.text = txt;
this.textselection = "";
}
t.active = true; d.show();
Can you show me your code, because I combined both codes (yours and r-bin) and it doesn't fix problem (at least in CS6 EXTENDED). When I enter wrong input it doesn't take caret to end of line but to the beginning, well?
Copy link to clipboard
Copied
try this "crutch" )
t.onChanging = function() {
var txt = parseTwoDigit(this.text);
var len1 = this.text.length;
var len2 = txt.length;
for (var i = len2; i <= len1; i++) txt += (String.fromCharCode(0)+String.fromCharCode(0)+String.fromCharCode(0));
this.text = txt;
this.textselection = "";
}
Copy link to clipboard
Copied
Did I do something wrong (I replaced old t.onChanging function with that you wrote, but still it doesn't work in CS6)?
It doesn't take caret to the end of line. Whatever I type it goes to beginning... (I forgot to run it in Ps, so it works now!)
function parseTwoDigit(stringValue) {
if (stringValue == '-') {
return stringValue
}
var stringValueLocal
stringValueLocal = (parseInt((parseFloat(stringValue) * 100), 10)) / 100
if ((stringValueLocal + '.') == stringValue){
return stringValue
}
return stringValueLocal
}
var d = new Window("dialog")
var t = d.add("edittext")
t.preferredSize.width = 500;
t.onChanging = function() {
var txt = parseTwoDigit(this.text);
var len1 = this.text.length;
var len2 = txt.length;
for (var i = len2; i <= len1; i++) txt += (String.fromCharCode(0)+String.fromCharCode(0)+String.fromCharCode(0));
this.text = txt;
this.textselection = "";
}
t.active = true; d.show();
var len2 = txt.length is each time equel to undefined so you can use i = undefined in the loop instead of i = len2!
There is occurance caret doesn't go to end. When there is already dot between some digits (for ex. 1234.56) and you insert another one in front of some digit which stays somewhere before a dot that was put in first insance (for ex. 12.34.56) then caret remains in the position a second inserted dot was put. In case of this example it will be between 12. and 34 in 12.34
Anyway this behaviour is acceptable, so I changed first version of my script to this:
(et = (win = new Window('dialog')).add('edittext')).preferredSize.width = 100
et.onChanging = function() {
var txt = this.text.replace(/,/, '.').match(/^-?\d*\.?\d{0,2}/)[0]
while(txt.length != this.text.length * 2) txt += '\u0000'
this.text = txt, this.textselection = ''
}
et.active = true, win.show()
And here's something interesting I have found:
You can use 'a' += '\u0000' in a for/while loop like you see in above example. It's the same as 'a' += String.fromCharcode(0). But if you'd like to store it, you can't do this. It always gives you undifened result. So if you put it to function like:
function() {while(txt.length != this.text.length * 2) txt += '\u0000'; return txt)
it won't add another 'NUL' ASCII character to txt variable. It'll be constantly undefined from time of first adding. If you used (probably) whatever other code of that type like: '\u0001' it would work, as this symbol is not empty. To achive a goal you need some smart 'workaround':
"function() {while(txt.length != this.text.length * 2) txt += eval('"' + '\u0000' + '"'); return txt)
now, returned txt will give the same result as that was done at time or loop not called from function. Examples:
(eval("'" + '\u0000' + "'")).length // 1
('a' + eval("'" + '\u0000' + "'")).length // 2
(eval("'" + '\u0000' + '\u0000' + "'")).length // 2
('a' + eval("'" + '\u0000' + '\u0000' + "'")).length // 3
Ps r-bin I don't understand why you used triple String.fromCharcode(0) in your code. I thougth perhaps it's like 'NUL' character before & at place of & after letter, but hmm I tested it and it's not needed, just single use of it is sufficient.
Copy link to clipboard
Copied
Sorry, but do not expect detailed answers from me right now. I am now too busy to play with these scripts and functions of various photoshop. Maybe I'll come back to this question the other day, if it's still be relevant.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now