Hi Jeffkr.
Sorry for the slow reply. I’ve had a busy weekend and testing your script required a bit of setting up.
I’m afraid I no longer have iCal 4.0.4. I do have iCal 3.0.8 on a PowerPC machine running Lion and Calendar 8.0 on an Intel machine running El Capitan.
Your script compiles on both machines. On the El Capitan one, it even compiles for application “Calendar” directly from the application “iCal” source code! But “iCal” still has to be changed to “Calendar” in the GUI Scripting section. Calendars have had ‘names’ rather than ‘titles’ since at least iCal 2.0.5, but ‘title’ still appears to work in both cases.
You didn’t mention your start conditions, so I’ve had to infer them from your script and the thread. With iCal 3.0.8, given the existence of calendars named “Personal” and “Personal 2” and a single-event file on the desktop named “Personal.ics”, the script does more or less what I’d expect. The calendars are renamed “Personal (old)” and “Personal 2 (old)” respectively, the event is imported to a new calendar named after the event’s summary, and a dialog is displayed saying that “Personal 2” was renamed “Personal 2 (old)”.
With Calendar 8.0, the calendars are renamed, but Calendar has to be quit and reopened for the changes to appear in the GUI. The ‘open’ command in the script appears to be ignored ” not even throwing an error ” so the “Add Events” window never opens and the repeat waits forever for it to do so. Maybe this is what’s happening with iCal 4.0.4 on your machine.
The equivalent manual actions still work with Calendar 8.0: double-clicking the file on the desktop opens an “Add Event” window and clicking “OK” in this with “New Calendar” selected imports the event to a new calendar. Here, though, the new calendar’s named “Personal” after the file rather than being named with the event’s summary.
I tried scripting the Finder, instead of Calendar, to open the file and got a security message saying that CoreServicesUIAgent wanted to access my calendars. I decided not to allow this the first time, after which even the Finder wouldn’t open the file in the script, although manually double-clicking still worked. But since the access permission had been asked, I was able to allow it in the Security pane of System Preferences. After this ” and after ‘process “iCal”’ had been changed to ‘process “Calendar”’ and “Add Events” to “Add Event” ” the script successfully ran to completion. There was another difference with the calendar naming in that the new calendar was automatically given the next number in the series ” ie. “Personal 3” ” and its creation encouraged Calendar to display the changed calendar names immediately.
Apart from the calendar naming differences, the version of your script below works for me with both iCal 3.0.8 and Calendar 8.0, so there’s a reasonable chance it’ll work with iCal 4.0.4 too.
set dupCalName to ""
set thePath to (path to desktop as text) & "Personal.ics"
tell application id "com.apple.iCal"
activate
-- Get the application's name (presumably either "iCal" or "Calendar") for the GUI Scripting later.
set iCalName to its name
-- Since it's possible for more than one calendar to have the same name, the repeat loop idea is better than simple name references here. But only one loop is necessary in this part of the script.
repeat with i from 1 to count of calendars
set thisCalendar to calendar i -- Gets an id reference to the calendar.
if (name of thisCalendar is "Personal") then
set dupCalName to "Personal"
set replacementName to "Personal (old)"
set name of thisCalendar to replacementName
else if (name of thisCalendar is "Personal 2") then
set dupCalName to "Personal 2"
set replacementName to "Personal 2 (old)"
set name of thisCalendar to replacementName
end if
end repeat
end tell
delay 1
-- "Double-click" the file, since iCal/Calendar's 'open' doesn't work in later versions.
tell application "Finder" to open file thePath
tell application "System Events"
tell process iCalName
-- Hedge bets over the name of the "Add Event" or "Add Events" window.
tell (first window whose title begins with "Add event")
repeat until (it exists)
delay 0.2
end repeat
click pop up button 1
repeat until (menu 1 of pop up button 1 exists)
delay 0.2
end repeat
click menu item "New Calendar" of menu 1 of pop up button 1
repeat while (menu 1 of pop up button 1 exists)
delay 0.2
end repeat
click button "OK"
end tell
end tell
end tell
--check if a Personal 2 exists after the import of new iCal and change its name
tell application id "com.apple.iCal"
activate
repeat with i from 1 to count of calendars
set thisCalendar to calendar i
if (name of thisCalendar) is "Personal 2" then
set name of thisCalendar to "Personal"
exit repeat
end if
end repeat
end tell
tell application "SystemUIServer"
activate
if dupCalName is not "" then display dialog "Found an existing calendar named " & dupCalName & ". It has been renamed " & replacementName & "." buttons {"OK"} default button "OK" with icon 2
end tell