Please comment and leave ideas :)

Ok, so I saw a bunch of posts with alarms that went off and gave you info. I liked how it gave you readouts, so I made one without the alarm, just a big readout. Please comment any ideas that you have for it to give you info on, thanks a ton! :smiley:
PS: Sorry if the applescript doesnt come out right or something, first post with applescript in it

-- You dont need this next line if you dont have snow leopard OS X
do shell script "killall SpeechRecognitionServer"
-- Sets the time
on thetime()
	-- hours
	set timeStr to time string of (current date)
	set Pos to offset of ":" in timeStr
	set theHour to characters 1 thru (Pos - 1) of timeStr as string
	set timeStr to characters (Pos + 1) through end of timeStr as string
	
	-- minutes
	set Pos to offset of ":" in timeStr
	set theMin to characters 1 thru (Pos - 1) of timeStr as string
	set timeStr to characters (Pos + 1) through end of timeStr as string
	
	
	return (theHour & ":" & theMin & "") as string
end thetime
-- Pauses yor music (add any other music apps you use to ensure its quiet)
tell application "iTunes"
	pause
end tell
set TID to AppleScript's text item delimiters
-- Gets the temperature in fahrenheit and the current outlook (go to the site and search your location and use that url, but keep the curl in front)
try
	set webaddress to do shell script "curl [url=http://i.wund.com/auto/iphone/PA/Oxford.html]http://i.wund.com/auto/iphone/PA/Oxford.html"[/url]
	--Note: The following is not my code, I borrowed it from someone else, Im not good with using delimiters to split objects in html, I prefer to use offset to find a 'marker' I found in the html source for the website
	set AppleScript's text item delimiters to "<span class=\"nowrap\"><b>"
	set text1 to text item 3 of webaddress
	set AppleScript's text item delimiters to "</b>"
	set tempC to text item 1 of text1
	set temp to (((9 / 5) * (tempC as number)) + 32) as string
	set temp to round (temp)
	--And the outlook for the day
	set AppleScript's text item delimiters to "<div>"
	set text2 to text item 3 of webaddress
	set AppleScript's text item delimiters to "</div>"
	set outlook to text item 1 of text2
	set AppleScript's text item delimiters to TID
	--The temperature is now stored as "temp" and the outlook as "outlook" insha'allah. If not:
on error
	set outlook to "unknown"
	set temp to "unknown"
end try

--DATE
set d to (date string of (current date))

-- Sets options for greetings
set d to (date string of (current date))
set theOptions to {"Hello!", "Hey Alex!", "Your information:", "Current news:", "Sup.", "Yo."}
set theChoice to some item of theOptions
say theChoice displaying theChoice with waiting until completion
delay 1
-- Day of week/time
say "Its " & word 1 of d
say "and its "
say thetime()
delay 1
--Says the outlook/temperature
say "The outlook for the weather in Oxford today is. " & outlook displaying "The outlook for the weather in Oxford today is. " & outlook with waiting until completion
delay 1
say "The current temperature is. " & temp
if temp is not equal to "unknown" then
	say "degrees fahrenheit" displaying "degrees fahrenheit" with waiting until completion
end if
-- If you want, change the following to different temps depending on what you think is hot/cold
if temp is not equal to "unknown" then
	if temp is greater than 85 then say "It's hot out side!"
	if temp is less than or equal to 85 and temp is greater than 68 then say "It's warm out side."
	if temp is less than or equal to 68 then say "It's cold out side!"
end if
delay 1
-- Lets you say a day and finds it on a website and gives you weather read out (see earlier section for info on how to change location)
tell application "SpeechRecognitionServer"
	try
		set themAnswer to listen for {"tomorrow", "tomorrow night", "monday", "monday night", "tuesday", "tuesday night", "wednesday", "wednesday night", "thursday", "thursday night", "friday", "friday night", "saturday", "saturday night", "sunday", "sunday night", "today", "now", "tonight", "this afternoon", "no", "no thank you", "im fine", "im good", "none"} with prompt "Wood u like to hear a detailed report for a day this week?" giving up after 30
		-- Note: The only reason it says "wood u" is cause the voice sounded funny so i changed it to how it seems like you should spell "would you"
	on error
		say "No report found."
	end try
