Improper spacing of names output in iCal

I recently modified an iCal script based on MLB Scheduler, 1.1, modified by
Jason Snell jsnell@macworld.com Based on Sports Scheduler, v. 1.0, © 2006 by
Frank Zalar frank.zalar@gmail.com so it parses csv data into a call schedule and a
vacation schedule on .Mac for our organization and it actually works really
well! The problem is a fine point but it is annoying me so much that I
searched the web and Macscripter site and did not find the answer. I have no experience scripting other than
hypercard scripting in the early 80’s.

The Perplexing PROBLEM is spacing between variables output. I would like it to come out like:

                                                             Out of Town Feng Bach Lund Yeh Rizea Schandelmeier Barger

but it comes out:

                                                             Out of Town FengBachLundYehRizeaSchandelmeierBarger

and I manually seperate the names which is a hassle.

 
set OutOfTown to "Out of Town " as text -- new variable that stores text to differentiate reason off call schedule 00T=Out of Town

set OOTDate to text (item 1 of OOTData & "," & item 2 of OOTData) as date

set OOTTime to (item 2 of OOTData)

set OutOfTownEqualsY to (item 3 of OOTData)

set Feng to (item 4 of OOTData) -- who is out of town

set Lund to (item 5 of OOTData) -- who is out of town

set Bach to (item 6 of OOTData) -- who is out of town

set Yeh to (item 7 of OOTData) -- who is out of town

set Rizea to (item 8 of OOTData) -- who is out of town

set Schandy to (item 9 of OOTData) -- who is out of town

set Barger to (item 10 of OOTData) -- who is out of town

set OutOfTownProviders to (Feng & Lund & Bach & Yeh & Rizea & Schandy &
Barger) as text

if (OutOfTownEqualsY is equal to "Y") then -- checks for Y in 3rd column of csv to determine if anyone is OOT

set WhoIsOutOfTown to (OutOfTown & OutOfTownProviders) as text -- define combined data as text and puts it into variable WhoIsOutOfTown

-- it to iCal's event n class summary property (unicode text) below

else if (OutOfTownEqualsY is not equal to "Y") then

set WhoIsOutOfTown to (item 3 of OOTData) as text -- sets variable WhoIsOutOfTown to 7:00 AM if nobody is out of town

end if 

Thank you for assisting me with this problem.

Sincerely Gschandy

Model: Dual 1.8 Mhz G5
AppleScript: 1.10.6
Browser: Safari 417.9.3
Operating System: Mac OS X (10.4)

 
set OutOfTownProviders to (Feng & Lund & Bach & Yeh & Rizea & Schandy &
Barger) as text

set WhoIsOutOfTown to (OutOfTown & OutOfTownProviders) as text -- define combined data as text and puts it into variable WhoIsOutOfTown

Hi Gschandy,

The above lines are what set the string. The 1st one takes the seperate names and joins (concatenate) them together. It’s putting them together without spaces. You change that to


set OutOfTownProviders to (Feng & space & Lund & space & Bach & space & Yeh & space & Rizea & space & Schandy & space & Barger

set WhoIsOutOfTown to (OutOfTown & space & OutOfTownProviders)

or


set OutOfTownProviders to (Feng & " " & Lund & " " & Bach & " " & Yeh & " " & Rizea & " " & Schandy & " " & Barger

set WhoIsOutOfTown to (OutOfTown & " " & OutOfTownProviders)

For a while, I was using scripts I found. It was only recently that I started to write my own. I’m finding AppleScript very interesting and fun, and not impossible to learn. Like the above changes, not too bad huh?

There’s lots of resources on this site to help you continue. Good luck.

Gary

Wow Great gyuen,
That does it. Your right I was very happy to work this script out with applescript from an open source resoure script. Here is the complete modified open source script for anyone making a vacation schedule for up to seven people in iCal.
Thanks again gyuen

Sincerely gschandy


(*
Out of Town Scheduler based on

Call Scheduler, 1.1, modified by Greg Schandelmeier <gschandy@mac.com> based on
MLB Scheduler, 1.1, modified by Jason Snell <jsnell@macworld.com>
Based on Sports Scheduler, v. 1.0, © 2006 by Frank Zalar <frank.zalar@gmail.com>

This script is distributed as open source software
You are free to copy, modify and share parts of or the entire script, as
long as you
a) Keep this notice in any modified version of the script
b) Transfer this notice to any derrivative work
c) Don't charge money for this script or any derrivative of it

This software is provided "as is" without any warranty, explicit or implied.
The author of this software is not liable to any loss or damage of data that
may happen by installing or using it.

Specifications:
This script operates on the front document of TextEdit. The document should
be formatted as a comma delimite table of data equivalent to the downloadable
CSV files available on individual baseball teams' mlb.com sites.

The only fields used are found in the first eleven:
date, time local Pacific, Y = yes someone is OOT, Provider1, Provider2, Provider3,Provider4,Provider5,Provider6,Provider7, Out of Town = OOT,
e.g.
4/3/2006,7:00 AM,Y,Provider1,Provider2,Provider3,Provider4,Provider5,Provider6,Provider7,Out of Town

*)

tell application "iCal"
	-- Create new calendar and rename with user supplied name
	set NewCalendarName to display dialog "Enter a name for the calendar to be created:" default answer "Out of Town"
	set NewCalendar to make new calendar at end of calendars
	set title of NewCalendar to text returned of NewCalendarName
	set Entry to 1 -- Initialize entry counter
	-- Obtain data from TextEdit
	set Schedule to the text of front document of application "TextEdit"
	set NumberOOTData to number of paragraphs in the text of Schedule
	-- Generate events for all entries in the schedule
	repeat while (Entry is less than NumberOOTData)
		set Entry to (Entry + 1) -- Increment entry counter
		set OOT to every character of paragraph Entry of Schedule -- Decompose the schedule entry into characters for parsing
		set OOTData to {} -- OOTData will hold the pieces of schedule information for each entry -- Parse entire entry
		repeat while (number of items in OOT is greater than 1)
			set temp to "" -- temp will hold the characters making up the piece of information
			-- Parsing looks for tab or end of character list
			repeat while ((the first item of OOT is not equal to ",") and (number of items in OOT is greater than 1))
				set temp to temp & (item 1 of OOT)
				if (number of items in OOT is greater than 1) then
					set OOT to items 2 thru -1 of OOT
				end if
			end repeat
			if (number of items in OOT is greater than 1) then
				set OOT to items 2 thru -1 of OOT
			else if (number of items in OOT is equal to 1) then
				set temp to temp & (item 1 of OOT)
			end if
			set temp to temp as string
			set OOTData to OOTData & temp
		end repeat
		
		-- Reformat date and rename entry data
		set OutOfTown to "Out of Town " as text -- new variable that stores text to differentiate reason off call schedule
		set OOTDate to text (item 1 of OOTData & "," & item 2 of OOTData) as date
		set OOTTime to (item 2 of OOTData)
		set OutOfTownEqualsY to (item 3 of OOTData)
		set Provider1 to (item 4 of OOTData) -- who is out of town
		set Provider2 to (item 5 of OOTData) -- who is out of town
		set Provider3 to (item 6 of OOTData) -- who is out of town
		set Provider4 to (item 7 of OOTData) -- who is out of town
		set Provider5 to (item 8 of OOTData) -- who is out of town
		set Provider6 to (item 9 of OOTData) -- who is out of town
		set Provider7 to (item 10 of OOTData) -- who is out of town
		--set OutOfTownProviders to (Provider1 & space & Provider2 & space & Provider3 & space & Provider4 & space & Provider5 & space & Provider6 & space & Provider7)
		set OutOfTownProviders to (Provider1 & " " & Provider2 & " " & Provider3 & " " & Provider4 & " " & Provider5 & " " & Provider6 & " " & Provider7)
		if (OutOfTownEqualsY is equal to "Y") then -- checks for Y in 3rd column of csv to determine if anyone is OOT
			--set WhoIsOutOfTown to (OutOfTown & space & OutOfTownProviders) -- define combined data as text and puts it into variable WhoIsOutOfTown
			set WhoIsOutOfTown to (OutOfTown & OutOfTownProviders)
			-- it to iCal's event n class summary property (unicode text) below
		else if (OutOfTownEqualsY is not equal to "Y") then
			set WhoIsOutOfTown to (item 3 of OOTData) as text -- sets variable WhoIsOutOfTown to 7:00 AM if nobody is out of town
		end if
		set Venue to (WhoIsOutOfTown) -- (item 3 of OOTData)
		-- Create new event in newly created calendar and apply entry data
		set NewEventEntry to (make new event at the end of events in NewCalendar)
		set summary of NewEventEntry to (WhoIsOutOfTown)
		set location of NewEventEntry to (item 3 of OOTData)
		set start date of NewEventEntry to OOTDate --defines starting time of out of town to 7:00 am because call starts at this time
		set end date of NewEventEntry to (OOTDate + 3 * hours + 30 * minutes) as date
		if (OOTTime is equal to "TBA") then
			set allday event of NewEventEntry to true
		end if
	end repeat
end tell

Model: Dual 1.8 Mhz G5
AppleScript: 1.10.6
Browser: Safari 417.9.3
Operating System: Mac OS X (10.4)

Hi,

Here’s another example of a way to change a list of string to string with delimiter instead of typing all the spaces.

set l to {“a”, “b”, “c”}
set def_tid to AppleScript’s text item delimiters
set AppleScript’s text item delimiters to {space}
set t to l as string
set AppleScript’s text item delimiters to def_tid
return t

gl,

Excellent kel. I knew it went something like that though I would have had to read and experiment for at least a few minutes to figure that out.

As a handler for reuse:


set string1 to "Hey,"
set string2 to "this"
set string3 to "works!"

set newString to my combineTogether({string1, string2, string3})
display dialog newString

on combineTogether(myList)
	set l to myList
	set def_tid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {space}
	set t to l as string
	set AppleScript's text item delimiters to def_tid
	return t
end combineTogether

Nice work, Gary. :slight_smile:

You could make the handler even more versatile by specifying different delimiters in the calling statement - perhaps something like this:

to make_text from l around s
	set d to text item delimiters
	set text item delimiters to s
	set l to l as text
	set text item delimiters to d
	l
end make_text

display dialog (make_text from {"Here", "are", 1, "or", 2, "examples", "of", "making", "text", "from", "a", "list..."} around space)

set pet_list to {"cat", "dog", "hamster", "mouse", "elephant", "budgerigar", "parrot", "vulture", "goldfish", "whale"}
display dialog "Is your pet a " & (make_text from pet_list's items 1 thru -2 around ", ") & " or " & pet_list's item -1 & "?"

set shopping_list to {"bread", "milk", "eggs", "MacBook Pro", "cheese", "poatoes", "Lamborghini Countach", "butter"}
display dialog "My Shopping List:" & return & return & (make_text from shopping_list around return)

very interesting kai.

I wasn’t sure how to iterate a range if items in a list. I’m was using code like “2 thru (count of items in thisList)”. Though “2 thru end” isn’t right; maybe it’s “2 thru 0”?

Lists know how to count backwards, and you can use “last item” as well. The last item is item -1, the next to last is item -2, etc. The range from second item to second to last item is 2 thru -2.

These are called “range references” and there is an excellent article by Nigel Garvey on Range References here

In fact, Gary, your experiments really only lacked a specification of the object class to be listed. Since, in this case, we’re extracting from a list (and we’re not trying to filter items of any particular class), we should ask for ‘items’.

The pet_list example above actually contains an example of this. Here’s how it works:

to make_text from l around s
	set d to text item delimiters
	set text item delimiters to s
	set l to l as text
	set text item delimiters to d
	l
end make_text

set pet_list to {"cat", "dog", "hamster", "mouse", "elephant", "budgerigar", "parrot", "vulture", "goldfish", "whale"}
(* define the list to be referenced and attribute values to it *)

make_text from pet_list's items 1 thru -2 around ", "
(* that's: every item of pet_list, from the first  to the second to last item - separated by a comma/space *)
--> "cat, dog, hamster, mouse, elephant, budgerigar, parrot, vulture, goldfish"

"Is your pet a " & result & " or " & pet_list's item -1 & "?"
(* concatenate the strings: ["Is your pet a "] & [the above text] & [" or "] & [last item of pet_list] & ["?"] *)
--> "Is your pet a cat, dog, hamster, mouse, elephant, budgerigar, parrot, vulture, goldfish or whale?"

(* then just display the result in a dialog *)
display dialog result

I’d echo Adam’s advice that Nigel’s enlightening article is well worth reading. Not only does Mr G. know this stuff inside out, but he puts it across so well. :slight_smile:

Thanks Adam and kai.

Both you, everyone else, and this have been a real great resource.

Gary