Applescript check if specific Numbers sheet exists

328 Views Asked by At

I'm trying to use Applescript to process inputs from one worksheet to create another with a specific name. I can create the new sheet without any problem, but if I run the script twice, it (appropriately) gives me an error because a sheet with that name already exists.

This is what I've tried, but it gives me an error on the if statement ('Can't get sheet whose name = "[the value of nextTuesdaysName]"'):

set nextTuesday to current date
repeat until nextTuesday's weekday is Tuesday
    set nextTuesday to nextTuesday + days
end repeat
set {year:nextTuesdaysYear, month:nextTuesdaysMonth, day:nextTuesdaysDay} to nextTuesday
set nextTuesdaysName to "Tuesday " & (nextTuesdaysYear * 10000 + nextTuesdaysMonth * 100 + nextTuesdaysDay as string)

tell application "Numbers"
    tell document 1

        if (sheet whose name is nextTuesdaysName) exists then
               display dialog "There's already a worksheet named '" & 
                      nextTuesdaysName & "'" buttons {"Quit", "Replace"} default button 2 with icon 1
            # delete (first sheet whose name is nextTuesdaysName)
        end if
        set thisSheet to make new sheet with properties {name:nextTuesdaysName}
    end tell
end tell

How do I structure the if statement to check if the named sheet exists?

TIA

2

There are 2 best solutions below

3
Mockman On BEST ANSWER

This script attempts to make a new sheet. When it fails —due to any error— it coughs up a dialogue with two choices. The 'replace' choice first deletes the existing sheet of that name and then creates another. Update: include specific error message.

set nextTuesday to current date
repeat until nextTuesday's weekday is Tuesday
    set nextTuesday to nextTuesday + days
end repeat
set {year:nextTuesdaysYear, month:nextTuesdaysMonth, day:nextTuesdaysDay} to nextTuesday
set nextTuesdaysName to "Tuesday " & (nextTuesdaysYear * 10000 + nextTuesdaysMonth * 100 + nextTuesdaysDay as string)
tell application "Numbers"
set nextTuesdaysName to "somethingorother"
tell document 1
    try
        set thisSheet to make new sheet with properties {name:nextTuesdaysName}
    on error number -2763
        display dialog "Worksheet '" & nextTuesdaysName & "' already exists" buttons {"Quit", "Replace"} default button 2 with icon 1
        
        if button returned of the result is "Replace" then          
            delete sheet nextTuesdaysName
            set thisSheet to make new sheet with properties {name:nextTuesdaysName}
            
        end if
    end try
end tell
end tell

Here is an alternate method which does not utilize error handling, instead adding an if…then to test for the sheet's existence. Both now incorporate the date-based sheet-naming mechanism.

set nextTuesday to current date
repeat until nextTuesday's weekday is Tuesday
    set nextTuesday to nextTuesday + days
end repeat
set {year:nextTuesdaysYear, month:nextTuesdaysMonth, day:nextTuesdaysDay} to nextTuesday
set nextTuesdaysName to "Tuesday " & (nextTuesdaysYear * 10000 + nextTuesdaysMonth * 100 + nextTuesdaysDay as string)

tell application "Numbers"
    tell document 1
        if name of sheets contains nextTuesdaysName then
            display dialog "There's already a worksheet named '" & nextTuesdaysName & "'" buttons {"Quit", "Replace"} default button 2 with icon 1
            
            if button returned of the result is "Replace" then
                delete sheet nextTuesdaysName
                set thisSheet to make new sheet with properties {name:nextTuesdaysName}
            end if
        else
            set thisSheet to make new sheet with properties {name:nextTuesdaysName}
        end if
    end tell
end tell
1
Robert Kniazidis On

@Russ,

AppleScript can pretty fine check if some object exists without throwing any error. Your mistake is using whose clause. Correct syntax:

set nextTuesday to current date
repeat until nextTuesday's weekday is Tuesday
    set nextTuesday to nextTuesday + days
end repeat
set {year:nextTuesdaysYear, month:nextTuesdaysMonth, day:nextTuesdaysDay} to nextTuesday
set nextTuesdaysName to "Tuesday " & (nextTuesdaysYear * 10000 + nextTuesdaysMonth * 100 + nextTuesdaysDay as string)

tell application "Numbers" to tell document 1
    if (sheet nextTuesdaysName) exists then
        display dialog "There's already a worksheet named '" & nextTuesdaysName & "'" buttons {"Quit", "Replace"} default button 2 with icon 1
        -- delete (sheet nextTuesdaysName)
    end if
    set thisSheet to make new sheet with properties {name:nextTuesdaysName}
end tell