Copy link to clipboard
Copied
Trying to figure out if I can set up a conditional script based on the font size of a type layer.
For example if the font is 10 pt then _________ , 12 pt then ______ , 14 pt then_________.
Is this doable with scripting?
Thanks,
Tim
If you use several if else statements, you can do it. You can either have a range with each if statement or a cascading range.
var doc = activeDocument;
var dL = doc.activelayer;
var textSize = dL.textItem.size;
//using range if:
if(textSize >=119.8 && textSize <= 120.2){
//code here
}
//cascading if else
if(textSize<100){
//code here
}
if else(textSize<119.8){
//more code
}
if else(textSize=<120.2){
//This captures the range you want 119.8 ~ 120.2
else{}
Copy link to clipboard
Copied
Are you confusing Conditional Actions with Photoshop Scripting. Scripting is programming of course there is an "if" statement. Many things can be tested. Photoshop's Conditional Action feature supports a small set of conditions. None that deal with fonts or text layers.
Copy link to clipboard
Copied
Nope, not getting them confused. I am aware that the conditional Actions are extremely limited in what conditions one can apply.
I know if it is doable it will need to be achieved with JavaScript or some other scripting. Is font size one of those testable characteristics?
Copy link to clipboard
Copied
You can Process layers and test the type. You can also retrieve information about text layer. I never have but there have been threads in the forum on that subject.
Re: Need PS script, which will copy properties of selected text
Copy link to clipboard
Copied
Yes, you can do something like that:
#target photoshop
var doc = activeDocument
var lay = doc.activeLayer
if(parseInt(lay.textItem.size)<=10){//code here
}
else if(parseInt(lay.textItem.size)<=12){//more code here
}
else if(parseInt(lay.textItem.size)<=14){//more code here
}
else{//final code here
}
Just be aware the text may not be what you think it is, or there might be different sizes of text.
Copy link to clipboard
Copied
Thanks, Chuck. This is helpful.
I was actually wondering about this discrepancy in font size. Being that we do a lot of development for mobile apps across iOS and Android we use pixel measurements for text instead of points. It is almost a 1:1 ratio in size, but often have results as you have shown. As in your example, is it possible to select a range from 119.8 to 120.2?
Copy link to clipboard
Copied
If you use several if else statements, you can do it. You can either have a range with each if statement or a cascading range.
var doc = activeDocument;
var dL = doc.activelayer;
var textSize = dL.textItem.size;
//using range if:
if(textSize >=119.8 && textSize <= 120.2){
//code here
}
//cascading if else
if(textSize<100){
//code here
}
if else(textSize<119.8){
//more code
}
if else(textSize=<120.2){
//This captures the range you want 119.8 ~ 120.2
else{}
Copy link to clipboard
Copied
Hey, Chuck. Thanks again. I've got pretty much all the coding done for what I want to do after I determine the text size, but I just can't get this to work.
Tried switching it up quite a bit (fontSize vs. textSize), ( > vs. >= ), just nothing I do here seems to work. My script works until I enter in the "if".
Any ideas?
Copy link to clipboard
Copied
In your example, you didn't define textSize. You need add a line:
var textSize = lay.textItem.size.
(line 3 in my above code.)
Copy link to clipboard
Copied
It will depend on what version of Photoshop the script will be run on, textItem.size will only work with CS6 if the text has not been transformed. I am not sure if this has been fixed in CC?
Copy link to clipboard
Copied
It's working with 2014, which is what I have on my computer now. It won't show the transform value, as you mentioned, just the original text value.
Copy link to clipboard
Copied
Ah, got it. I knew it had to something simple I was forgetting.
Thanks. That worked!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now