aquilafedele

Q: Applescript to set alternate rows background color Numbers

Hi!

 

I have just a little experience with AppleScript but I made some working scripts for iTunes.

 

Now I am working with Numbers. Periodically I want to parse some csv files into a new sheet of a Numbers document I already have. I already achieved the creation of all the data and tables that I need and now I am working on the format of the tables, things like dimensions, colors and so on.

 

I would like to activate the "Alternate row color" and then set the color of the secondary row using the script.

 

I now that I can achieve this assigning different background color based on the even or odd rows, but I would like to use the tools provided by the program, if possible.

 

The actual code is below:

----------------------------------------------------

 

-- Open a random numbers document and run the following:

tell application "Numbers"

     activate

     my tableColors()

end tell



-- Select the correct colors for the table

on tableColors()

       tell application "System Events"

            tell process "Numbers"

            -- Check if the format window is open

            if (value of attribute "AXMenuItemMarkChar" of menu item "Formattazione" of menu "Inspector" of menu item "Inspector"

               of menu "Vista" of menu bar item "Vista" of menu bar 1) is not "✓" then -- open the window

            click menu item "Formattazione" of menu "Inspector" of menu item "Inspector" of menu "Vista" of menu bar item "Vista" of menu bar 1

            end if

 

            -- Navigate in the window

            click radio button "Tabella" of radio group 1 of window 1 -- Select the right view

            -- Activate the row colors different for near rows

            click checkbox "Colore riga alternato" of scroll area 4 of window 1

            -- Set the color of the second row

            click color well 1 of scroll area 4 of window 1

            delay 2

            click button 2 of toolbar 1 of window "Colori"

            delay 2

            set focused of text field 4 of window "Colori" to true

            delay 2

            keystroke "F0F6E5"

            delay 2

            keystroke return

            delay 0.5

            -- Close the color window

            keystroke "C" using command down

            click color well 1 of scroll area 4 of window 1

            end tell

       end tell

end tableColors

 

----------------------------------------------------

 

The issue is that the color window is never "selected", for instance during the execution of the script I have never seen the 3 window button on the top left coloured. I am not able to set that window frontemost and so the change to the color doesn't apply. The text "F0F6E5" is written in the selected cell on the numbers document instead that inside the text field.

Also trying to directly set value of text field "Rosso" of window "Colori" to "158" does not work.

 

I am using Numbers in italian, so in the script the names of the UI Elements are in italian. The english one should be:

  • Formattazione --> Format / Formatting
  • Vista --> View
  • Tabella --> Table
  • Colore riga alternato --> Alternate row color
  • Rosso --> Red
  • Colori --> Color / Colors

 

Thanks for the help!!

MacBook Pro (15-inch Early 2011), macOS Sierra

Posted on Sep 5, 2016 2:17 PM