PDA

View Full Version : import plain textfile into ical events




yoyellow
Jul 16, 2012, 05:01 PM
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.

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!



kryten2
Jul 16, 2012, 10:48 PM
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.

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

jared_kipe
Jul 16, 2012, 10:52 PM
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.

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.

yoyellow
Jul 17, 2012, 04:39 AM
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?

kryten2
Jul 17, 2012, 09:17 AM
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

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

yoyellow
Jul 17, 2012, 09:39 AM
cheers kryten, this wil get me going for a while :)

yoyellow
Jul 17, 2012, 10:08 AM
so ill just keep asking :D

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

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

kryten2
Jul 17, 2012, 10:31 AM
theCal is a list. Change this :

tell calendar theCal

to

tell calendar (item 1 of theCal)

yoyellow
Jul 17, 2012, 10:44 AM
theCal is a list. Change this :

tell calendar theCal

to

tell calendar (item 1 of theCal)

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:

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

kryten2
Jul 17, 2012, 04:48 PM
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 :


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)