Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jul 16, 2012, 06:01 PM   #1
yoyellow
macrumors newbie
 
Join Date: Jul 2012
import plain textfile into ical events

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!
yoyellow is offline   0 Reply With Quote
Old Jul 16, 2012, 11:48 PM   #2
kryten2
macrumors 6502a
 
Join Date: Mar 2012
Location: Belgium
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 Thumbnails
Click image for larger version

Name:	Screen shot 2012-07-17 at 5.09.32 AM.png
Views:	96
Size:	182.3 KB
ID:	348444   Click image for larger version

Name:	Screen shot 2012-07-17 at 5.09.24 AM.png
Views:	37
Size:	100.3 KB
ID:	348445  
__________________
Space Corps Directive 34124
kryten2 is offline   1 Reply With Quote
Old Jul 16, 2012, 11:52 PM   #3
jared_kipe
macrumors 68030
 
jared_kipe's Avatar
 
Join Date: Dec 2003
Location: Seattle
Send a message via AIM to jared_kipe
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.
jared_kipe is offline   0 Reply With Quote
Old Jul 17, 2012, 05:39 AM   #4
yoyellow
Thread Starter
macrumors newbie
 
Join Date: Jul 2012
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?

Last edited by yoyellow; Jul 17, 2012 at 09:57 AM.
yoyellow is offline   0 Reply With Quote
Old Jul 17, 2012, 10:17 AM   #5
kryten2
macrumors 6502a
 
Join Date: Mar 2012
Location: Belgium
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 Thumbnails
Click image for larger version

Name:	Screen shot 2012-07-17 at 15.48.36.png
Views:	23
Size:	80.0 KB
ID:	348491  
__________________
Space Corps Directive 34124
kryten2 is offline   0 Reply With Quote
Old Jul 17, 2012, 10:39 AM   #6
yoyellow
Thread Starter
macrumors newbie
 
Join Date: Jul 2012
cheers kryten, this wil get me going for a while
yoyellow is offline   0 Reply With Quote
Old Jul 17, 2012, 11:08 AM   #7
yoyellow
Thread Starter
macrumors newbie
 
Join Date: Jul 2012
so ill just keep asking

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

Last edited by yoyellow; Jul 17, 2012 at 11:18 AM.
yoyellow is offline   0 Reply With Quote
Old Jul 17, 2012, 11:31 AM   #8
kryten2
macrumors 6502a
 
Join Date: Mar 2012
Location: Belgium
theCal is a list. Change this :

Code:
tell calendar theCal

to

tell calendar (item 1 of theCal)
__________________
Space Corps Directive 34124
kryten2 is offline   0 Reply With Quote
Old Jul 17, 2012, 11:44 AM   #9
yoyellow
Thread Starter
macrumors newbie
 
Join Date: Jul 2012
Quote:
Originally Posted by kryten2 View Post
theCal is a list. Change this :

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

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..
yoyellow is offline   0 Reply With Quote
Old Jul 17, 2012, 05:48 PM   #10
kryten2
macrumors 6502a
 
Join Date: Mar 2012
Location: Belgium
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 Thumbnails
Click image for larger version

Name:	Screen shot 2012-07-17 at 23.45.35.png
Views:	31
Size:	92.7 KB
ID:	348547  
__________________
Space Corps Directive 34124
kryten2 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Deleting Old Events in iCal Garoolgan iOS 7 1 May 11, 2014 07:14 AM
iPhone 5 not syncing events on iCal Johnny Bowsky iPhone 4 May 21, 2013 12:22 AM
iCal not syncing events from iPhone 5 Johnny Bowsky Mac Applications and Mac App Store 2 May 18, 2013 10:14 AM
iPhoto Events and Last Import stockcerts Mac Applications and Mac App Store 3 Feb 9, 2013 04:01 PM
Import some events to iCal digitalove Mac Basics and Help 1 Jun 14, 2012 07:48 AM

Forum Jump

All times are GMT -5. The time now is 04:30 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC