Hard-coding a filename

I have (with much struggle and searching on the Net) written a Visual Basic macro and an Applescript to help automate extracting info from a weekly calendar (in Word format) and putting it into iCal as a series of events.

I have the Applescript calling Microsoft Word to run the macro, which concludes by saving a text file to disk containing the date/event info as a tab-delimited file. I then want the continued Applescript to automatically take the text file I saved from Word and process it into iCal events.

My problem is getting the Applescript to let me hard code the pathname/filename in the routine that opens the text file. If I code it like this:

set theFile to choose file with prompt "Choose a tab-delimited text file" of type {"TEXT"}
open for access theFile

it works fine, but I have to pick the file from a file selection dialog. If I use the hard-coded pathname like this:

set theFile to "TiBookX:Users:steve:DL:schedule.txt"
open for access theFile

I get the error message:

iCal got an error: Can't make "TiBookX:Users:steve:DL:schedule.txt" into a file.

How can I make that path work automatically? I have confirmed is the same one that gets passed to the “theFile” variable when using the choose file method.

Hi,

You’re using a string:

“TiBookX:Users:steve:DL:schedule.txt”

instead or a reference:

file “TiBookX:Users:steve:DL:schedule.txt”

The best way I think, is to always use a reference. The following uses a reference:

set desk_path to (path to desktop) as string
set new_ref to (desk_path & “New File”) as file specification
set ref_num to (open for access new_ref)
try
– read/write
close access ref_num
on error
close access ref_num
end try

If the file "New File does not exists, then file “New File” will be created on the desktop. Using strings may add confusion.

gl,

Thanks for the suggestions.

I tried this:

set theFile to file "TiBookX:Users:steve:DL:schedule.txt"
open for access theFile

and got the message “NSCannotCreateScriptCommandError”

I then tried this:

set desk_path to "TiBookX:Users:steve:DL" as string
set new_ref to (desk_path & "schedule.txt") as file specification
set ref_num to (open for access new_ref)
display dialog ref_num & " " & ref_num as text
try
	-- read/write 
	set theData to read ref_num as list using delimiter return
	close access ref_num
on error
	close access ref_num
end try

and got the error message “The variable theData is not defined”.

What am I missing here?

Hi,

Does schedule.txt have any data?

gl,

Hi,

It’s been a while since I fooled around with the ‘as list’ and ‘using delimiter’, but there are quirks to these parameters. To read using these parameters, you must create and write to the file with the open for access and read/write commands and I think you have to leave off the extension. It’s better not using these parameters. To get a list of paragraphs, first read the file. Then, get the paragraphs. Here’s an example that works with the parameters. Run it several times:

set desk_path to (path to desktop) as string
set new_ref to (desk_path & “New File”) as file specification
set ref_num to (open for access new_ref with write permission)
try
– read/write
write “Here’s some text.” & return to ref_num starting at ((get eof ref_num) + 1)
set theData to (read ref_num from 1 as list using delimiter return)
close access ref_num
on error
close access ref_num
beep 2
end try
return theData

gl,

Again, thanks for the comments.

Yes, schedule.txt already contains data; that file was created with the Word macros I use in my first step.

I am pretty sure that the METHOD I am using to read from the file works; remember, I said that if I use the choose file method of opening schedule.txt, the Applescript runs perfectly. I just want some way of hard-coding the filename into the statement that opens the file so I don’t have to pick it from a list.

I don’t want to write to schedule.txt; I want to read from it.

If this helps any, each of the five lines in the file contain a date, followed by a tab, followed by the name of the event that I want put into iCal.

If I was doing this on an Apple II, it would look like this oin Applesoft BASIC:

10 DIM DT$(5), EV$(5)
20 D$ = CHR$(4)
30 PRINT D$"OPEN SCHEDULE.TXT"
40 PRINT D$"READ SCHEDULE.TXT"
50 FOR I = 1 TO 5
60 INPUT DT$(I), EV$(5)
70 NEXT I
80 PRINT D$"CLOSE"
(more program to process the data)

(that is, assuming that the file was comma delimited instead of tab-delimited).

So, the only part of the Applescript program that is not working as I want it to is the PRINT D$“OPEN SCHEDULE.TXT” part. If I choose the file from a dialog box, it works great.

Hi,

Ok. If you just want to read the text file, then something like this:

