Hello everybody,
Last time I posted in this forum you were all extremely helpful, so I was hoping you might be willing to help me on another scripting issue…
I’m provided a .txt file which contains my roster for my working week as it is variable, and it also contains everybody else so what I’m trying to do is have it extract each individual person’s roster, and if it matches my name, creates iCal events based on the times it provides.
Sounds simple enough, but there’s lots of snags in the process as you might notice; Here is a copy of my section of my roster below (Everybody’s looks the same):
A chunk of the roster is visible at this URL
The entire roster is full of spaces and tabs rather than actual columns, the page number is not always 38 and there’s always the white space prior to my employee number but the amount of it is random each week.
The only pattern that is always the same is that if all the whitespace is removed, it has this for the shifts:
Ideally I would have it strip out everything except the number, my name, and the format mentioned above, and have a variable/list with each day’s chunk separate to make it easier to put them into a calendar.
The script I have so far feels really clunky and a really dodgy method of doing it, but my lack of experience is holding me down with fixing it.
If anybody was willing to help out with this I would be very grateful.
Here’s the script I have so far (It’s not finished, but so far it’s pretty sucky):
ï® The functions .
on splitText(delimiter, someText)
set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to delimiter
set output to text items of someText
set AppleScript's text item delimiters to prevTIDs
return output
end splitText
-- Example
-- splitText(",", "a,b,c,d,e")
on findAndReplace(tofind, toreplace, TheString)
set ditd to text item delimiters
set text item delimiters to tofind
set textItems to text items of TheString
set text item delimiters to toreplace
if (class of TheString is string) then
set res to textItems as string
else -- if (class of TheString is Unicode text) then
set res to textItems as Unicode text
end if
set text item delimiters to ditd
return res
end findAndReplace
-- Set the variables
set userName to "24367 Matthew Stares "
set endRosterChunk to "____________________"
set rosterFolder to "Macintosh HD:Users:Mat:Dropbox:Lunch Rosters:"
-- Set the roster location, this is temporary until it searches for all
set theRoster to "Macintosh HD:Users:Mat:Dropbox:Lunch Rosters: P12W02.txt" as alias
open for access theRoster
set theData to read theRoster
close access theRoster
--Split each roster into 'chunks' of individual user's rosters
set theDataSplit to splitText(endRosterChunk, theData)
set totalAdvisors to count of theDataSplit
--Get the week it runs for
set startDate to text 86 through 93 of (item 1 of theDataSplit)
set splitStartDate to splitText("/", startDate)
-- Convert it to an Australian format for calendar usage
set theStartWeek to (item 2 of splitStartDate) & "/" & (item 1 of splitStartDate) & "/" & (item 3 of splitStartDate)
--The formats are as follows:
--ID - XXXXX (5 Numbers)
--Date - mm/dd/yy
--Day - Sun/Mon/etc.
--Start/Stop - hh:mm
--Exception Code - Break/Lunch/Vacation/Public Holiday
repeat with i from 1 to count of theDataSplit
set tempString to item i of theDataSplit as text
-- remove spaces for easier formatting (It won't work for new lines though..)
set tempString to findAndReplace(" ", "", tempString)
set item i of theDataSplit to findAndReplace(" ", "", tempString)
end repeat
-- Testing breaking it down into parts based on newlines
set tempString to item 38 of theDataSplit
set AppleScript's text item delimiters to return
set testTempString to text items of tempString
(*
Output of the above little testing chunk:
{"", "
", "
IEXTotalViewPage:38", "
Date:03/17/13to03/23/13WeeklySchedules", "
Thursday,03/14/13", "
", "
ManagementUnit:T2", "
", "
", "
Sortedby:Name", "
ReportAcrossAgentMoves:NoReportAgentMoves:No", "
", "
", "
24367MatthewStares", "
DateDayStartStopExceptionCodeStartStop", "
______________________________________________________", "
03/17/13SunOFFOFF", "
03/18/13Mon08:0017:00Break10:2010:30", "
Lunch13:0014:00", "
Break15:3515:45", "
03/19/13Tue08:0016:30Break10:0510:15", "
Lunch12:3013:30", "
Break15:2015:30", "
03/20/13Wed08:0016:30Break09:3509:45", "
Lunch11:3012:30", "
Break14:3014:40", "
03/21/13ThuOFFOFF", "
03/22/13Fri08:0016:30Break10:2010:30", "
Lunch12:3013:30", "
Break15:0515:15", "
03/23/13Sat08:0016:30Break10:2010:30", "
Lunch12:3013:30", "
Break15:0515:15", "
", "
"}
*)
--Trying to get only the parts from my name until the end of the week.
set onetestTempString to items 14 through -2 of testTempString
(*
Output after running the bit above:
{"
24367MatthewStares", "
DateDayStartStopExceptionCodeStartStop", "
______________________________________________________", "
03/17/13SunOFFOFF", "
03/18/13Mon08:0017:00Break10:2010:30", "
Lunch13:0014:00", "
Break15:3515:45", "
03/19/13Tue08:0016:30Break10:0510:15", "
Lunch12:3013:30", "
Break15:2015:30", "
03/20/13Wed08:0016:30Break09:3509:45", "
Lunch11:3012:30", "
Break14:3014:40", "
03/21/13ThuOFFOFF", "
03/22/13Fri08:0016:30Break10:2010:30", "
Lunch12:3013:30", "
Break15:0515:15", "
03/23/13Sat08:0016:30Break10:2010:30", "
Lunch12:3013:30", "
Break15:0515:15", "
"}
)*
Model: Mac Mini
Browser: Chrome
Operating System: Mac OS X (10.8)