Mac If statement in ApplescriptObjC not working.

battlekid

macrumors regular
Original poster
I spent a bit of time on a simple app written in ApplescriptObjC a few months ago, and abandoned it after getting frustrated and deciding I was bored.

I picked it back up yesterday, cleaned up all my crappy code, and adjusted it to work.

It's very simple — I made a little app in Interface Builder that has a "username" field, a "password" field, a dropdown menu with three options (none, battlekid and fz), a "server" field, and a "Launch" button.

If you've ever heard of Minecraft, well, it's basically a extremely simple replacement for the default launcher, that I'm planning to later expand with more features. "battlekid" and fz are my two accounts I own for the game, so the dropdown menu would allow you to simply select a username instead of typing in the user/password.

My code that's ran on launch looks like this (with quite a bit of logging, which I added for debugging, it's going to be removed once I fix the problem — I also don't have a check for the "fz" dropdown option):

Code:
		set theServer to theServerInput's stringValue() -- This sets the server to the server input textbox's return value
		set thePW to thePWInput's stringValue() -- this sets thePW to the password input textbox's return value
		set theUser to theUserInput's stringValue() --this sets theUser to the user input textbox's return value
		-- next
		log "This is the server" -- debug code
		log theServer -- debug code
		log "This is the password" -- debug code
		log thePW -- debug code
		log "This is the username" -- debug code
		log theUser -- debug code
		log "This is theUserInputPicker's output" -- debug code
		log theUserInputPicker's titleOfSelectedItem() -- debug code
		set someVariable to theUserInputPicker's titleOfSelectedItem()
		log "This is SomeVariable's output" -- debug code
		log someVariable -- debug code
		log "======================" -- debug code
		if someVariable = "none" then
			log theServer -- debug code
			log thePW -- debug code
			log theUser -- debug code
			log "NO RESULT" -- debug code
		end if
		log "======================" -- debug code
		if someVariable = "battlekid" then
			set theUser to "battlekid"
			set thePW to "REMOVED"
			log theServer -- debug code
			log thePW -- debug code
			log theUser -- debug code
			log "It's battlekid" -- debug code
		end if
		log "======================" -- debug code
		set thescript to "cd Desktop ;
open -a Minecraft.app --args " & theUser & " " & thePW & " " & theServer
		do shell script (thescript)
Now, what this *should* do, is set the user/password/server as according to the input boxes, and then set the picker result's title to the variable someVariable.

When running this, everything runs fine, except any code in if statements. Nothing is logged, it just doesn't work. As you might notice, I log someVariable right after creating it, and the result is correct (none/battlekid/), but the if statement doesn't seem to trigger at all...

I then removed the first if statement, and added an else statement to the second one, which worked perfectly, except that choosing the option "battlekid" (or , or none) still would trigger the else statement without setting theUser/thePW or logging anything.

I'm now extremely confused. I'm not sure if the if statement just hates me, or if I messed up with the variable's scope, but it isn't really making sense.

At any rate, it isn't working, and I can't seem to find a solution in Google.

Edit: The rest of the code works fine — I can still manually enter my username and password in the fields for them, and it works, but if I use the dropdown menu it just ignores it. And I know that it's passing variables to the script because of the log command.
 
Last edited:

Red Menace

macrumors 6502
May 29, 2011
389
50
Littleton, Colorado, USA
One of the big things to remember with AppleScriptObjC is that Cocoa methods return Cocoa objects. You should get in the habit of explicitly coercing stuff returned by Cocoa methods into the desired AppleScript class if you are going to be using them in AppleScript statements, because although things will compile and run (or show in the log), the results may not be what you think.

In your code snippet, coercing the results from titleOfSelectedItem() to text will fix your comparisons, but the other text fields should also be coerced to text to avoid mysterious problems when they are used later. The console log may display text, but that doesn't necessarily mean that the objects really are text.
 

battlekid

macrumors regular
Original poster
One of the big things to remember with AppleScriptObjC is that Cocoa methods return Cocoa objects. You should get in the habit of explicitly coercing stuff returned by Cocoa methods into the desired AppleScript class if you are going to be using them in AppleScript statements, because although things will compile and run (or show in the log), the results may not be what you think.

In your code snippet, coercing the results from titleOfSelectedItem() to text will fix your comparisons, but the other text fields should also be coerced to text to avoid mysterious problems when they are used later. The console log may display text, but that doesn't necessarily mean that the objects really are text.
Thanks for responding.

Mind pointing me in the right direction for how to coerce the output to text? Sorry, but I've only worked with shell scripting and basic Applescripting before, I've been using Google and the little I know of applescript to write this so far, but I'm not quite certain how to convert it to text, and a quick google search didn't turn up much.
 

Red Menace

macrumors 6502
May 29, 2011
389
50
Littleton, Colorado, USA
Just add the class you want to coerce to, for example
Code:
set theServer to theServerInput's stringValue() as text
set someVariable to theUserInputPicker's titleOfSelectedItem() as text
The main Cocoa > AppleScript equivalents would be NSString > text, NSArray > list, NSDictionary > record, yes/no/0/1 > boolean, and the various number classes would be whatever you need - number, integer, real, etc. Check the Cocoa documentation to see what the various methods return, and the AppleScript Language Guide for its classes and what they can be coerced to (these will be in Xcode's API reference).
 

battlekid

macrumors regular
Original poster
Just add the class you want to coerce to, for example
Code:
set theServer to theServerInput's stringValue() as text
set someVariable to theUserInputPicker's titleOfSelectedItem() as text
The main Cocoa > AppleScript equivalents would be NSString > text, NSArray > list, NSDictionary > record, yes/no/0/1 > boolean, and the various number classes would be whatever you need - number, integer, real, etc. Check the Cocoa documentation to see what the various methods return, and the AppleScript Language Guide for its classes and what they can be coerced to (these will be in Xcode's API reference).
Thanks!