try
set the_file to alias “TiBookX:Users:steve:DL:schedule.txt”
set the_text to (read the_file)
on error err_mess – the file does not exist
display dialog err_mess
return
end try
set paragraph_list to paragraphs of the_text

You don’t need to ‘open for access’ when just reading.

gl,

Thanks, that did it.

Here is the whole thing, for those who (like myself) were having problems finding a resource on how to 1) read from a text file, and 2) how to create iCal records from Applescript. Some of this I found on the Net, and sorry, I don’t remember the source:

-- Schedule Event script
-- by Steven Weyhrich
-- A modification of a script posted on [url=http://www.blankreb.com/studiosnips.php?ID=17]http://www.blankreb.com/studiosnips.php?ID=17"[/url]
-- and the "Create a Birthday Calendar" script on Apple's web site
-- with help from kel on MacScripter.net

global theBigList, theFile
global theData, theLine

try

This part is necessary only in my particular situation, where I have a Word macro that sucks the pertinent date data off of a Word-based schedule. The macro creates a text file called “schedule…txt”, and stores it at a specific disk location.

The file contains records separated by returns, and within each record are two fields separated by tabs, a date followed by the name of the event:

10/11/04Daily meeting
10/12/04Power nap
etc.

	--Run Schedule macro in Microsoft Word, to prepare the Schedule.txt file
	display dialog "Be SURE that the AHFCC Schedule file is loaded into Microsoft Word and is the ONLY file that Word currently has open." as text
	
	tell application "Microsoft Word"
		do Visual Basic "Schedule"
	end tell

Now, the important part.

	tell application "iCal"
		activate
		set the calendar_name to "Steve Work" as string  -- name of calendar where events are to be placed
		if the calendar_name is "false" then error number -128
		
		-- Read the tab-delimited file, putting the data
		-- into a variable using a return as the delimiter. 
		-- This gives a list in which each item is 
		-- one line, or record, from the file
		
		try
			set theFile to alias "TiBookX:Users:steve:DL:schedule.txt"
			set theData to read theFile as list using delimiter return
		on error err_mess -- the file does not exist
			display dialog err_mess
			return
		end try
		
		set theData to read theFile as list using delimiter return
		
		-- After declaring an empty list, fill it by changing AppleScript's 
		-- text item delimiters to tab and then creating a list called 
		-- theLine with the fields of each record as its items. Then 
		-- that list is copied to our empty list. The text item delimiters 
		-- are then set back to the default.
		
		set theBigList to {}
		
		set text item delimiters of AppleScript to tab
		
		repeat with i from 1 to count of theData
			set theLine to text items of item i of theData
			copy theLine to the end of theBigList
		end repeat
		
		set text item delimiters of AppleScript to ""
		
		-- Put up a barber pole dialog box in iCal that tells
		-- what's going on.

		show progress it with message "Adding events…"
		delay 1
		breathe now it
		
		-- This is the loop that creates each event.
		-- The date (item 1) is extracted from line i in
		-- theBigList, and the event name is extracted
		-- from item 2 of theBigList.

		-- Since for my purposes, ALL of these events
		-- start at 7am and end at 9am, the start and end
		-- dates are created with that in mind.

		repeat with i from 1 to count of theBigList
			set eventSummary to item 2 of item i of theBigList as string
			set event_date to item 1 of item i of theBigList as string
			set e_start to event_date & " 07:00" as string
			set e_end to event_date & " 09:00" as string
			set eventStart to my get_date((e_start))
			set eventEnd to my get_date((e_end))
			set this_calendar to (the first calendar whose title is the calendar_name)
			
			tell this_calendar
				set this_event to make new event at end of events with properties {start date:eventStart, end date:eventEnd, summary:eventSummary}
				(* for adding an alarm to the event
				tell this_event
					make new mail alarm at end of mail alarms with properties {trigger interval:-900}
				end tell
				*)
			end tell
			
		end repeat
		
		show progress it with message "Importing completed."
		delay 2
		
		dismiss progress it
	end tell
	
on error error_message number error_number
	try
		tell application "iCal"
			dismiss progress it
		end tell
	end try
	if the error_number is not -128 then
		display dialog error_message buttons {"OK"} default button 1
	end if
end try

on get_date(this_date)
	return date this_date
end get_date

Again, many thanks for your help, kel!