Hi, I was just wondering whether my code could be tidied up a bit more and streamlined…
--
-- JarvisAppDelegate.applescript
-- Jarvis
--
-- Created by Seb on 18/07/2010.
-- Copyright 2010 __MyCompanyName__. All rights reserved.
--
script JarvisAppDelegate
--Inheritance
property parent : class "NSObject"
property comWindow : missing value
set library to (load script ("/Users/Seb/Library/Scripts/Library.scpt") as POSIX file)
-- IBOutlets
-- IBActions ( button clicks )
--Do something
on showComs_(sender)
comWindow's makeKeyAndOrderFront_(sender)
end showComs_
on showJarvis_(sender)
try
set library to (load script ("/Users/Seb/Library/Scripts/Library.scpt") as POSIX file)
set dt2 to library's daytime()
set dt to library's lowerCase(dt2)
set chosen to text returned of (display dialog "Anything I can help you with?" default answer "")
on error (err)
library's displayError(err)
end try
end showJarvis_
on shoJarvis_()
try
set library to (load script ("/Users/Seb/Library/Scripts/Library.scpt") as POSIX file)
set dt2 to library's daytime()
set dt to library's lowerCase(dt2)
set curDate to date string of (current date) as string
do shell script "defaults write /Library/Preferences/Jarvis lastOpenTime " & quoted form of curDate
set chosen to text returned of (display dialog "Anything else I can do for you?" default answer "")
on error (err)
library's displayError(err)
end try
end shoJarvis_
##################################################
# Application
on awakeFromNib()
-- Code here
end awakeFromNib
on applicationWillFinishLaunching_(aNotification)
-- Insert code here to initialize your application before any files are opened
set library to (load script ("/Users/Seb/Library/Scripts/Library.scpt") as POSIX file)
set dt2 to library's daytime()
set dt to library's lowerCase(dt2)
set lot to do shell script "defaults read /Library/Preferences/Jarvis lastOpenTime"
set curDate to date string of (current date) as string
set chosen to ""
set the_text to ""
if lot is curDate then
--Normal routine
set the_text to text returned of (display dialog "Good " & dt & " Seb! How are you?" default answer "")
else if lot is not curDate or lot is "" then
-- Get the "hour"
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
-- Get the "minute"
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
set dW to library's displayWeather({postcode:"SN5 8ZH"})
if dW contains "curl" or dW contains "error" then
set dW to "Weather..." & return & return & "...will be nice I'm sure. Unfortunately, I was unable to fetch the weather forecast."
end if
--do shell script "defaults write /Library/Preferences/Jarvis lastWeather " & quoted form of dW
set dialogText to "Good " & dt & " Sir!" & return & return & ("It is " & ((thehour & ":" & theMin) as string) & return & "Today is " & (month of (current date) as string) & space & (word 2 of (date string of (current date) as string)) & ", " & (year of (current date))) & return & return & library's unreadMail() & return & return & dW & return & return & "How are you?"
do shell script "defaults write /Library/Preferences/Jarvis lastOpenTime " & quoted form of curDate
set the_text to text returned of (display dialog dialogText buttons {"OK"} default button 1 default answer "")
end if
if the_text contains "hurry up" or the_text contains "Hurry up" or the_text contains "hurry" or the_text contains "run" then try
set chosen to text returned of (display dialog "Anything I can help you with?" default answer "")
on error (err)
library's displayError(err)
end try
if the_text contains "good" or the_text contains "great" or the_text contains "brilliant" or the_text contains "amazing" then try
set chosen to text returned of (display dialog "Good! Anything I can help you with?" default answer "")
on error (err)
library's displayError(err)
end try
if the_text contains "ok" or the_text contains "OK" or the_text contains "crap" then try
set chosen to text returned of (display dialog "Aww! Anything I can help you with?" default answer "")
on error (err)
library's displayError(err)
end try
-----------------------------------------------------------------------------------------------------------
--IFS...
try
if chosen is not "" then
log chosen
end if
if chosen contains "status" or chosen contains "info" then
log "Status/info requested"
-- Get the "hour"
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
-- Get the "minute"
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
set dW to library's displayWeather({postcode:"SN5 8ZH"})
if dW contains "curl" or dW contains "error" then
set dW to "Weather..." & return & return & "...will be nice I'm sure. Unfortunately, I was unable to fetch the weather forecast."
end if
--do shell script "defaults write /Library/Preferences/Jarvis lastWeather " & quoted form of dW
set dialogText to "Good " & dt & " Sir!" & return & return & ("It is " & ((thehour & ":" & theMin) as string) & return & "Today is " & (month of (current date) as string) & space & (word 2 of (date string of (current date) as string)) & ", " & (year of (current date))) & return & return & library's unreadMail() & return & return & dW & return & return & "How are you?"
display dialog dialogText buttons {"OK"} default button 1
delay 1
showJarvis_()
end if
if chosen contains "How are you" then
display dialog "Great as ever, thank you Sir." buttons {"OK"} default button 1
shoJarvis_()
end if
if library's simplify(chosen) contains "show" and library's simplify(chosen) contains "weather" then
display dialog "Loading Sir." buttons {"OK"} default button 1
set dW to library's displayWeather({postcode:"SN5 8ZH"}, "")
log dW
display dialog dW buttons {"OK"} default button 1
shoJarvis_()
end if
if chosen contains "No" and chosen does not contain "north" then
display dialog "Ok, just message me if you need me Sir." buttons {"OK"} default button 1
end if
if chosen contains "calendar" or chosen contains "cal" then
delay 1
display dialog "Opening iCal..." buttons {"OK"} default button 1
tell application "iCal" to activate
shoJarvis_()
end if
if chosen contains "web" and chosen contains "open" then
delay 1
display dialog "Ok, just opening Firefox right now Sir." buttons {"OK"} default button 1
tell application "Firefox 4" to activate
shoJarvis_()
end if
if chosen contains "check" and chosen contains " mail" then
delay 1
display dialog "I'll open Mail for you..." buttons {"OK"} default button 1
tell application "Mail"
activate
check for new mail
end tell
shoJarvis_()
end if
if chosen contains "unread" then
set uM to library's unreadMail()
activate me
display dialog uM buttons {"OK"} default button 1
end if
if chosen contains "date" or chosen contains "time" then
delay 1
activate me
display dialog (current date) buttons {"OK"} default button 1
shoJarvis_()
end if
if chosen contains "mute" or chosen contains "quiet" then
library's volumeMute()
delay 1
display dialog "Yes Sir." buttons {"OK"} default button 1
shoJarvis_()
end if
if chosen contains "down" then
library's volumeDown()
delay 1
display dialog "Yes Sir." buttons {"OK"} default button 1
shoJarvis_()
end if
if chosen contains "up" or chosen contains "louder" then
library's volumeUp()
delay 1
display dialog "Yes Sir." buttons {"OK"} default button 1
shoJarvis_()
end if
if chosen contains "pause" or chosen contains "play" and chosen contains "itunes" then
display dialog "Yes Sir."
log "Pausing iTunes"
pauseItunes()
shoJarvis_()
end if
if chosen contains "pause" or chosen contains "play" and chosen contains "spotify" then
display dialog "Ok Sir." buttons {"OK"} default button 1
log "Pausing Spotify"
pauseSpot()
shoJarvis_()
end if
if chosen contains "capital" then
set cap to library's capital(chosen)
log cap
display dialog cap buttons {"OK"} default button 1
end if
if chosen contains "battery" or chosen contains "bat" then
set bS to library's batteryStat("MacBook Pro")
log bS
activate me
display dialog bS buttons {"OK"} default button 1
end if
if chosen contains "external" then
set eIP to library's externalIP()
log eIP
activate me
display dialog eIP buttons {"OK"} default button 1
end if
if chosen contains "internal" then
set iIP to library's internalIP()
log iIP
activate me
display dialog iIP buttons {"OK"} default button 1
end if
on error (err)
library's displayError(err)
end try
end applicationWillFinishLaunching_
on applicationShouldTerminate_(sender)
-- Insert code here to do any housekeeping before your application quits
return current application's NSTerminateNow
end applicationShouldTerminate_
end script
Thanks in advance!