script syntax questions

Hello, I have assembled a little startup script that runs when I boot up. It says hello, tells me the date and time, checks my email and tells me how many messages I have, and launches programs I specified. What I am trying to do now is get it to not announce the seconds when it calls out the time and I am trying to get the programs it launches to start docked. I am posting the entire script. Thanks in advance --Jake

l


--say time commands
-- need to exclude seconds
set theDate to current date
set theDay to weekday of theDate
set theTime to time of theDate

if the theTime > 0 and the theTime < 43200 then say ¬
	"Good morning Jake! It's " & theDay & "."
if the theTime > 43199 and the theTime < 57600 then say ¬
	"Good afternoon Jake!"
if the theTime > 57599 and the theTime < 64800 then say ¬
	"Good evening Jake!"
if the theTime > 64799 and the theTime < 86400 then say ¬
	"It's " & theDay & " night."
say "the time is"
say (time string of (current date))


tell application "Mail"
	check for new mail
	
	
	set unreadCount to unread count of inbox
	
	
	if unreadCount is equal to 0 then say ¬
		"You have no new messages!"
	if unreadCount is equal to 1 then say ¬
		"You have a new message!"
	if unreadCount is greater than 1 then say ¬
		"You have " & unreadCount & " new messages!"
	delay 2
	
	
	
	
	
	-- app open commands
	
	tell application "Terminal" to activate
	--tell application "Terminal" to close window 0 This is my attempt to dock this app on startup, it returns error 4
	
	
	
	
	tell application "Safari" to activate
	--tell application "Safari" to close window 0
	
	
	
	
	tell application "Firefox" to activate
	--tell application "Firefox" to close window 0
	
	
	
		
	
	
	
	-- lets me know it finished without errors
	say "script end"
end tell


Browser: Firefox 2.0.0.3
Operating System: Mac OS X (10.4)

Hi Jake,

very nice script :slight_smile:
Try this

--say time commands
-- need to exclude seconds

tell (current date) to set {theDay, theTime, theHours, theMinutes} to {its weekday, time, its hours, its minutes}

if the theTime > 0 and the theTime < 43200 then say ¬
	"Good morning Jake! It's " & theDay & "."
if the theTime > 43199 and the theTime < 57600 then say ¬
	"Good afternoon Jake!"
if the theTime > 57599 and the theTime < 64800 then say ¬
	"Good evening Jake!"
if the theTime > 64799 and the theTime < 86400 then say ¬
	"It's " & theDay & " night."
if theMinutes < 10 then set theMinutes to "o" & theMinutes as string
say "the time is " & theHours & space & theMinutes

tell application "Mail"
	check for new mail
	set unreadCount to unread count of inbox
	if unreadCount is equal to 0 then say ¬
		"You have no new messages!"
	if unreadCount is equal to 1 then say ¬
		"You have a new message!"
	if unreadCount is greater than 1 then say ¬
		"You have " & unreadCount & " new messages!"
	delay 2
end tell
-- app open commands

set appList to {"Terminal", "Safari"}
repeat with oneapp in appList
	tell application oneapp
		activate
		if exists window 1 then close window 1
	end tell
end repeat

-- Firefox is a sprecial case, because it's lousy scriptable
activate application "Firefox"
tell application "System Events" to tell (get 1st process whose title is "Firefox")
	if exists window 1 then keystroke "w" using command down
end tell

-- lets me know it finished without errors
say "script end"

Alternatively:

set {weekday:theDay, time:theTime, hours:theHours, minutes:theMinutes} to (current date)

if theTime ≥ 0 and theTime ≤ 43199 then
	say "Good morning Jake! It's " & theDay & "."
else if theTime ≥ 43200 and theTime ≤ 57599 then
	say "Good afternoon Jake!"
else if theTime ≥ 57600 and the theTime ≤ 64799 then
	say "Good evening Jake!"
else
	say "It's " & theDay & " night."
end if

-- Handle minutes that are less than 10
set theMinutes to text -2 thru -1 of ("o" & theMinutes)
if theMinutes is "o0" then set theMinutes to "o'clock"

