I’m trying to create a service that sets the style of selected text to strikethrough.
Further constraints are:
the selected text can be in any application, including those that do not have a native strikethrough style (such as Mail where one needs to open the ⌘T Font dialog to set this style);
this must add the strikethrough style to the selected text style; e.g. if the text is part bold and underline, strikethrough must be added to it;
the service must substitute formatted text to the selected text; in case there’s no selection the service must be a no op;
if the application cannot receive strikethrough text (e.g. through pasting), the service must be a no op.
Being new to AppleScript I would have expected to find tons of examples of the like (possibly with other styles such as double strike or all caps;-), but I couldn’t find any working example. A few examples for setting font size through TextEdit can be found, but 1) I don’t understand why TextEdit would be needed and 2) I couldn’t find how to set font style in general.
Hi. There is no universal style. Text properties can be set in AS aware applications that are able to edit Rich text, such as TextEdit, and each app will have different syntax for accessing its text attributes.
Thanks Marc. I wonder whether the font dialog box could be used though, the logic being
if fontdialog is not opened then open fontdialog and set flag to fontdialog_wasclosed
activate menu item for strkethrough
if fontdialog_wasclosed then close fontdialog
Would that work ?
Any hint about how to test whether font dialog is already open, and how to access its menus (or pointer to doc) ?
Thx!
The following script applies this specific font style (strike through). Even if no text has been selected by the user, the style is applied to subsequent text input. In addition, this script shows how easy it is to get various important characteristics of the text through GUI scripting.
tell application "TextEdit"
activate
tell application "System Events" to tell process "TextEdit"
tell window 1
set Text_Area to text area 1 of scroll area 1
-- FOCUS TEXT AREA
click text area 1 of scroll area 1
-- APPLY "Struck through" STYLE TO SELECTED TEXT
click menu button 1 of group 1
click menu item "Struck through" of menu 1 of menu button 1 of group 1
-- IS TEXT AREA FOCUSED?
set Text_Area_is_Focused to value of attribute "AXFocused" of Text_Area
-- THE SELECTED TEXT OF TEXT AREA
set Selected_Text to value of attribute "AXSelectedText" of Text_Area
-- THE INSERTION POINT LINE NUMBER
set theCursor_Line to value of attribute "AXInsertionPointLineNumber" of Text_Area
-- ENTIRE TEXT OF DOCUMENT
set theContents to value of attribute "AXValue" of Text_Area
-- NUMBER OF CHARACTERS OF ENTIRE TEXT
set Characters_Count to value of attribute "AXNumberOfCharacters" of Text_Area
end tell
end tell
end tell
A while back I developed this crude GUI-based method. Should work across any app that has a “Show Fonts” menubar item that has the OS-wide shortcut key Cmd-T. May have to adjust the “delay” command lengths based on your machine’s GUI responsiveness.
tell application "System Events" to set activeApp to name of first application process whose frontmost is true
tell application "System Events" to keystroke "t" using command down -- show fonts window
delay 0.1
tell application "System Events" to tell process activeApp to tell window "Fonts" to tell toolbar 1 to tell group 3 to click menu button 1
delay 0.2
tell application "System Events"
key code 125
key code 125
keystroke return
keystroke "t" using command down -- hide fonts window
end tell
Would be better to use :
set theBundle to ((path to library folder from system domain as string) & "Frameworks:AppKit.framework:") as «class furl»
set fonts_loc to localized string "Fonts" from table "FontManager" in bundle theBundle
tell application "System Events"
tell (first application process whose frontmost is true)
if not (exists window fonts_loc) then
keystroke "t" using command down -- show fonts window
end if
repeat until exists window fonts_loc
delay 0.1
end repeat
tell window fonts_loc to tell toolbar 1 to tell group 3
tell menu button 1
click it
repeat until exists menu 1
delay 0.1
end repeat
tell menu 1
-- name of menu items --> {"Aucun", "Simple", "Double", missing value, "Couleur"}
click menu item 3
end tell
end tell -- menu button 1
end tell -- window…
keystroke "t" using command down -- hide fonts window
end tell -- process
end tell -- "System Events"
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 19 juin 2020 20:50:50