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
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.
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.