• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Change color of selected text in front app's front window
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Change color of selected text in front app's front window


  • Subject: Change color of selected text in front app's front window
  • From: Barry Fass-Holmes via AppleScript-Users <email@hidden>
  • Date: Sat, 4 Jan 2025 21:26:01 -0800

Greetings!

ChatGPT produced the script below in response to my request for a script that
changes the color of selected text in the front most window of the front most
program; the color is selected by the user from a dialog that includes black,
clover, aqua, maraschino, and tangerine.

The response indicated that the generated script assumes the front-most
application is one that supports changing text color through AppleScript, like
TextEdit, Pages, or similar apps.

The script displayed an error message when I tested it with TextEdit and Pages;
the former’s was "Error: TextEdit got an error: Can't get selection of document
1.” The latter’s was "This app doesn't support text color changes through
AppleScript.”

Would you have expected these error messages to display if the script was
correct?

If not, and if the script has flaw(s), could you please tell me how to fix
it(them)?

Many thanks in advance!

Barry

-- Display a dialog for the user to choose a color
set colorChoice to choose from list {"Black", "Clover", "Aqua", "Maraschino",
"Tangerine"} with prompt "Select a color for the selected text:" default items
{"Black"} without multiple selections allowed and empty selection allowed

-- If the user canceled the dialog, exit the script
if colorChoice is false then
    display dialog "No color selected. Exiting..." buttons {"OK"} default
button "OK"
    return
end if

-- Get the chosen color
set chosenColor to item 1 of colorChoice

-- Define RGB color values for each color
set colorRGB to {}
if chosenColor is "Black" then
    set colorRGB to {0, 0, 0}
else if chosenColor is "Clover" then
    set colorRGB to {0, 128, 0}
else if chosenColor is "Aqua" then
    set colorRGB to {0, 255, 255}
else if chosenColor is "Maraschino" then
    set colorRGB to {255, 64, 64}
else if chosenColor is "Tangerine" then
    set colorRGB to {255, 165, 0}
end if

-- Get the front-most application
tell application "System Events"
    set frontApp to name of first process whose frontmost is true
end tell

-- Apply the color change to the selected text in the front-most application
tell application frontApp
    activate
    try
        -- Example for TextEdit: change the color of selected text
        -- Modify this part based on the front app's scripting capabilities
        if frontApp is "TextEdit" then
            set selectedText to the selection of document 1
            set text color of selectedText to colorRGB
        else
            display dialog "This app doesn't support text color changes through
AppleScript." buttons {"OK"} default button "OK"
        end if
    on error errMsg
        display dialog "Error: " & errMsg buttons {"OK"} default button "OK"
    end try
end tell
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: Change color of selected text in front app's front window
      • From: Deivy Petrescu via AppleScript-Users <email@hidden>
  • Next by Date: Re: Change color of selected text in front app's front window
  • Next by thread: Re: Change color of selected text in front app's front window
  • Index(es):
    • Date
    • Thread