Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Script based on font size

Community Beginner ,
Mar 04, 2016 Mar 04, 2016

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

TOPICS
Actions and scripting
1.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Mar 07, 2016 Mar 07, 2016

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{}
Translate
Adobe
Community Expert ,
Mar 04, 2016 Mar 04, 2016

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.

Capture.jpg

JJMack
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 04, 2016 Mar 04, 2016

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 04, 2016 Mar 04, 2016

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

https://forums.adobe.com/search.jspa?place=%2Fplaces%2F1383833&sort=updatedDesc&q=Get+Text+Layer+fon...

JJMack
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 04, 2016 Mar 04, 2016

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 07, 2016 Mar 07, 2016

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 07, 2016 Mar 07, 2016

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{}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 10, 2016 Mar 10, 2016

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?

Screen Shot 2016-03-10 at 11.55.11 AM.png

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 10, 2016 Mar 10, 2016

In your example, you didn't define textSize. You need add a line:

var textSize = lay.textItem.size.

(line 3 in my above code.)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Mar 10, 2016 Mar 10, 2016

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 10, 2016 Mar 10, 2016
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 10, 2016 Mar 10, 2016

Ah, got it. I knew it had to something simple I was forgetting.

Thanks. That worked!

Screen Shot 2016-03-10 at 1.01.16 PM.png

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines