Change font name, size, color & attributes of multiple text

Moderator: Kathy_9

Post Reply
dejudicibus
Posts: 44
Joined: Thu Mar 03, 2011 7:07 pm
operating_system: Windows 7 Professional
System_Drive: C
32bit or 64bit: 64 Bit
Contact:

Change font name, size, color & attributes of multiple text

Post by dejudicibus »

I have the following problem: I have to set the font name, size, color, and attributes (bold, italic, underscore) of many texts at the same time. This is not possible by using the interface, so I have to do it by a script. I wrote a script that get the font size but there is a problem: text is duplicated, even if I set the txt['TextTarget'] to item['Path']. I do not understand why. Second, I am not able to extend it to get also font name, color, and attributes, possibly by selecting the font, not by multiple requests. Any help appreciated. PS Here is the code from def Do(Environment) statement (of course I have also import and all the other stuff on top of file).

Code: Select all

def Do(Environment):
	# EnableOptimizedScriptUndo
	App.Do( Environment, 'EnableOptimizedScriptUndo', {
			'GeneralSettings': {
				'ExecutionMode': App.Constants.ExecutionMode.Default, 
				'AutoActionMode': App.Constants.AutoActionMode.Match, 
				'Version': ((16,0,0),1)
				}
			})

	# Clear script output window
	App.Do( Environment, 'ScriptWndClear')
	
	# Get list of selected vector objects
	SelectedText = App.Do( Environment, 'ReturnVectorObjectProperties' )
	
	if SelectedText['ListOfObjects'] == None:
		print 'No text is selected...'
		return

	# Ask user for font size
	UserSize = App.Do( Environment, 'GetNumber', {
		'DefaultValue': 12,
		'MinValue': 6,
		'MaxValue': 90,
		'DialogTitle': 'Font Size',
		'GetInteger': App.Constants.Boolean.true,
		'LogarithmicSlider': App.Constants.Boolean.false,
		'Prompt': 'Enter font point size (6 to 90)',
		'GeneralSettings': {'ExecutionMode': App.Constants.ExecutionMode.Interactive}})

	if UserSize['OKButton']:
 
		# loop through selected text list
		for item in SelectedText['ListOfObjects']:

			# read the text data
			txt = item['TextExData']

			# only text data allowed
			if txt == None:
				continue

			# change the font point size
			txt['PointSize'] = UserSize['EnteredNumber']
			txt['Font'] = 'Lato Medium'
			txt['AntialiasStyle'] = App.Constants.AntialiasEx.Smooth
			#Set text path to object we're changing
			txt['TextTarget'] = item['Path']  
			txt['PathTarget'] = None 

			# update the text
			App.Do( Environment, 'TextEx', txt )
    
	print 'Done...'
Dario de Judicibus
Site- Blog- Book
LeviFiction
Advisor
Posts: 6831
Joined: Thu Oct 02, 2008 1:07 pm
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Alienware M17xR4
processor: Intel Core i7-3630QM CPU - 2_40GH
ram: 6 GB
Video Card: NVIDIA GeForce GTX 660M
sound_card: Sound Blaster Recon3Di
Hard_Drive_Capacity: 500GB
Corel programs: PSP: 8-2023
Location: USA

Re: Change font name, size, color & attributes of multiple t

Post by LeviFiction »

This really belongs in the scripting section.

Anyway, the reason that using item['Path'] doesn't work is because 'TextTarget' is a relative path. So it's a list of how to get to the text from your current location. I realize that's stupid and makes absolutely no sense. But it is what it is.

So, what you have to do is first, select the bottom layer since all layer and object paths are relative to the bottom layer. Then you have to select the layer where your text object is a child. Finally you set the path 'TextTarget" path to the child that represents the text.

This seemed to work well for me. I even used Layer Groups and vector groups and it seemed to function just fine.

So the first change I made is at the start of each new item in the loop I select the bottom most layer using the path (9999, -9999, [], False). After we prove that the object is a text object I use the first two path parameters to select the layer where the text object exists. Path = (item['Path'][0], item['Path'][1], [], False)

Then I set the item['TextTarget'] = (0,0, item['Path'][2], True)

This will traverse only the children of the currently selected layer. This includes entering layer groups, and vector groups.

These three changes are all you need to get this script to work as you like. This script could also make a neat "CopyFormatting" option. Where the first object in the selection is the template and all other text objects selected get changed to match its parameters. Excellent script idea.

Code: Select all

def Do(Environment):
   # EnableOptimizedScriptUndo
   App.Do( Environment, 'EnableOptimizedScriptUndo', {
         'GeneralSettings': {
            'ExecutionMode': App.Constants.ExecutionMode.Default, 
            'AutoActionMode': App.Constants.AutoActionMode.Match, 
            'Version': ((16,0,0),1)
            }
         })

   # Clear script output window
   App.Do( Environment, 'ScriptWndClear')
   
   # Get list of selected vector objects
   SelectedText = App.Do( Environment, 'ReturnVectorObjectProperties' )
   
   if SelectedText['ListOfObjects'] == None:
      print 'No text is selected...'
      return

   # Ask user for font size
   UserSize = App.Do( Environment, 'GetNumber', {
      'DefaultValue': 12,
      'MinValue': 6,
      'MaxValue': 90,
      'DialogTitle': 'Font Size',
      'GetInteger': App.Constants.Boolean.true,
      'LogarithmicSlider': App.Constants.Boolean.false,
      'Prompt': 'Enter font point size (6 to 90)',
      'GeneralSettings': {'ExecutionMode': App.Constants.ExecutionMode.Interactive}})

   if UserSize['OKButton']:
 
      # loop through selected text list
      for item in SelectedText['ListOfObjects']:
         App.Do( Environment, 'SelectLayer', {'Path': (9999, -9999, [], False)})
         # read the text data
         txt = item['TextExData']

         # only text data allowed
         if txt == None:
            continue
         App.Do( Environment, 'SelectLayer', {'Path': (item['Path'][0],item['Path'][1], [], False)})
         # change the font point size
         txt['PointSize'] = UserSize['EnteredNumber']
         txt['Font'] = 'Lato Medium'
         txt['AntialiasStyle'] = App.Constants.AntialiasEx.Smooth
         #Set text path to object we're changing
         txt['TextTarget'] = (0, 0, item['Path'][2], True)  
         txt['PathTarget'] = None 

         # update the text
         App.Do( Environment, 'TextEx', txt )
    
   print 'Done...'
https://levifiction.wordpress.com/
dejudicibus
Posts: 44
Joined: Thu Mar 03, 2011 7:07 pm
operating_system: Windows 7 Professional
System_Drive: C
32bit or 64bit: 64 Bit
Contact:

Re: Change font name, size, color & attributes of multiple t

Post by dejudicibus »

It works! Thank you. Working with relative paths is really a mess. You are great.
Dario de Judicibus
Site- Blog- Book
Post Reply