end tell
if themAnswer is "now" then set themAnswer to ""
if themAnswer is "tomorrow" then
	set cdow to word 1 of d
	if cdow is "Monday" then set themAnswer to "Tuesday"
	if cdow is "Tuesday" then set themAnswer to "Wednesday"
	if cdow is "Wednesday" then set themAnswer to "Thursday"
	if cdow is "Thursday" then set themAnswer to "Friday"
	if cdow is "Friday" then set themAnswer to "Saturday"
	if cdow is "Saturday" then set themAnswer to "Sunday"
	if cdow is "Sunday" then set themAnswer to "Monday"
end if
if themAnswer is "tomorrow night" then
	set cdow to word 1 of d
	if cdow is "Monday" then set themAnswer to "Tuesday night"
	if cdow is "Tuesday" then set themAnswer to "Wednesday night"
	if cdow is "Wednesday" then set themAnswer to "Thursday night"
	if cdow is "Thursday" then set themAnswer to "Friday night"
	if cdow is "Friday" then set themAnswer to "Saturday night"
	if cdow is "Saturday" then set themAnswer to "Sunday night"
	if cdow is "Sunday" then set themAnswer to "Monday night"
end if
-- Reads the weather for a certain day out loud (see choices above)
if themAnswer is "no" or themAnswer is "no thank you" or themAnswer is "im fine" or themAnswer is "im good" or themAnswer is "none" then
	say "Ok."
else
	if themAnswer is "today" then set themAnswer to "this afternoon"
	try
		set theResponse to themAnswer & "</span><br />"
		set text10 to offset of theResponse as string in webaddress
		set todayF to text text10 thru end of webaddress
		set thisAft to text ((offset of "<div>" in todayF) + 5) thru ((offset of "</div>" in todayF) - 1) of todayF
		say thisAft displaying thisAft with waiting until completion
	on error
		say "I cant find a detailed report."
	end try
end if
-- Reads the top article from business week (technology) aloud
try
	set webaddress2 to do shell script "curl [url=http://www.businessweek.com/technology/]http://www.businessweek.com/technology/"[/url]
	set text2end to text (offset of "<li class=\"first clearfix\">" in webaddress2) thru end of webaddress2
	set text3end to text ((offset of "title=" in text2end) + 7) thru end of text2end
	set text2 to text 1 thru ((offset of "\"" in text3end) - 1) of text3end
	say "Latest tech: " & text2
on error
	say "No tech found"
end try
-- Tells how many events you have today
tell (current date) to set today to its date string
set counter to 0
tell application "iCal"
	
	set e_vent to every event in calendar "Personal"
	repeat with i from 1 to number of items in e_vent
		set this_item to item i of e_vent
		set startdate to start date of this_item
		--set starttime to start of this_item
		set startdate to date string of startdate
		if startdate is today then
			set counter to counter + 1
		end if
	end repeat
end tell
if counter = 1 then
	say "You have " & (counter as string) & " event today. It is:"
else
	if counter = 0 then
		say "You have no events today."
	else
		say "You have " & (counter as string) & " events today. They are:"
	end if
end if
set oldCounter to counter
-- If you have events today, says them.
if not counter = 0 then
	tell application "iCal"
		set e_vent to every event in calendar "Personal"
		repeat with i from 1 to number of items in e_vent
			set this_item to item i of e_vent
			set startdate to start date of this_item
			--set starttime to start of this_item
			set startdate to date string of startdate
			if startdate is today then
				set counter to (counter - 1)
				if counter = 0 and not oldCounter = 1 then
					say " and " & (get summary of this_item) & "."
				else
					if counter = 0 and oldCounter = 1 then
						say (get summary of this_item) & "."
					else
						say (get summary of this_item) & ","
					end if
				end if
			end if
		end repeat
	end tell
end if
-- List of possible salutations
set theOptions to {"Goodbye!", "Bye Alex!", "See ya!", "Have fun today!", "Bye!", "Adios!"}
set theChoice to some item of theOptions
delay 1
say theChoice displaying theChoice with waiting until completion
-- Sets the speech server up again for use (if you have the messed up snow leopard)
try
	tell application "SpeechRecognitionServer"
		listen for {} with prompt "" giving up after 1.0E-27
	end tell
end try

