Class Calendar

Get help using and writing Nisus Writer Pro macros.
Post Reply
User avatar
midwinter
Posts: 333
Joined: 2004-09-09 18:07:11
Location: Utah
Contact:

Class Calendar

Post by midwinter » 2009-08-23 20:36:07

Every semester, I create a syllabus for my classes that includes a table with a list of dates and assignments for those dates. I have always done this by hand. Would it be possible to have a macro spit out a list of days/dates within a certain range (for MWF and T/Th) so I don't have to do this by hand? Something like

Monday 8/24
Wednesday 8/26

or

Tuesday 8/25
Thursday 8/27

Thanks in advance!

Kino
Posts: 400
Joined: 2008-05-17 04:02:32

Re: Class Calendar

Post by Kino » 2009-08-24 23:10:54

Does this work as you expected? It will ask you to specify the first date, the last date and days of week and insert a table at the current insertion point (in a new document if there is no open document).

This macro does not check the validity of date format and that of a date itself. So you may get an unexpected result or an error if you give a wrong one. E.g. "2009-4-31" (wrong date in the valid format).

Code: Select all

 Require Pro Version 1.3

$namesOfDay = Array.new
$namesOfDay[0] = Date.nameOfWeekday 7
$i = 1
while $i < 7
	$namesOfDay[$i] = Date.nameOfWeekday $i
	$i += 1
end

$indexToName = Hash.new
foreach $i, $n in $namesOfDay
	$indexToName{$i} = $n
end

$now = Date.now
$today = $now.year & '-'
$today &= $now.month & '-'
$today &= $now.day

$date1 = Prompt Input 'From...', '', '', $today
$date2 = Prompt Input 'To...', '', '', $date1
$date1 = $date1.split '-'
$date2 = $date2.split '-'

$checked = Prompt Checkboxes 'Check names of day', '', '', $namesOfDay
if ! $checked.count
	exit 'No item checked, exit...'
end

$checkedNames = Hash.new
foreach $name in $checked
	$i = $namesOfDay.indexOfValue $name
	$checkedNames{$i} = 1
end

$output = ''

Set Exported Perl Variables 'date1', 'date2', 'checkedNames', 'indexToName', 'output'
begin Perl
	use Time::Local;
	$i = timelocal (0, 0, 12, $date1[2], $date1[1]-1, $date1[0]);
	$j = timelocal (0, 0, 12, $date2[2], $date2[1]-1, $date2[0]);
	while ($i <= $j) {
		@lt = localtime($i);
		if (defined $checkedNames{$lt[6]}) {
			$output .= sprintf ("%s\t%01d/%01d\t\n", $indexToName{$lt[6]}, $lt[4]+1, $lt[3]);
		}
		$i += 86400;
	}
end

$doc = Document.active
if $doc == undefined  # if no document is open...
	Document.new
end

Insert Text $output
Menu ':Table:Convert to Table'
This macro uses Perl because Nisus Macro language does not have a command for generating an arbitrary date object and properties such as year, month, day are Read-only. I hope these limitations will be removed in a future version. Also I'd like to have a command such as

Code: Select all

$dateObject = Prompt Date 'Message', 'Detail', 'Button', $defaultDateObject
which will prompt a dialog box having input fields with stepper for date and time, something like those in Date & Time system preferences pane.

User avatar
midwinter
Posts: 333
Joined: 2004-09-09 18:07:11
Location: Utah
Contact:

Re: Class Calendar

Post by midwinter » 2009-09-27 13:19:55

I neglected to respond, but yes yes yes! This is perfect!

Post Reply