• 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
Re: Change color of selected text in front app's front window
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Subject: Re: Change color of selected text in front app's front window
  • From: Barry Fass-Holmes via AppleScript-Users <email@hidden>
  • Date: Mon, 6 Jan 2025 14:40:13 -0800

Hi Deivy,

Many thanks for your time and posting.

Barry

> On Jan 5, 2025, at 11:45 AM, Deivy Petrescu <email@hidden> wrote:
>
> Hi Barry,
> Your script has some conceptual errors and some color error.
> Not strange given it came from ChatGPT
>
> First,
> "tell application "System Events" to  set frontApp to name of first process
> whose frontmost is true”
>
> Will return the ScriptEditor or whatever app you are using to run the script,
> not the app you want.
> Even if it did return the app you wanted, say TextEdit or Pages, you’d still
> have an issue.
> The way the script is written, it might not compile without having the
> specific name of the app to get its dictionary properly.
> Finally there colors, the way written are all basically black. To get the
> colors you wanted you have to do the following:
>
> 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 * 128, 0}
> else if chosenColor is "Aqua" then
> set colorRGB to {0, 255 * 255, 255 * 255}
> else if chosenColor is "Maraschino" then
> set colorRGB to {255 * 255, 64 * 64, 64 * 64}
> else if chosenColor is "Tangerine" then
> set colorRGB to {255 * 255, 165 * 165, 0}
> end if
>
> Here is what I got:
>
> <script>
> -- 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 * 128, 0}
> else if chosenColor is "Aqua" then
> set colorRGB to {0, 255 * 255, 255 * 255}
> else if chosenColor is "Maraschino" then
> set colorRGB to {255 * 255, 64 * 64, 64 * 64}
> else if chosenColor is "Tangerine" then
> set colorRGB to {255 * 255, 165 * 165, 0}
> end if
>
> set frontApp to choose from list {"TextEdit", "Pages"} with prompt "Select a
> the App for the color change:" default items {"Pages"} without multiple
> selections allowed and empty selection allowed
>
> if frontApp is false then
> display dialog "No app selected. Exiting..." buttons {"OK"} default button
> "OK"
> return
> end if
>
> set frontApp to item 1 of frontApp
> if frontApp is "Pages" then
> tell application "Pages"
> activate
> tell document 1
> try
> tell its body text
> set ct to count its characters
> repeat with j from 1 to ct
> tell its character j
> set its color to colorRGB
> set its font to "Arial"
> set its size to 18
> end tell
> end repeat
> end tell
> on error errMsg
> display dialog "Error: " & errMsg buttons {"OK"} default button "OK"
> end try
> end tell
> end tell
> else
> tell application "TextEdit"
> activate
> tell document 1
> try
> tell its text
> set ct to count its characters
> repeat with j from 1 to ct
> tell its character j
> set its color to colorRGB
> set its font to "Arial"
> set its size to 18
> end tell
> end repeat
> end tell
> on error errMsg
> display dialog "Error: " & errMsg buttons {"OK"} default button "OK"
> end try
> end tell
> end tell
> end if
> </script>
>
>> On Jan 5, 2025, at 2:26 AM, Barry Fass-Holmes via AppleScript-Users
>> <email@hidden> wrote:
>>
>> 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
>
>
>
> Best, be safe.
>
> Deivy Petrescu
> email@hidden
>
>
>
>
>
>

 _______________________________________________
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

References: 
 >Change color of selected text in front app's front window (From: Barry Fass-Holmes via AppleScript-Users <email@hidden>)
 >Re: Change color of selected text in front app's front window (From: Deivy Petrescu via AppleScript-Users <email@hidden>)

  • Prev by Date: Re: Change color of selected text in front app's front window
  • Next by Date: Save/export attachment in Apple Notes
  • Previous by thread: Re: Change color of selected text in front app's front window
  • Next by thread: Save/export attachment in Apple Notes
  • Index(es):
    • Date
    • Thread