comparing strings no longer works after restart and read from defaults

Hello,

The first time I run the app I select an app and it calls the function when the app launches. When I quit the app and restart it, the function is no longer called. I saved the appName to defaults read it back.

This is how I set the defaults :

        ud's setObject_forKey_(appName, "appName")

I call them on startup :

        set appName to ud's stringForKey_("appName")

I checked the plist file and it is saved correctly. And on startup I log the defaults that I read back and that also works.

This is where I compare the string appName :

    on watch_(aNotification)
        set var to aNotification's userInfo's NSApplicationName
        if debug then
            log ":" & var & ":" -- added colons to check if there was a white space in front/back of string
            log ":" & appName & ":" 
        end if
        if var = "Address Book" then log "var is equal" -- does not work
        if appName = "Address Book" then log "appname is equal" -- does not work
        if var = appName then -- does not work
            if debug then log "app is launched, appCall()"
            appCall()
        end if
        if debug then log "called watch"
    end watch_

This is the log output for the above code on a second startup :

2011-03-19 18:52:08.131 app[2273:903] :Address Book:
2011-03-19 18:52:08.132 app[2273:903] :Address Book:
2011-03-19 18:52:08.133 app[2273:903] called watch

In the log statements var and appName are NSString’s, so you have to coerce them to a AS string’s before logging.

if var as string = "Address Book" then log "var is equal" -- does not work
if appName as string = "Address Book" then log "appname is equal" -- does not work

if var = appName then 

Here, I’m not sure what is wrong – both var and appName should be NSString’s, but I’m not sure what notification you’re using. Try logging aNotification’s userInfo’s NSApplicationName’s |class|() and see what you get

Ric

This is what I use for notification :

set noter to current application's NSWorkspace's sharedWorkspace()'s notificationCenter()
        noter's addObserver_selector_name_object_(me, "watch:", current application's NSWorkspaceDidLaunchApplicationNotification, missing value)

Adding the as string makes the two statements work.

To fix the problem this is what I did :

        set appName to ud's stringForKey_("appName") as string

I did try “if (var’s isEqualToString_(appName) as boolean) then”, but that did not work. Could you explain this ?

So whenever using obj-c methods I need to convert between NS… to AS… ?

Thanks

I don’t know what you mean by this. You often do have to coerce cocoa method results to AS things like strings or lists to use them.

The isEqualToString method should return TRUE or FALSE (in objective-C) but I think applescript coverts that to 1 or 0, so I use “if (var’s isEqualToString_(appName) as integer is 1) then”.

That’s exactly what I wanted to know.

The following code does work :

set test1Va to test1's stringValue()
		set test2Va to test2's stringValue()
     
        if (test1Va's isEqualToString_(test2Va) as boolean)

test1/2 are linked to input box. Different from the other example ?

The two examples look like they should be the same. Did you get any error message from the example that didn’t work?

-[appAppDelegate watch:]: Can’t make quoted form of «class ocid» id «data kptr00000000902C6C70FF7F0000» into type Unicode text. (error -1700)

From this :

set var to aNotification's userInfo's NSApplicationName
        if (var's isEqualToString_(appName) as boolean) then

I’m lost now – too many partial pieces of code for me to tell what is going on. I tried the following and it worked fine:

script VarTestingAppDelegate
	property parent : class "NSObject"
	property ud : missing value
	property var : missing value
	property appName : missing value
	
	on applicationWillFinishLaunching_(aNotification)
		set noter to current application's NSWorkspace's sharedWorkspace()'s notificationCenter()
		noter's addObserver_selector_name_object_(me, "watch:", current application's NSWorkspaceDidLaunchApplicationNotification, missing value)
		set ud to current application's NSUserDefaults's standardUserDefaults()
		set appName to ud's stringForKey_("appName")
	end applicationWillFinishLaunching_
	
	on watch_(aNotification)
		set var to aNotification's userInfo's NSApplicationName
		if var as string = appName as string then log "var=appName"
		if (var's isEqualToString_(appName) as boolean) then log "(var's isEqualToString_(appName) as boolean was true"
		ud's setObject_forKey_(appName, "appName")
	end watch_
	
end script

I ran a slightly altered program once to get the name “Safari” into the user defaults, then I ran it as written above and both if statements worked. The first if statement only worked if both “as string” coercions were in there.

Ric

After edit: It works also if you take out the “as boolean”, so just:
if var’s isEqualToString_(appName) then log …