convert applescript to Xcode

I have a weblog script in applescript, that updates my online blog by sending POST variables to an online php script.

When I use the script a dialog box pops up in which I can enter the text. But now I want to build an interface for this script in Xcode, so instead of having a dialog box popping up, I want to make several textfields in which I can enter my text. Does anyone have a code example on how to do that?

property address : "http://***********/process.php"

display dialog "Enter title:" default answer ""
set theTitle to text returned of result

display dialog "Enter text:" default answer ""
set theText to text returned of result

do shell script "curl -F \"title=" & theTitle & "text=" & theText & "\" " & address

Browser: Safari 412, Xcode 2.1
Operating System: Mac OS X (10.4)

Sorry, no example but…

Open your MainMenu.nib file and make a new window.

Then put in text fields.

Ok, that’s the part I understood already, but what I actually meant is, how can I make the code interact with the textfields in Xcode?

I’ve made two textfields, 1 called “title” and the other “text”. I also have a submit-button.

What should I change in my code so that once the submit-button is pressed, the contents of the text fields is sent to the php script?

use…

set contents of text field “feildname” of window “windowname” to “some text”

and

get contents of text field “feildname” of window “windowname”

A more detailed description:

  1. Go to IB, select a text field.
  2. Go to the AppleScript tab in the Inspector window (I think it’s apple+8 in Tiger, apple+7 in Panther). Enter a name for your text field, so you can identify it later in your AS code.
  3. Repeat with all elements, including the main window (simply unselect anything, or click the window itself in the “Mainmenu.nib (English)” window.
  4. You can avoid these steps and just remember your elements, and target them later positionally (ie, button 1, instead of button “foo”).
  5. Select again the button. You will attach an event handler to it. For example, check the “clicked” event under “Action”. Now, link it to your main applescript (eg, “Application.applescript”, just below).
  6. Return to Xcode. Double-click the file “Application.applescript”. You may see now a handler called “clicked” with a var “theObject”. When the user clicks your button, the button itself will be sent as parameter to this handler.
on clicked theObject
	set objName to name of theObject
	if objName is "submit" then --> submit button
		beep
	end if
end clicked

You don’t need get the name of the button. But it’s useful when you have lots of buttons :wink:
7. Browse AppleScript Studio’s dictionary (you will find here all available events and object’s properties). Study the examples in “/Developer/Examples/AppleScript Studio”. Ask more questions.

Wow, this is a very clear description! Just what I needed to start understanding the concepts behind Xcode. Thanks a lot. I got it working & heard the beep :slight_smile: Now I understand how it works, I can start building my ideas!

Thanks for your help and advice!

Yes, if you sticked before with AS, and perhaps some kind of UI builder (FaceSpan, Dialog Director or 24u Appearance, or even HTML, javascript or actionscript), you need only these basic steps to understand the “modus operandi” of this thing called “AppleScript Studio”, much better than docs and source-code … :wink:

Yeah, and I’m actually much more of a php-programmer. I must say: applescript does have a very friendly syntax, it’s much more like reading a book than reading code. You can easily see what every line does. Though, coming from php, I tend to end every line with “;” :slight_smile: Conversion trouble :slight_smile:

I still have one question. My script now looks like this:


property address : "http://****/process.php"

on clicked theObject
	set objName to name of theObject
	if objName is "submit" then --> submit button
		set theTitle to contents of text field "title" of window "main"
		set theText to contents of text field "text" of window "main"
		do shell script "curl -F \"title=" & theTitle & "text=" & theText & "\" " & address
	end if
end clicked

It works, the variables get sent to the php script, but it seems that applescript sends both variables as one variable named “title”, 'cos when for example I submit the following data:

field title: “bla bla”
field text: “lalala”

after the php file submits it into the database the title field has the following data:

“bla blatext=lalala”

The text field is empty in the database. It seems that the “text=” part gets sent as a string, not as a POST-variable.

As “text” is a string you send to a shell, it has not scope within AS. I think you’re missing a concat char “&”:

do shell script "curl -F " & quoted form of ("title=" & theTitle & "&text=" & theText) & space & address
--> will call: curl -F 'title=THETITLE&text=THETEXT' http://****/process.php
--> instead of: curl -F 'title=THETITLEtext=THETEXT' http://****/process.php

Thanks for the example, I tried it but now I get:

THETITLE&text=THETEXT

So it seems that the “&text” is still sent as a string…

Maybe there’s a different separator for POST-variables? I know “&” is typically for sending GET-variables, but I’m not sure if this is also the separator for POST-variables. Just guessing where it could go wrong…

Hmmm… I use “-d” in my scripts, instead of “-F”. According to curl’s man page, “-F” is used for “multipart/form-data”… Hmmm… Yes, I’ve used this option to upload files to servers. Try using “-d” instead of “-F”. Otherwise, you can try specifying “-F” for every variable.

  • And make sure your text is URL-Encoding-safe! (“foo” is good enough for tests)

JJ, it works now! Changing it to -d did the trick! Strange that the encryption type makes such a difference…

Anyway thanks for all your help! I’m looking forward to exploring Applescript much further, 'cos I really enjoyed my “first steps”, and there’s so much you can do with it!

I’m making a mac-tool for posting to my weblog-system http://zomplog.zomp.nl, so I think I’ve got the basics in place now :slight_smile:

Ok, still one question:

In IB, I changed the field called “text” from a text filed to a text view.

In my code, i changed one line:

set theText to contents of text view "text" of window "main"

I now keep getting the error:

NSReceiverEvaluationScriptError: 4 (1)

I looked everywhere to see what I was doing wrong. I probably should use different code to refer to the text view. Or maybe I should configure the text view in IB in a certain way?

Unlike text fields, text views are enclosed in scroll views so you have to go down the proper reference path to get to the text view (make sure you name the scroll view as well as the text view):

set theText to contents of text view "text" of scroll view "text" of window "main"

Jon

Thanks a lot, I found out how!

Gerben.