Tidy up code...

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!

e obvious suggestion would be to make library a property and load the external script just once.

Thanks for that Shane, I made library a property now.

What exactly is the difference between a property and a variable? Also, the “ifs” (if chosen is…) don’t seem to work on showJarvis_(sender), or shoJarvis_() . Why? How can I get them to work?

A property is persistent; it will retain its value while the script is running. It can also be seen within the handlers. A variable is confined to the handler its used in, and its value doesn’t persist between calls.

You’re going to have to be a lot more specific – that’s a lot of code to wade through.

showJarvis_(sender) and shoJarvis_() both have this bit of code:

set chosen to text returned of (display dialog "Anything else I can do for you?" default answer "")

Just like when you launch the application. When you launch the application, there is virtually the same code and I can type something and things will happen because of the “ifs” -

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

etc…

However, those “ifs” (^ above) don’t work on showJarvis_(sender) or shoJarvis_(), only in “applicationWillFinishLaunching” (when the application launches, it shows a dialog, setting chosen to text returned from that dialog) why? how can I get them to work?

Any other suggestions on compacting/streamlining my code?

I can’t see any obvious reason, but as I said, that’s a lot of code to wade through. Can you pos a simple example that shows the problem?

Ok, simple example…

on applicationLaunch()
    set the_text to text returned of (display dialog "What's your name?" default answer "Bob")
end applicationLaunch

applicationLaunch()

set the_text to text returned of (display dialog "Who's awesome" default answer "Steve")

if the_text is "Bob" then
    display dialog "Hi Bob"
end if

It won’t show a dialog saying “Hi Bob” if you type “Bob” into the first dialog box that shows (“What’s your name?”)

I’m not sure why it should. it’s not in the same handler, and the_text is not a property so it’s value is lost once applicationLaunch() has finished running.

Hmm… the same things happening in my main application (first post). So do I maybe need to set chosen as a property? How do I solve this?

If you want to set a variable in one handler and access it in another, it has to be a property.

How do I set a property to the text returned of a dialog box??

You just declare a property at the top of your script:

property foo:

Then foo will be persistent; the only catch is that you need to say 'my foo" when setting it’s value.

This is not peculiar to ASObjC – it’s the same as in standard AS.

Thanks for your help Shane!

Well, setting the property works in that example script but not in my actual AppleScript Delegate of my Xcode project. “if chosen is…” parts don’t work on the handlers shoJarvis_() and showJarvis_(sender) even when I set the property “chosen”.