Voice Help

I’m having problems with my very first script ever. The goal is to get it to say the answer of the calculation, two questions, one why dose speakable items folder return missing value. Two is why won’t it talk at the end. it gives me this error “Can’t make {0.001319477859, " % of disk copacity”} into type «class utf8»."

set KB to 1000
set MB to 1000000 --Handlers for calculations
set GB to 1.0E+9
tell application "System Events" --sets the disks and folders
	set home to home folder
	set lib to (item -8 of home)
	set brain to (item -4 of lib)
	set brain_cap to startup disk
	
end tell
tell application "Finder"
	set brain_size to (physical size of brain) / MB
	set size_brain_cap to (capacity of brain_cap) / GB
	set free_brain_cap to (free space of brain_cap) / GB
end tell
tell application "SpeechRecognitionServer"
	set per to " % "
end tell
set per_brain to brain_size / KB / size_brain_cap * 100
say ((per_brain) & " %")

Hi,

a few notes:

¢ MB is 1024 kB. not 1000 :wink:
¢ calculation of the size can take a moment, during this time the value is missing value
¢ your way to specify the folders is quite inconvenient, better use path to
¢ per_brain at the end is a number but the command say expects a string

set KB to 1024
set MB to 1024 * 1024 --Handlers for calculations
set GB to 1024 * 1024 * 1024

set home to path to home folder
set lib to path to library folder from user domain
set brain to path to speakable items from user domain -- I assume, it's speakable items, my item -4 is something else
set brain_cap to path to startup disk


tell application "Finder"
	repeat until (get physical size of brain) is not missing value
		delay 0.5
	end repeat
	set brain_size to (physical size of brain) / MB
	set size_brain_cap to (capacity of brain_cap) / GB
	set free_brain_cap to (free space of brain_cap) / GB
end tell

tell application "SpeechRecognitionServer"
	set per to " % "
end tell
	
set per_brain to brain_size / KB / size_brain_cap * 100
say ((per_brain as string) & " %")

Thank you very much :smiley:

I would like to say this is the best site ever, I am not a coder, I have never done it before, but I love apple script! I do have a problem I delved into subroutines, although it works. I’m sure this is more of an “else, if” question, I made a subroutine that will calculate the percentage, but I want it to output the answer as a integer if the answer is above zero and a real number if its below zero. Any help would greatly be appreciated!

set KB to 1024
set MB to 1024 * 1024 --Handlers for calculations
set GB to 1024 * 1024 * 1024
on cal_per(x, y) -- Subroutine that calculates percent
	(x / y) * 100
end cal_per

set home to path to home folder
set brain to path to speakable items from user domain
set brain_cap to path to startup disk
set UsrMem to ((do shell script "/usr/sbin/sysctl -n hw.usermem") / 1048576) as integer
--uptime script
set upsys to (do shell script "uptime")
set uptime to words 1 thru 4 of text item 2 of upsys
tell uptime to set msg_uptime to item 1 & " Hour " & space & item 2 & " minutes"


tell application "Finder"
	repeat until (get physical size of brain) is not missing value
		delay 0.5
	end repeat
	set brain_size to (physical size of brain) / MB
	set size_brain_cap to (capacity of brain_cap) / GB
	set free_brain_cap to (free space of brain_cap) / GB
end tell
--Calculations
set per_brain to (cal_per((brain_size / KB), size_brain_cap))
set free_space_per to (cal_per(free_brain_cap, size_brain_cap))


tell application "SpeechRecognitionServer"
	set cname to computer name of (system info) --System information to be read
	set sysv to "Apple's OS " & system version of (system info)
	set cpus to CPU speed of (system info)
	set cput to first word of CPU type of (system info)
	set pmem to physical memory of (system info)
	set ipv4 to IPv4 address of (system info)
	set date1 to date string of (current date) --seperated Date and time to make it sound more natural at least on my Comp
	set time1 to time string of (current date)
	set listt to {cname, sysv, cpus}
	set listt2 to {ipv4, date1, time1}
	--Listen Commands (rep= report)
	--set full_rep to listen for "full report" giving up after 30
	--if full_rep is "full report" then say ((per_brain as string) & " %")
	
	
	--replies
	
end tell
--Going to put the calculations Here
--End of calculations
say ((per_brain as string) & " %")
say ((free_brain_cap as string) & " GB")
--say ((free_space_per as string) & "%")
--say (((UsrMem) as string) & " MB free")
--say ((first item of listt as string) & " ip is " & (first item of listt2))
--say msg_uptime

Hi,

the subroutine can be like this

on integer_real(n)
	if n > 0 then
		return n as integer
	else if n < 0 then
		return n as real
	end if
end integer_real

In your script I don’t understand at all, why you’re talking to application SpeechRecognitionServer.
All the system information can be retrieved without it and can be shortened a bit


.
set {computer name:cname, system version:sysv, CPU speed:cpus, physical memory:pmem, IPv4 address:ipv4} to system info --System information to be read
	set sysv to "Apple's OS " & sysv
	set {date string:date1, time string:time1} to current date --seperated Date and time to make it sound more natural at least on my Comp
	set listt to {cname, sysv, cpus}
	set listt2 to {ipv4, date1, time1}
.

the uptime calculation doesn’t work on my machine, if the uptime is < 1 hour.
I recommend to filter the information with the awk command of the shell,
the print $… command prints out the specified fields

set uptime to words of (do shell script "uptime | awk '{print $3, $4}'")
tell uptime
	if item 2 is "mins" then
		set msg_uptime to item 1 & " minutes"
	else if item 2 is "hr" then
		set msg_uptime to item 1 & " Hour"
	else
		set msg_uptime to item 1 & " Hour " & space & item 2 & " minutes"
	end if
end tell

Sorry about another question, but I’m experimenting with the tutorials on this site, i’m trying to make speech scripts I use the speech stuff a lot on my Mac, so this script that I am working on will be a system info script with voice interaction. so here is what i got, i need help on the listen for parts I want it to be constantly running with a listen for continuously command.

set sysPro to do shell script "system_profiler" as string
set freeMem to (((do shell script "/usr/sbin/sysctl -n hw.usermem") / 1048576) as integer) & " MB free"
set modName to paragraph 5 of sysPro & " inch"
set cpuType to paragraph 7 of sysPro
set cpuSpeed to paragraph 8 of sysPro
set numCPU to paragraph 9 of sysPro
set l2Cache to paragraph 10 of sysPro
set memRam to paragraph 11 of sysPro
set busSpeed to paragraph 12 of sysPro
set bootRom to paragraph 13 of sysPro
set smcVer to paragraph 14 of sysPro
set serNum to paragraph 15 of sysPro
set sysv to "Apple's OS " & system version of (system info)
set inIP to "This computer's internal IP address is, " & IPv4 address of (system info)
set exIP to "This computer's external IP address is, " & word 25 of (do shell script "curl http://checkip.dyndns.org/ | grep 'Current IP Address'") as string
set cname to " The system's name is " & computer name of (system info)
set uptime to words of (do shell script "uptime | awk '{print $3, $4}'")
tell uptime
	if item 2 is "mins" then
		set msg_uptime to item 1 & " minutes"
	else if item 2 is "hr" then
		set msg_uptime to item 1 & " Hour"
	else if item 1 is equal to "1" then
		set msg_uptime to item 1 & " Hour " & space & item 2 & " minutes"
	else
		set msg_uptime to item 1 & " Hour's " & space & item 2 & " minutes"
	end if
end tell
set proReport to cpuType & ", " & numCPU & ", " & cpuSpeed & ", " & l2Cache & ", " & busSpeed as string
set sysReport to modName & ", " & serNum & ", " & smcVer & ", " & bootRom & ", running, " & sysv & ",  " & cname
set memReport to memRam & ", " & freeMem & ", on for, " & msg_uptime
set ipReport to inIP & ", " & exIP
--Folders and there sizes
on cal_per(x, y) -- Subroutine that calculates percent
	if (x / y) * 100 is less than or equal to 0.1 then
		return ((x / y) * 100 as real) & " %" as string
	else
		return ((x / y) * 100 as integer) & " %" as string
	end if
end cal_per
-- Conversion table by Nigel Garvey
on convertByteSize(byteSize)
	if byteSize is greater than or equal to 1.099511627776E+12 then -- Terabytes (2 ^ 40) 
		((byteSize / 1.099511627776E+12 div 0.01 / 100.0) as string) & " TB"
	else if byteSize is greater than or equal to 1.073741824E+9 then -- Gigabytes (2 ^ 30) 
		((byteSize / 1.073741824E+9 div 0.01 / 100.0) as string) & " GB"
	else if byteSize is greater than or equal to 1048576 then -- Megabytes (2 ^ 20) 
		((byteSize / 1048576 div 0.01 / 100.0) as string) & " MB"
	else if byteSize is greater than or equal to 1024 then -- Kilobytes (2 ^ 10) 
		((byteSize div 1024) as string) & " K"
	else
		(byteSize as string) & " bytes"
	end if
end convertByteSize
tell application "Finder"
	set brain to path to speakable items from user domain --speakable items folder
	repeat until (physical size of brain) is not missing value
		delay 0.5
	end repeat
	set brain_size to get physical size of brain
	set homeFolder to path to home folder --Home folder 
	repeat until (physical size of homeFolder) is not missing value
		delay 0.5
	end repeat
	set home_size to physical size of homeFolder
	set deskFolder to path to desktop folder
	repeat until (physical size of deskFolder) is not missing value
		delay 0.5
	end repeat
	set deskFolder_size to physical size of deskFolder
	set docFolder to path to documents folder
	repeat until (physical size of docFolder) is not missing value
		delay 0.5
	end repeat
	set docFolder_size to physical size of docFolder
	set movFolder to path to movies folder
	repeat until (physical size of movFolder) is not missing value
		delay 0.5
	end repeat
	set movFolder_size to physical size of movFolder
	set musFolder to path to music folder
	repeat until (physical size of musFolder) is not missing value
		delay 0.5
	end repeat
	set musFolder_size to physical size of musFolder
	set picFolder to path to pictures folder
	repeat until (physical size of picFolder) is not missing value
		delay 0.5
	end repeat
	set picFolder_size to physical size of picFolder
	set appFolder to path to applications folder
	repeat until (physical size of appFolder) is not missing value
		delay 0.5
	end repeat
	set appFolder_size to physical size of appFolder
	set utFolder to path to utilities folder
	repeat until (physical size of utFolder) is not missing value
		delay 0.5
	end repeat
	set utFolder_size to physical size of utFolder
	set mainDisk to path to startup disk --Disks and sizes
	repeat until (capacity of mainDisk) is not missing value
		delay 0.5
	end repeat
	set mainDisk_cap to (capacity of mainDisk)
	repeat until (free space of mainDisk) is not missing value
		delay 0.5
	end repeat
	set mainDisk_free to (free space of mainDisk)
end tell
tell application "SpeechRecognitionServer"
	set cmdDic to {"Report", "get size of", "get location of", "open"}
	set sysDic to {"CPU Type", "CPU Speed", "Number of CPU's", "system Version", "Free memory", "Model", "Model type", "L2 cache", "Memory", "Total Memory", "Bus speed", "Boot Rom", "SMC Version", "Serial Number", "System Version", "OS Version", "IP", "Internal IP", "External IP", "System name", "Computer name", "up time", "Time on"}
	set folderDic to {"speakable items folder", "home folder", "desktop folder", "Document Folder", "My Documents", "Movie Folder", "My Movies", "Music Folder", "My Music", "Picture Folder", "My Pictures", "Applications", "My Applications", "Utilities", "Utilities Folder", "Main Disk", "Hard Disk", "Hard Drive"}
	set singularComm to {"just", "only"}
	listen continuously for cmdDic
	if cmd is "Report" then say "it works"
	
	stop listening for identifier "stop"
	
	
	
	
	
end tell