-- 24-hour clock
say "The time is " & theHours & " " & theMinutes

tell application "Mail"
	launch
	check for new mail
	set unreadCount to unread count of inbox
	
	if unreadCount = 0 then
		say "You have no new messages!"
	else if unreadCount = 1 then
		say "You have a new message!"
	else
		say "You have " & unreadCount & " new messages!"
	end if
end tell

delay 2

{"Terminal", "Safari"}
repeat with thisItem in result
	tell application thisItem
		launch
		try
			-- This doesn't work for Firefox, because it's dictionary doesn't include the Standard Suite. The try block will allow you to handle/ignore any errors.
			if exists window 1 then close window 1
		end try
	end tell
end repeat

-- Handle Firefox
activate application "Firefox"
tell application "System Events" to tell (first process whose title is "Firefox")
	if exists window 1 then keystroke "w" using command down
end tell
activate application "Finder" -- Bring the Finder to the front, instead of Firefox

say "script end"

Hi, Jake.

Besides Stefan’s suggestions (and, I see as I log back on, to augment Bruce’s), you might be interested in the ‘if . else if .’ construction for testing the various times and unreadCount. When it gets a hit, it performs the appropriate action, then skips any remaining tests and jumps straight to the ‘end if’. Also, since each successive test is only done if the previous one fails, we can assume that the previous possibilities have been eliminated without having to test for them again:

tell (current date) to set {theDay, theTime, theHours, theMinutes} to {its weekday, time, its hours, its minutes}

if the theTime < 43200 then -- We know the time is in the range 0 to 86399.
	say "Good morning Jake! It's " & theDay & "."
else if the theTime < 57600 then -- We've already eliminated times before 43200.
	say "Good afternoon Jake!"
else if the theTime < 64800 then -- We've already eliminated times before 57600.
	say "Good evening Jake!"
else -- If we've reached here, the time must be between 64800 and 86399.
	say "It's " & theDay & " night."
end if

Of course, since Stefan’s version of the script gets the hours for you, you could use those instead, which would make the script easier to understand:

tell (current date) to set {theDay, theHours, theMinutes} to {its weekday, its hours, its minutes}

if theHours < 12 then
	say "Good morning Jake! It's " & theDay & "."
else if theHours < 16 then
	say "Good afternoon Jake!"
else if the theHours < 18 then
	say "Good evening Jake!"
else
	say "It's " & theDay & " night."
end if

Wow, thanks for the quick response, it works like a charm now. I have some questions.

1."
– Firefox is a sprecial case, because it’s lousy scriptable
activate application “Firefox”
tell application “System Events” to tell (get 1st process whose title is “Firefox”)
if exists window 1 then keystroke “w” using command down
end tell

If I am reading this correctly you are using system events to pass actual keystrokes to Firefox. Is that correct?

2.f the theTime < 43200 then – We know the time is in the range 0 to 86399.
say "Good morning Jake! It’s " & theDay & “.”
else if the theTime < 57600 then – We’ve already eliminated times before 43200.
say “Good afternoon Jake!”
else if the theTime < 64800 then – We’ve already eliminated times before 57600.
say “Good evening Jake!”
else – If we’ve reached here, the time must be between 64800 and 86399.
say “It’s " & theDay & " night.”
end if

Does this make the script execute faster? It does seem to me to be cleaner logic.

  1. Is there a way to trigger parts of this script when I bring the computer off standby? I would like it to report the time and check the email for me.

Once again thanks for the quick response.

exactly

Yes, it does because there are a few less comparisons

Only with tools e.g. Sleepwatcher

Is Firefox just not apple script friendly? Is Camino any more scriptable?

Camino is slightly scriptable. It can “do javascript”, “get URL”, “open URL”, and get the properties of a browser window. Tabs are not directly reachable from AS, but Camino responds to GUI scripting them.

I just updated Camino to v1.0.4 and it still won’t read the dictionary for Camino. Anybody else experience this?

Thanks,

I would think that Camino would know how to add dictionaries as Netscape does or is Netscape completely different from Camino.