• 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: Deivy Petrescu via AppleScript-Users <email@hidden>
  • Date: Sun, 5 Jan 2025 16:45:39 -0300
  • Ui-outboundreport: notjunk:1;M01:P0:KN0IWn1P6K4=;d2vBQ/pMzIJwCZIXSNSfvvqhboH Dbh5w5LnZvwBprpmB8/qVbwMkVf7gx+ZmQBPcX3SwDoNavGZ4uOTefE8fykst9EDgGYZm8a9d TGGjrrwQ3Gk8N86GS3VNni0qIAnxNfCYfYvbcyk/0rlm2OwaaDLmpinWGkjJCxCsFV8MvnLXJ qwrFUbpY9HDTlYmLzTBgdzgg8Khm1u/UU5LnCtXqS8bbO+QRI1vIVSghiGdUbWAeOGCmpOnK/ Vew0qaapRzoVRROyopMTOX7C+j4OKXy2emkNFYhnKnlGBCeWcy0LZ18HNbeEw+tBz9Lzm3WdT rtr1eKFe43W5P1H6FJkOsEs3l1Awp9Rxu+YZvYJBGBDJP7DeYa5djNAv+sbbrEnPVD2nPhSDb ZK2aPLeE16B4BSwhX0H9wV1m5xkIkTeUaGXfPD8MkDWUtJWxZRZUr4kLZt1oaTKYIpC5dGbfY fW20HI8WGKVvmWh97Tlnejbu5fUpuGw9uW5aLkzW0s2oT7XbcSEcVGIF06Eic1jeNs2C6PbfN xsFifTShl3aaoO2E113u6VCUak7hmQZ7eX7BHVaw5lawwBiKKRKHKfPcVBm0+GnYkMFHQNakb kzfB1PKYpUX0JC8zUTJLfP4JG/4YFomzA8UMMfXNF686YX4txZZXpabSiq6MLJLchVMjiPJUi IOpT8DivGyT7+BfsTEGr8bo1suNN2FTOmchckaFVS02dlSQ/vyex0FMwYf/4fVVY/2VVqnPcQ dFwNikfOo2bJpGhc4zit724xAjydyMpLtt64LkhDQMSGkgqY7c4BzmAdAxm36Bi2Mnd9rTswQ OHgFaQJ9e8goN7K0hZ32E1nUWPNYIOMkGIutkIe40A+DAaPjv3sULdAP8STrRR5CJiw/kDJ8e Dhw2DtcK46AtSseD7mfuVoSn4ItAQ8clQOkptWPVNFB61THXy11j8DTzoY+9fgGusJvX2M2Ar cb2MR0xF6HYAWBDDbiehhzMcnxJY5wnp9rujE3Rwaq2gWvGvsYiHHvRUinapylSZ2/D/szrXM wJrLv/E6DBWCVV0vInE+eofrwuRtQFs+DrR1vAT

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

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

  • Prev by Date: Change color of selected text in front app's front window
  • Next by Date: Re: Change color of selected text in front app's front window
  • Previous by thread: 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