Search code examples
applescript-objc

Why can't I add these two strings together?


on oneButtonClicked_(sender)
    set faceNumber's setStringValue() to faceNumber's stringValue() & "1"
end oneButtonClicked_

I get this error: "Can’t make «class ocid» id «data optr000000000058B37BFF7F0000» into type list, record or text. (error -1700)"

faceNumber is a label and when the user clicks the button, I want to add string of "1" to it. So for example, if the user clicked the button 5 times


Solution

  • stringValue returns an NSString(wrong answer) CFString. You have to make a real AppleScript String to use it. BTW your code set faceNumber's setStringValue() is not correct. The reasons are:

    1. The Cocoa handlers are always using the underscore.
    2. If you use the setter setStringValue() you don't need to use set x to
    3. If you want to use setStringValue() you must give the parameter between the parentheses

    Now put everything together:

    on oneButtonClicked_(sender)
        faceNumber's setStringValue_((faceNumber's stringValue) as string & "1")
    end oneButtonClicked_
    

    or (to have it clearer):

    on oneButtonClicked_(sender)
        tell faceNumber
            set currentValue to (its stringValue) as string
            setStringValue_(currentValue & "1")
        end tell
    end oneButtonClicked_
    

    I hope you like the answer, after pressing the button twice you have an 11 at the end of the label.

    Cheers, Michael / Hamburg