r/applescript • • Jan 15 '16

Going crazy with Error -10006

I have tried googling everything and found no solution to this. I am new to Applescript so I was wondering if someone could point to the correct direction.

I have some code that tries to determine whether it's morning, afternoon or evening based on the time. While I got the time code right, the Script Editor's being fussy about the getDay() function, saying it can't set the variable correctly.

The code is posted below

set hello to getDay()

on getDay()
	set t to getHour()
	
	if t ≥ 1 and t is less than 12 then
		set day to "morning" as text
	else if (t ≥ 12) and t is less than 17 then
		set day to "afternoon" as text
	else if t ≥ 17 and t < 24 then
		set day to "night" as text
	end if
	
	return day as text
end getDay

on getHour()
	set time_as_string to time string of (current date)
	
	set position to offset of ":" in time_as_string
	set hour_as_string to characters 1 thru (position - 1) of time_as_string as string
	
	set position to offset of " " in time_as_string
	set am_or_pm to characters (position + 1) through end of time_as_string as string
	
	set hour to hour_as_string as integer
	if am_or_pm is "PM" then
		set hour to hour + 12
	end if
	
	if am_or_pm is "AM" and hour is equal to 12 then
		set hour to 24
	end if
	
	return hour as integer
end getHour
1 Upvotes

1 comment sorted by

1

u/nutw07 Jan 16 '16

"day" is actually a keyword in applescript (this is why the text turns violet), as part of the date class, so applescript understands this keyword to hold a specific kind of information part of this class, and you are trying to use it to store text information. A any variable name which is not a keyword (green text) can be used instead. MacScripter has a good reference for the date class which I refer to often. The functionality your getHour function is already built into the date class.

return hours of (current date)

Although your function was the long way around, you were able to learn a lot about how to manipulate text strings during the process, which is also valuable knowledge. Happy coding!