import plain textfile into ical events

Discussion in 'Mac Programming' started by yoyellow, Jul 16, 2012.

  1. macrumors newbie

    Joined:
    Jul 16, 2012
    #1
    Dear People,

    Id like to call upon your expertise to help me with a little project of mine.

    Me, together with all my colleagues, get our work-schedule every week drafted in a "very" ugly and quite unreadable .pfd file.

    what i would like to, is to somehow parse the usefull info from these files and put these events straight into iCal.

    because the pdf file is really stupid to read i can copy paste all the text and make it a plain text tile. which is easier to read. for starters id love to know where/how to start parsing the piece of text i pasted here.

    Code:
    Employee Name:  last name, firstname
    Nominal Date Day Earliest Start Latest Stop Work General SCHHRS
    07/23/2012 Mon 10:00 18:30 APIOS1 PREL_2 8:00
    07/24/2012 Tue 11:30 20:00 APIOS1 PREL_2 8:00
    07/26/2012 Thu 10:00 18:30 APIOS1 PREL_2 8:00
    07/27/2012 Fri 10:00 18:30 APIOS1 PREL_2 8:00
    what i would like is to make from this list 4 cal events on mon, tue, etc that start and end on the correct time. From there on i can build this bigger and maybe start using more calenders etc, we'll see.

    now im a total n00b when it comes to programming or scripting, so the easier the better. i have done tiny bits of applescripting in teh days, and a bit of "Automator action"

    Id like to hear good ways of making this possible. Any real help is welcome!
     
  2. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #2
    Perhaps this script will get you started. Looks like your text file uses the mm/dd/yyyy notation for the date and 24h notation for the time. I had to set my region to United States and adjusted the end times in the text file (eg 20:00=08:00). Note: only lines starting with date in the txt file.

    Code:
    --Change line below to the location of your txt file
    set theFile to "/Users/kryten/schedule.txt"
    set theSchedule to paragraphs of (read theFile)
    repeat with aParagraph in theSchedule
    	try
    		set TID to AppleScript's text item delimiters
    		set AppleScript's text item delimiters to {space}
    		set theEvent to text items of aParagraph
    		set AppleScript's text item delimiters to TID
    	on error
    		set AppleScript's text item delimiters to TID
    	end try
    	set startDate to item 1 of theEvent & space & item 3 of theEvent & space & "AM"
    	set endDate to item 1 of theEvent & space & item 4 of theEvent & space & "PM"
    	set startDate to date startDate
    	set endDate to date endDate
    	tell application "iCal"
    		tell calendar "Work"
    			make new event with properties {description:item 6 of theEvent, start date:startDate, end date:endDate, summary:item 5 of theEvent}
    		end tell
    	end tell
    end repeat
    
     

    Attached Files:

  3. macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #3
    You should be able to accomplish most/all of this through shell scripting, but I'm not sure about Automator (probably Applescript too but that language is just so verbose I can't get behind it).

    For example, presuming that is how your 'text' file looks, this will echo out the following.
    Code:
    awk '$5 == "APIOS1" { print $1 " " $2 " " $3 "-" $4;}' temp.txt
    
    07/23/2012 Mon 10:00-18:30
    07/24/2012 Tue 11:30-20:00
    07/26/2012 Thu 10:00-18:30
    07/27/2012 Fri 10:00-18:30
    
    Which may or may not be much closer for iCal to be able to import. So the next step would be to lookup what format's iCal is able to import, and form the print step into something iCal can get and redirect awk's output into a file.

    EDIT: Orrrr just use kryten2's nearly fully formed applescript.
     
  4. yoyellow, Jul 17, 2012
    Last edited: Jul 17, 2012

    thread starter macrumors newbie

    Joined:
    Jul 16, 2012
    #4
    Perfect! the script from kryten2 works like a charm, now i have to work out how i can use this as an example to enhance the script and to get it to work without the am/pm notation (living in the netherlands we don't use am/pm here)

    edit:
    OK so i fiddled around with the script, one thing i found that the date format is really picky on its settings. and i havent found out a way to make it less picky.

    it gives this error all teh time: Invalid date and time date 07/23/2012 10:00 of «script».

    any idea on how to make it work with both dutch and US date/time formats?
     
  5. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #5
    Set your region to Netherlands (Dutch). Use the unmodified date lines in the txt file eg 07/23/2012 Mon 10:00 18:30 APIOS1 PREL_2 8:00

    Code:
    --Change line below to the location of your txt file
    set theFile to "/Users/kryten/schedule.txt"
    set theSchedule to paragraphs of (read theFile)
    repeat with aParagraph in theSchedule
    	try
    		set TID to AppleScript's text item delimiters
    		set AppleScript's text item delimiters to {space}
    		set theEvent to text items of aParagraph
    		set AppleScript's text item delimiters to TID
    	on error
    		set AppleScript's text item delimiters to TID
    	end try
    	-- next line to get date in the dd/mm/yyyy format
    	set theDate to do shell script "date -j -f %m/%d/%Y" & space & item 1 of theEvent & space & "+%d/%m/%Y"
    	-- no need for the AM/PM stuff
    	set startDate to theDate & space & item 3 of theEvent
    	set endDate to theDate & space & item 4 of theEvent
    	set startDate to date startDate
    	set endDate to date endDate
    	tell application "iCal"
    		tell calendar "Work"
    			make new event with properties {description:item 6 of theEvent, start date:startDate, end date:endDate, summary:item 5 of theEvent}
    		end tell
    	end tell
    end repeat
    Events & Replies from the script :


    tell current application
    read "/Users/kryten/schedule.txt"
    --> "07/23/2012 Mon 10:00 18:30 APIOS1 PREL_2 8:00\n07/24/2012 Tue 11:30 20:00 APIOS1 PREL_2 8:00\n07/26/2012 Thu 10:00 18:30 APIOS1 PREL_2 8:00\n07/27/2012 Fri 10:00 18:30 APIOS1 PREL_2 8:00"
    do shell script "date -j -f %m/%d/%Y 07/23/2012 +%d/%m/%Y"
    --> "23/07/2012"
    end tell
    tell application "iCal"
    make new event with properties {description:"PREL_2", start date:date "maandag 23 juli 2012 10:00:00", end date:date "maandag 23 juli 2012 18:30:00", summary:"APIOS1"}
    --> event id "EB720DB8-A3D2-4EFD-9767-1C6ACFCB323D" of calendar id "EE99D648-C315-4664-B92A-AC6C5CE0929E"
    end tell
     

    Attached Files:

  6. thread starter macrumors newbie

    Joined:
    Jul 16, 2012
    #6
    cheers kryten, this wil get me going for a while :)
     
  7. yoyellow, Jul 17, 2012
    Last edited: Jul 17, 2012

    thread starter macrumors newbie

    Joined:
    Jul 16, 2012
    #7
    so ill just keep asking :D

    so i enhanced some things and ran into an error im not sure how to fix (yet)

    Code:
    --input file in plain text
    tell application "Finder"
    	set new_file to choose file with prompt "Open file to be edited" of type ("txt")
    	set theFile to new_file
    end tell
    --ask in what cal u want the events imported
    tell application "iCal"
    	set theCalendarNames to title of every calendar
    end tell
    
    set theCal to choose from list theCalendarNames with title "Work Calender" with prompt "In which calender do you want your workhours?"
    
    
    set theSchedule to paragraphs of (read theFile)
    repeat with aParagraph in theSchedule
    	try
    		set TID to AppleScript's text item delimiters
    		set AppleScript's text item delimiters to {space}
    		set theEvent to text items of aParagraph
    		set AppleScript's text item delimiters to TID
    	on error
    		set AppleScript's text item delimiters to TID
    	end try
    	-- next line to get date in the dd/mm/yyyy format
    	set theDate to do shell script "date -j -f %m/%d/%Y" & space & item 1 of theEvent & space & "+%d/%m/%Y"
    	-- no need for the AM/PM stuff
    	set startDate to theDate & space & item 3 of theEvent
    	set endDate to theDate & space & item 4 of theEvent
    	set startDate to date startDate
    	set endDate to date endDate
    	tell application "iCal"
    		tell calendar theCal
    			make new event with properties {description:item 6 of theEvent, start date:startDate, end date:endDate, summary:item 5 of theEvent}
    		end tell
    	end tell
    end repeat
    problem is this line (i think): tell calendar theCal, where theCal is for example {"Werk"} but its not accepted somehow...

    what am i doing wrong?


    edit:

    looks like its fixed this way

    set theCal1 to choose from list theCalendarNames with title "Work Calender" with prompt "In which calender do you want your workhours?"
    set theCal to item 1 of theCal1
     
  8. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #8
    theCal is a list. Change this :

    Code:
    tell calendar theCal
    
    to
    
    tell calendar (item 1 of theCal)
     
  9. thread starter macrumors newbie

    Joined:
    Jul 16, 2012
    #9
    Nice, yes i got it working now. And allready trying the next thing :p

    Because the file is a quite nasty pdf i dont think appelscript will ever be able to read from this (correct me if im wrong). for now i copy all the text into a .txt file i use for this script.

    The Next thing i want to do is to be able to simple copy/paste all the text from the pdf in a txt file and then extract only the usefull info out of that.

    First thing i want is to get all the names from people in a list so i can select who's calendar to update.

    got a piece of example file:

    Code:
    07/13/2012  13:50:48
    Superstate Hours with Schedule Summary
    07/23/2012 to 07/29/2012
    Employee Group:  FT40
    Employee Name:  lastname1, firstname1
    Nominal Date Day Earliest Start Latest Stop Work General SCHHRS
    07/23/2012 Mon 11:30 20:00 APIOS1 PREL_2 8:00
    07/24/2012 Tue 10:00 18:30 APIOS1 PREL_2 8:00
    07/25/2012 Wed 09:45 18:15 APIOS1 PREL_2 8:00
    07/27/2012 Fri 10:00 18:30 APIOS1 PREL_2 8:00
    07/28/2012 Sat 09:30 18:00 APIOS1 PREL_2 8:00
    Total hours and minutes for lastname1, firstname1: 40:00
    Employee Name:  lastname2, firstname2
    Nominal Date Day Earliest Start Latest Stop Work General SCHHRS
    07/23/2012 Mon 11:30 20:00 APIOS1 PREL_2 8:00
    07/24/2012 Tue 08:00 16:30 APIOS1 PREL_2 8:00
    07/25/2012 Wed 08:00 16:30 APIOS1 PREL_2 8:00
    07/26/2012 Thu 08:00 16:30 APIOS1 PREL_2 8:00
    07/27/2012 Fri 11:30 20:00 APIOS1 PREL_2 8:00
    Total hours and minutes for lastname2, firstname2: 40:00
    so how do i get applescript to read only the names out of this file, and then, lateron use the times and dates we previously used to import that into ical...

    what i came up with so far is using alot "if>then's" to get to read certain part, but maybe it can be done easier..
     
  10. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #10
    Certainly not the best or most elegant way and I'm not sure if this is the list format you want but you can give it a try :


    Code:
    set theFile to "/Users/kryten/schedule2.txt"
    set theNames to do shell script "cat " & theFile & " | grep \"Employee Name:\"" & " | cut -d : -f 2"
    try
    	set TID to AppleScript's text item delimiters
    	set AppleScript's text item delimiters to {space}
    	set theNamesList to text items of theNames
    	set AppleScript's text item delimiters to ""
    	set theNamesList to text items of theNamesList as text
    	set AppleScript's text item delimiters to {return}
    	set theNamesList to text items of theNamesList
    on error
    	set AppleScript's text item delimiters to TID
    end try
    Tested with your example file(added 2 more employees)
     

    Attached Files:

Share This Page