3 Replies Latest reply: Nov 3, 2005 12:46 PM by Tyson Brown
Tyson Brown Level 1 Level 1 (5 points)
I need to edit the text of an iCal format .vcs file. I'm a REAL NEWBIE to Applescript. So far, I've been able to figure out how to get Applescript to open the file to import it into iCal. What I WANT to be able to do is to use Applescript to edit .vcs files that were exported from another application BEFORE I add them to my iCal. I know the .vcs files can be treated as straight text by Applescript/TextEdit. Here is some example text content from one of these files:

BEGIN:VCALENDAR
VERSION:1.0
BEGIN:VEVENT
DTStart:20051027T134500Z
DTEnd:20051027T164500Z
LOCATION;ENCODING=QUOTED-PRINTABLE:Elluminate Live!
SUMMARY;ENCODING=QUOTED-PRINTABLE: WEBQUEST TESTING
DESCRIPTION;ENCODING=QUOTED-PRINTABLE:To attend this session please goto: https://xxx.xxxx.xxxx
PRIORITY:3
END:VEVENT
END:VCALENDAR

The text after the SUMMARY;ENCODING=QUOTED-PRINTABLE: and

DESCRIPTION;ENCODING=QUOTED-PRINTABLE

Is varialble text that changes with each .vcs event that is created. I want to be able to either a) change that following text to something standard or (if that is not possible) b) remove the variable text altogether.

Could anyone give me any pointers about how that could be done?

Thanks
  • HD Level 4 Level 4 (3,510 points)
    Hi Tyson,

    This will modify the content of a file in the way you wish. I can't create a true .vcs file to test import. Hope it helps (I'm a bit rusty).

    set VCS_file to choose file
    set the_text to read VCS_file
    set parcount to count paragraphs in the_text
    set nu_text to ""
    set {oldtids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
    repeat with x from 1 to parcount
         set nextpar to paragraph x of the_text
         if nextpar begins with "SUMMARY" then set nextpar to text item 1 of nextpar & ":something"
         if nextpar begins with "DESCRIPTION" then set nextpar to text item 1 of nextpar & ":something else"
         set nu_text to nu_text & nextpar & return
    end repeat
    try
         set target_file to (open for access VCS_file with write permission)
         set eof target_file to 0
         write nu_text to target_file
         close access target_file
    on error
         display dialog "VCS file was not changed"
         close access target_file
    end try
    set AppleScript's text item delimiters to oldtids

    As it stands this creates a return character at the end of the last line. If that's a problem you could either parse it out or grep the file.

    HTH
    H
  • Tyson Brown Level 1 Level 1 (5 points)
    Thanks muchly! It works perfectly!!! Now I just have to take a copy of it apart to figure out how you made it!
  • Tyson Brown Level 1 Level 1 (5 points)
    After much futzing around with Applescript, and with the assistance of posters on this forum, I've been able to build an Applescript that does the following:
    1. process a bunch of .vcs files that are dropped overtop of it (drag and drop, courtesy of the Essential Subroutines pages)
    2. Parse out the .vcs file into text paragraphs, courtesy of the poster who replied to my original post.
    3. import that .vcs file content into a selected calendar (actually I have it performing that routine twice, as I want the same date and time information in two different calendars, but with different summary and description information... public versus private - courtesy of the sample Create Birthday Event script and a lot of mucking about!!
    4. provide a dialog to enter participant information for events into the Location field in iCal.

    I figured I should post this script here in case anybody else wants to use it or modify it. If you make changes to it to streamline it, let me know. I'm still a very newbie applescripter so I'm pretty sure this can be made more efficient.

    The Script

    -- the list of file types which will be processed
    -- eg: {"PICT", "JPEG", "TIFF", "GIFf"}
    property type_list : {"VCS"}
    -- since file types are optional in Mac OS X,
    -- check the name extension if there is no file type
    -- NOTE: do not use periods (.) with the items in the name extensions list
    -- eg: {"txt", "text", "jpg", "jpeg"}, NOT: {".txt", ".text", ".jpg", ".jpeg"}
    property extension_list : {"vcs"}

    -- This droplet processes files dropped onto the applet

    on open these_items
         repeat with i from 1 to the count of these_items
              set VCS_file to item i of these_items
              set the item_info to info for VCS_file
              if (folder of the item_info is false) and (alias of the item_info is false) and ((the file type of the item_info is in the type_list) or the name extension of the item_info is in the extension_list) then
                   processitem(VCSfile)
              end if
         end repeat
    end open

    -- this sub-routine processes files
    on processitem(VCSfile)
         -- NOTE that the variable this_item is a file reference in alias format
         -- FILE PROCESSING STATEMENTS GOES HERE
         -- this section reads the .vcs file into the variable the_text and counts the paragraphs
         set the_text to read VCS_file
         set parcount to count paragraphs in the_text
         set nu_text to ""
         set {oldtids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
         
         repeat with x from 1 to parcount
              set nextpar to paragraph x of the_text
              
         end repeat
         -- this section moves the text of each paragraph into a specific variable
         set para1 to paragraph 1 of the_text
         set para2 to paragraph 2 of the_text
         set descript to paragraph 8 of the_text
         set startdate to paragraph 4 of the_text
         set enddate to paragraph 5 of the_text
         set para6 to paragraph 6 of the_text
         set summar to paragraph 7 of the_text
         -- Here we remove the vcs coding from the pertinent paragraphs
         if startdate begins with "DTStart" then set startdate1 to text item 2 of startdate
         if descript begins with "DESCRIPTION;ENCODING=QUOTED-PRINTABLE" then set descript to text items 2 thru 4 of descript as string
         if summar begins with "SUMMARY;ENCODING=QUOTED-PRINTABLE" then set summar to text item 2 of summar
         -- here we start breaking down the text of the start date and end date in order to reconstitute them as something Applescript can understand
         set startyear to text 1 thru 4 of startdate1
         set startmonth to text 5 thru 6 of startdate1
         if startmonth = "01" then set startmonth to "January"
         if startmonth = "02" then set startmonth to "February"
         if startmonth = "03" then set startmonth to "March"
         if startmonth = "04" then set startmonth to "April"
         if startmonth = "05" then set startmonth to "May"
         if startmonth = "06" then set startmonth to "June"
         if startmonth = "07" then set startmonth to "July"
         if startmonth = "08" then set startmonth to "August"
         if startmont