3

I'm trying to duplicate a sheet in a numbers file (I need a new sheet everyday based on a template) using AppleScript. I'm really new to AppleScript but I'm already using it to fill-in some cells with results I get from a python script I run but this bit is still manual... =/

tell application "Numbers"
tell document 1
    set thisSh to make new sheet with properties {name:"May14"}
    duplicate sheet "Template" to sheet "May14"
end tell
end tell

The code above returns the error: error "Numbers got an error: Sheets can not be copied." number -1717 and, therefore, my question: Is there a way to duplicate a numbers sheet?

I'm currently running iWork '14 with numbers 3.2

Thanks!

P.S. I also tried duplicate every table of sheet "Template" to sheet "May14" with a similar error: Tables can not be copied.

pekapa
  • 881
  • 1
  • 11
  • 25

1 Answers1

2

You have two options. You can build the table in the new sheet programmatically, or you can select and copy the table(s) from the first sheet and copy it(them) in the new sheet. Here is code for the second option. Having not seen a screen grab of your template sheet, you may have to adjust, but this should give you everything you need. If you run into complications, please post a screenshot and description of your template sheet. Updated to account for a crazy number of tables in the sheet. Updated again to demonstrate how to change the active sheet.

tell application "Numbers"
    activate
    tell document 1
        set tempSheet to first sheet whose name is "My Template"
        set active sheet to tempSheet
        tell application "System Events"
            -- deselect all
            keystroke "a" using {command down, shift down}
            delay 0.1
            -- select all containers (tables, text items, etc.)
            keystroke "a" using {command down}
            delay 0.1
            -- copy the containers
            keystroke "c" using {command down}
        end tell
        delay 0.1
        set dateString to (month of (current date)) & (day of (current date)) as string
        set thisSheet to make new sheet with properties {name:dateString}
        tell thisSheet
            delete every table
            tell application "System Events"
                keystroke "v" using {command down}
            end tell
        end tell
    end tell
end tell
jweaks
  • 3,674
  • 1
  • 19
  • 32
  • I'll test something like this later today but it probably won't be really helpful as I have 17 tables per sheet (all with hidden cells). So it will probably be a mess... – pekapa May 22 '14 at 20:45
  • 1
    Don't be such a pessimist; you'll get it. :) I added more code to help demonstrate more. Post if you're still having issues. – jweaks May 23 '14 at 02:58
  • That's working quite nicely! xD However, my template sheet must be active, if any other sheet is in front, that will be the one copied. (I thought the 'touch' hack would solve that but no. =/) – pekapa May 23 '14 at 03:15
  • Added the set active sheet line, which solves that problem. Plus, showed you another way to copy all elements. Instead of the touch container hack, you can deselect all in a sheet, then select all. Let me know how that works. Please do accept the answer, if you appreciate the help. – jweaks May 23 '14 at 03:33
  • Wonderful! That completely solves it! I just don't understand why you can't duplicate them with `duplicate` since you can to it by right clicking in the sheet name... Anyway, thanks for all your help! – pekapa May 23 '14 at 04:06
  • Glad it's working. Hard to say about not duping a sheet, the the library is mostly geared towards building programmatically. – jweaks May 23 '14 at 04:08