Saturday, October 31, 2020

#1 2007-04-14 04:09:28 pm

rogbar
Member
Registered: 2007-04-13
Posts: 6

Script to make certain font changes

If I select some text in most any kind of document and want to change the font, I have to right-click on the text, select either Show Fonts or Show Colors and then make my choices. (Except, of course, for Bold or Italic, which have simple keystrokes - Cmd-B or Cmd-I).

How would I write an AppleScript to turn the selected text from whatever it is to Arial 12 point ... and a script to turn the selected text, which is in color, to plain black?

Model: MBP 15"
AppleScript: 1.10.7
Browser: Firefox 2.0.0.3
Operating System: Mac OS X (10.4)

Offline

 

#2 2007-04-14 08:48:21 pm

James Nierodzik
Member
From:: Chicago
Registered: 2005-09-04
Posts: 1151

Re: Script to make certain font changes

Well what app(s) specifically are you trying to do this in?  There could be easier ways then GUI scripting (which is what your descirbing) depending on the application in question.


Formerly known as myndcraft
(S+C) x (B+F)/T = V

Offline

 

#3 2007-04-15 02:21:24 pm

rogbar
Member
Registered: 2007-04-13
Posts: 6

Re: Script to make certain font changes

Pretty much everything, but right now the main ones are OmniOutliner, Microsoft Word, DevonThink Pro, Apple Mail and TextEdit.

Offline

 

#4 2007-04-15 03:51:30 pm

pidge1
Member
From:: Leeds-England-UK
Registered: 2005-08-06
Posts: 627

Re: Script to make certain font changes

Hi

This is a textedit script which does most of the things you want.

Applescript:

tell application "TextEdit"
   set D to document 1
   set C to (count of paragraphs of text of D)
   repeat with i from 1 to C
       set the color of word 1 of paragraph i of text of D to {0, 0, 0}--black
       set size of paragraph i of text of D to 12
       set font of paragraph i of text of D to "Arial"
   end repeat
end tell

you can alter it to suit your needs or see if any thing in there works in other apps


pidge


Filed under: TextEdit

Offline

 

#5 2007-04-15 06:14:10 pm

rogbar
Member
Registered: 2007-04-13
Posts: 6

Re: Script to make certain font changes

Hi Pidge, and thanks for the quick response.

One question: how would I rewrite this script to apply to selected text only?  In other words, I'd highlight the text I wanted changed, and then run a script to change font or color for just that text, not the whole document.

Thanks very much.

/rb

Offline

 

#6 2007-04-16 07:57:29 am

pidge1
Member
From:: Leeds-England-UK
Registered: 2005-08-06
Posts: 627

Re: Script to make certain font changes

Hi Rog

Not sure it can be done by selection (in textedit anyway)
will there be anything unique about the line of text you wish to change, you may have to reference it
by word, paragraph, character etc…


pidge

Offline

 

#7 2007-04-16 09:56:40 pm

kai
Member
From:: Brighton, UK
Registered: 2005-05-28
Posts: 912

Re: Script to make certain font changes

Unfortunately, there's no universal method of achieving this kind of thing. The environment, scripting support and GUI structure of each application can vary significantly - so each approach (if a viable one exists) will probably be quite different.

On the specific question of performing such an operation in TextEdit, the solution isn't that obvious. However, with a little juggling between the application itself and System Events, it is possible...

To change the font and size of the selection:

Applescript:

tell application "System Events" to set {b, e} to value of attribute "AXSelectedTextRange" of ¬
   text area 1 of scroll area 1 of front window of application process "TextEdit"
if e ≥ b then tell application "TextEdit" to tell front document's characters b thru e to set {font, size} to {"Arial", 12}

Similarly, to change the selection's color:

Applescript:

tell application "System Events" to set {b, e} to value of attribute "AXSelectedTextRange" of ¬
   text area 1 of scroll area 1 of front window of application process "TextEdit"
if e ≥ b then tell application "TextEdit" to set color of front document's characters b thru e to {0, 0, 0}


kai


Filed under: System, TextEdit

Offline

 

#8 2007-04-16 11:52:48 pm

rogbar
Member
Registered: 2007-04-13
Posts: 6

Re: Script to make certain font changes

Thank you very much.  I'll give that a try.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)