I just started looking at your code. Here’s the first thing I saw… in thetime() handler. You are getting ā€œthe charactersā€ of the time string which turns the string into a list of individual characters, then you coerce that list back to a string. Thats inefficient. You can use the keyword ā€œtextā€ instead of ā€œcharactersā€ to get the string directly. Also, the last ā€œset timeStrā€ line isn’t needed. Finally, the ā€œreturnā€ line is already a bunch of strings so you don’t need to coerce it to a string. In general you are making a lot of unnecessary coercions between lists and strings so that handler can be more efficient like this…

on thetime()
	-- hours
	set timeStr to time string of (current date)
	set Pos to offset of ":" in timeStr
	set theHour to text 1 thru (Pos - 1) of timeStr
	set timeStr to text (Pos + 1) through end of timeStr
	
	-- minutes
	set Pos to offset of ":" in timeStr
	set theMin to text 1 thru (Pos - 1) of timeStr
	
	return theHour & ":" & theMin
end thetime

With that said, here’s more compact code which can do the same thing…

on thetime()
	set t to current date
	set theHour to hours of t
	set theMin to minutes of t
	return (theHour as text) & ":" & (theMin as text)
end thetime

Thanks for the input! I got the beginning from someone else, and before I really understood it so I decided just to parse everything, but now I see that. Thanks so much! Now im gonna go back through the begining and look for any more ā€œdouble-parsesā€

Also, I have a question about another piece of code im thinking about adding:

set Searcha to "<span class=\"byuser\">by lemuralex13</span>
							</div>
						</div>
					</td>
					<td class=\"tc2\">"
set webaddress3 to do shell script "curl [url=http://macscripter.net/viewforum.php?id=2]http://macscripter.net/viewforum.php?id=2"[/url]
(offset of Searcha in webaddress3)

It’s supposed to search the html code for Searcha, but it cant find it (it returns 0). When I go into the source and take out the backslashes and use cmd + f, its there twice (I have two posts). The purpose of the code is to say the total of all my posts. I have more code which adds it up, but this is the part causing trouble. Is the reason it cant find it inside the html code because I have to use backslashes to put quotes in a string? If not, what is the reason? Thanks again!

Your code is very hard to follow and that leads me to my next comment. The code is hard to follow because it does many things… therefore you must learn to keep your code organized. This makes it easier for others to read your code and easier for you when you must make adjustments in the future. An approach that people use is to separate the code into tasks, put each task into a separate handler, and then just call the handlers from the top of the script. It makes it easy to see what is going on in the code.

Here’s an example. I took the first part of your script and broke it down this way. I think it will be easy for you to see what I did… and that’s the point. If you look at the details in the handlers you’ll see I cleaned up some of the parts too.

pauseMusic()
sayGreeting()
delay 1
sayTime()
delay 1
sayOutlookAndTemp()



(*====================== SUBROUTINES ======================*)
on sayOutlookAndTemp()
	set {temp, outlook} to my getCurrentTempAndDailyOutlook()
	set outLookText to "The outlook for the weather in Oxford today is. " & outlook
	set tempText to "The current temperature is. " & temp
	if temp is not equal to "unknown" then
		if temp is greater than "85" then
			set t to "It's hot out side!"
		else if temp is less than or equal to "85" and temp is greater than "68" then
			set t to "It's warm out side."
		else
			set t to "It's cold out side!"
		end if
		set tempText to tempText & " degrees fahrenheit and " & t
	end if
	say outLookText displaying outLookText with waiting until completion
	delay 1
	say tempText displaying tempText with waiting until completion
end sayOutlookAndTemp

on sayGreeting()
	-- You dont need this next line if you dont have snow leopard OS X
	set sysver to system version of (system info)
	if sysver is greater than or equal to "10.6" then
		try
			do shell script "killall SpeechRecognitionServer"
		end try
	end if
	set theGreeting to my getGreeting()
	say theGreeting displaying theGreeting with waiting until completion
end sayGreeting

on sayTime()
	set {thetime, theWeekday} to my dateStrings()
	set theSentence to "It's " & theWeekday & " and its " & thetime
	say theSentence displaying theSentence with waiting until completion
end sayTime

on dateStrings()
	set t to current date
	set theHour to hours of t
	set theMin to minutes of t
	set timeString to (theHour as text) & ":" & (theMin as text)
	set theWeekday to weekday of t as text
	return {timeString, theWeekday}
end dateStrings

on getGreeting()
	set theGreetings to {"Hello!", "Hey Alex!", "Your information:", "Current news:", "Sup.", "Yo."}
	return some item of theGreetings
end getGreeting

on pauseMusic()
	-- Pauses your music (add any other music apps you use to ensure its quiet)
	tell application "System Events" to set runningProcesses to name of processes
	if "iTunes" is in runningProcesses then tell application "iTunes" to pause
end pauseMusic

on getCurrentTempAndDailyOutlook()
	set TID to AppleScript's text item delimiters
	-- Gets the temperature in fahrenheit and the current outlook (go to the site and search your location and use that url, but keep the curl in front)
	try
		set webaddress to do shell script "curl [url=http://i.wund.com/auto/iphone/PA/Oxford.html]http://i.wund.com/auto/iphone/PA/Oxford.html"[/url]
		--Note: The following is not my code, I borrowed it from someone else, Im not good with using delimiters to split objects in html, I prefer to use offset to find a 'marker' I found in the html source for the website
		set AppleScript's text item delimiters to "<span class=\"nowrap\"><b>"
		set text1 to text item 3 of webaddress
		set AppleScript's text item delimiters to "</b>"
		set tempC to text item 1 of text1
		set temp to (((9 / 5) * (tempC as number)) + 32) as string
		set temp to round (temp)
		--And the outlook for the day
		set AppleScript's text item delimiters to "<div>"
		set text2 to text item 3 of webaddress
		set AppleScript's text item delimiters to "</div>"
		set outlook to text item 1 of text2
		set AppleScript's text item delimiters to TID
		--The temperature is now stored as "temp" and the outlook as "outlook" insha'allah. If not:
	on error
		set outlook to "unknown"
		set temp to "unknown"
	end try
	return {temp as text, outlook}
end getCurrentTempAndDailyOutlook

I don’t think that’s the reason. I think it’s the line endings. As you may know, there’s several types of line endings for text… macs use the carriage return character, unix uses the line feed character, and windows uses a combination of the two CRLF.

So in your Searcha variable you’re probably using the carriage return character and the html is coming back with the line feed character… or vise versa… therefore they don’t match.

Here’s a fix for that code… the do shell script command has ā€œwithout altering line endingsā€ which will return the code to you unaltered. In general the do shell script command changes the line endings automatically when it passes text from the command line to applescript, so you have to stop that in this case… so this works for you…

set Searcha to "<span class=\"byuser\">by lemuralex13</span>
							</div>
						</div>
					</td>
					<td class=\"tc2\">"
set webaddress3 to do shell script "curl [url=http://macscripter.net/viewforum.php?id=2]http://macscripter.net/viewforum.php?id=2"[/url] without altering line endings
(offset of Searcha in webaddress3)

Ok, I think that works, I’ll post the script with the categories broken down in a little bit, thanks again, lol :smiley:
EDIT: Nevermind, its still returning 0, its not finding it

It’s working for me. I click the ā€œOpen this Scriplet in your Editor:ā€ link next to my posted code, and it runs fine. So the problem is as I explained. Somehow you’re changing things probably when you copy/paste.

Hmm, I guess so, that worked, thanks

Ok, to find out how many posts you have (only in this topic):

countPosts()
on countPosts()
	set Searcha to "<span class=\"byuser\">by InsertUsernameHere</span>
							</div>
						</div>
					</td>
					<td class=\"tc2\">"
	set total to 0
	try
		set webaddress3 to do shell script "curl [url=http://macscripter.net/viewforum.php?id=2]http://macscripter.net/viewforum.php?id=2"[/url] without altering line endings
		repeat until (offset of Searcha in webaddress3) = 0
			set webaddress3 to text ((offset of Searcha in webaddress3) + 107) thru end of webaddress3
			set text3end to text 1 thru (offset of ">" in webaddress3) of webaddress3
			set total to total + 1
		end repeat
		if total = 1 then
			say "You have one post on Mac Scripter"
		else
			say "You have " & (total as string) & " posts on Mac scripter"
		end if
	on error
		say "No posts found on Mac scripter"
	end try
end countPosts

:slight_smile: Ok, I cleared up some stuff and I think im gonna post my new code (DEFINATELY not mt final code)
So is there any other things you think it should tell me too? I wanna do mail, but my mail doesnt work

Please put some ides here other people! It would be highly appreciated! :slight_smile:

Anyone? :frowning: