This is probably a stupid question, but how can I link an interface I made in the interface builder to my script? I have a few dialogs in the script and for most I just want the normal “display dialog…” setup, but I designed a large text input dialog and I can’t figure out how to have it show when the script runs. Any help?
Do you mean that you have a script that you made in Script Editor, and then you fired up Interface Builder (IB) and whipped up a nib file, and now you want to introduce them and encourage them to hook up? Unfortunately, that’s not how it works.
A nib file is only worth something if it’s part of an application… which you must create in Xcode (aka “Applescript Studio”, when used to make an applescript application). You need to create a new applescript-based application project in xcode, then edit the nib that’s created automatically in the project. Of course you can have many nib files in a project, but I’m guessing you’re not ready for that yet. Once you have your nib file, then you connect it to handlers in IB, which in turn are created in your main script file back in Xcode. Then you go back to xcode, and find a way to integrate the code you have in your Script Editor applescript into your xcode script. It’s nowhere near as simple as you may be hoping.
Try looking through the apple docs, looking through the sample applications (/Developer/Examples/Applescript Studio/), and searching around here at macscripter when you can’t find solutions to problems you encounter.
Thanks for the reply,
I have made the script in Xcode, and I did link the dialog’s button’s and text fields to it, all I need now is the code snippet to display the dialog. I was hoping is was something simple I could paste into my script to link the menu.nib so it runs at a point halfway through my script. Anyway, thanks for the apple developer link, I’ll read up on it
Okay, I have made a little progress. The developer guide is completely incomprehensible to me, I’m going to sit and read the whole thing later.
Anyway, I have a dialog, and I have the on clicked theObect, but I want the text from my dialog! I can find how to get the text returned and set it to a variable, I have a perfect script otherwise. PLEASE HELP!!!
If you already have an “on clicked theObject” handler, then it should be quite simple to display your dialog when an object in your window is clicked.
Let’s assume that in your window (say window “main”) you have a button that you named “myButton” in the IB and is linked to your script. In other words, when you click this button you want your dialog (here I set it as property myDialog) to be displayed on the screen. Then try this:
property myDialog: "Gotcha"
on clicked theObject
set objectName to the name of theObject as Unicode text
if objectName is "myButton" then tell me to display dialog myDialog
end clicked
Your program should display the text “Gotcha” on your screen as an AppleScript dialog.
Note that I did not reference the window “main” above. If above does not work as is, you may need to do that but it’s probably not necessary in a simple setting as above.
That is helpful, but I need it to do something else. I think I was wrong about using the on clicked thingy, here is the part of my script I want it in, tell me if I’m nuts!
my apple_login()
tell application "Forum Post Script"
activate
end tell
display dialog "Enter the subject" default answer "Post Subject" buttons {"OK"} default button 1
set the subject_text to the text returned of the result
--------------------------------------------------------
-- Right here I want to connect the dialog I built, so it runs and gives me post_text. Can I???
--------------------------------------------------------
tell application "Safari"
activate
set URL of document 1 to apple_newpost
delay 8
tell application "System Events"
tell process "Safari"
keystroke subject_text
keystroke " "
keystroke post_text
-- keystroke " "
-- keystroke return
end tell
end tell
end tell
my new_tab()
delay 1
tell application "Safari"
set URL of document 1 to script_newpost
delay 8
tell application "System Events"
tell process "Safari"
keystroke subject_text
keystroke " "
keystroke post_text
-- keystroke " "
-- keystroke return
end tell
end tell
end tell
On first glance at your codes, your app does not look like it’s in AS Studio. But then, you may have opted not to post the other parts of your codes.
I don’t know but here’s how I would do it if you still want your app in AS Studio. Instead of the classical AppleScript dialog box, I would create a AS Studio UI with a text field where users can input their preferred subject in a window. The prompt “Enter the subject” is basically just a “fixed” text (not connected to any script) but opposite that is a script-linked text box which shows your default text “Post subject” that you can preset on “awake from nib”. Then, simply pull out the content of that text box (actually a text field itself) when an “Enter” button is clicked and feed the value to your script for further processing.
While the above may look aesthetically more pleasing than pure AppleScript dialog box, it may look like an “overkill” for a simple text input function. Unless you have some other uses for the simple user-input window, I would stick with pure AppleScript.
Your code is all kind of confusing. I’m a bit baffled why you’d even write an interface if you’re not going to take advantage of it. Why not put a subject field in your interface instead of using a dialog? Seems sloppy and confusing.
Is this the name of your application? You shouldn’t need to activate yourself when you’re already active.
I don’t think that we’re all convinced that you’re actually creating an app in xcode, because you’re not posting any code that would convince us that you are. Where are your ‘on clicked’ handlers and other handlers that applications rely on? Once you are certain we’re all working on the same page, then continue.
What I would do is to make a window in my interface that has not just a post field, but also a a subject field. To display the window, just send it a “show” command. In my example below, I’ve named my window “postContentWindow”. You must provide a name for your window, and all of your other objects as well. You can’t get the value of a text field or act upon an object without a reference to that particular object! Nowhere in your code do I see any references to objects… i.e. window “X” or text field “yerMama”.
Another concept which seems to escape you is that applications don’t just run start to finish. Windows are NOTHING like dialogs. You can’t just drop a “set myPost to (display my window)” into your script and have it work like a dialog. Instead you need to put some real thought into the process flow of your app and design your code accordingly. To get the content of a text field or text view, you have to ask for it’s contents. If you need to open up the window, enter the contents, and then do something with it, these are all separate steps that must be handled in different places in your code. What you might end up with is something like…
on clicked theObject
if name of theObject is "getPostContent" then
show window "postContentWindow"
else if name of theObject is "processPostContent" then
tell window "postContentWindow"
set subject_text to content of text field "subject"
set post_text to content of text view "post" of scroll view "postScroll"
hide
end tell
enterMyPost(subject_text, post_text)
end if
end clicked
to enterMyPost(subject_text, post_text)
tell application "Safari"
activate
set URL of document 1 to apple_newpost
... BLAH, BLAH, BLAH...
end tell
end enterMyPost
In my first post I recommended that you take some time to read up a bit on how to work in asstudio. While I encourage your enthusiasm, I still strongly recommend that you spend some more time doing research. Your code doesn’t seem to reflect even a basic understanding of the fundamentals of working in asstudio.
Okay, I’ve been really looking into it, and I just have a little question. Please don’t tell me how sloppy the script is, everyone starts from somewhere. Anyway, I have two text fields in my interface, a little one, and a large one with a scroll bar. I’ve set the large one to post_field, and the small one to subject_field, and I don’t know how to get the text out of it and assign it to a variable. The script compiles, but when I run it I hear that post_text is not defined. So here is my script in it’s entirety, and this: http://jlee.fwaquarium.googlepages.com/Screenshot1.png (reload until it shows an image) is a link to an image of the interface. Thanks!
-- Forum Post.applescript
-- Forum Post
-- Created by Jamie on 03/11/06.
-- Copyright 2006 __MyCompanyName__. All rights reserved.
on new_tab()
tell application "Safari" to activate
tell application "System Events"
tell process "Safari"
click menu item "New Tab" of menu "File" of menu bar 1
end tell
end tell
end new_tab
on launched theObject
set title of "subject_field" to "Subject Text"
set title of "post_field" to "Post Text"
set title of "cancel_button" to "Cancel"
set title of "post_button" to "Post"
set title of "view_button" to "View Button"
set subject_text to the contents of text field "subject_field"
set post_text to the contents of text field "post_field"
end launched
on clicked theObject
-- Cancel Action
if title of theObject is equal to "Cancel" then
quit
-- View Posts Branching Point
else if title of theObject is equal to "View Button" then
set view_mainorpast to display dialog "Would you like to view your past posts, or just the main forum?" buttons {"View", "Main"}
set buttonfrom_view_mainorpast to button returned of view_mainorpast
-- View pasts posts script
if buttonfrom_view_mainorpast is equal to "View" then
tell application "Safari"
activate
set URL of document 1 to "https://support.apple.com/cgi-bin/WebObjects/ACAuthWeb.woa/wa/login?appIdKey=2ddbca23a85d20e5bf7478812379ae23&path=/login.jspa%3FsuccessURL%3D%2Findex.jspa"
delay 8
tell application "System Events"
tell process "Safari"
keystroke " "
keystroke "my login here"
delay 2
keystroke " "
keystroke "my password here"
keystroke return
end tell
end tell
end tell
set URL of document 1 to "http://discussions.apple.com/questions.jspa"
my new_tab()
set URL of document 1 to "http://bbs.applescript.net/search.php?action=show_user&user_id=14350"
-- View all forums
else if buttonfrom_view_mainorpast is equal to "Main" then
tell application "Safari"
activate
set URL of document 1 to "https://support.apple.com/cgi-bin/WebObjects/ACAuthWeb.woa/wa/login?appIdKey=2ddbca23a85d20e5bf7478812379ae23&path=/login.jspa%3FsuccessURL%3D%2Findex.jspa"
delay 8
tell application "System Events"
tell process "Safari"
keystroke " "
keystroke "my login here"
delay 2
keystroke " "
keystroke "my password here"
keystroke return
end tell
end tell
set URL of document 1 to "http://discussions.apple.com/category.jspa?categoryID=160"
my new_tab()
set URL of document 1 to "http://bbs.applescript.net/viewforum.php?id=2"
end tell
end if
else if title of theObject is equal to "Post" then
tell application "Safari"
activate
set URL of document 1 to "https://support.apple.com/cgi-bin/WebObjects/ACAuthWeb.woa/wa/login?appIdKey=2ddbca23a85d20e5bf7478812379ae23&path=/login.jspa%3FsuccessURL%3D%2Findex.jspa"
delay 8
tell application "System Events"
tell process "Safari"
keystroke " "
keystroke "my login here"
delay 2
keystroke " "
keystroke "my password here"
delay 1
keystroke return
delay 4
end tell
end tell
set URL of document 1 to "http://discussions.apple.com/post!default.jspa?forumID=724"
delay 8
tell application "System Events"
tell process "Safari"
keystroke subject_text
keystroke " "
keystroke post_text
-- keystroke " "
-- keystroke return
end tell
end tell
end tell
my new_tab()
delay 1
tell application "Safari"
set URL of document 1 to "http://bbs.applescript.net/post.php?fid=2"
delay 8
tell application "System Events"
tell process "Safari"
keystroke subject_text
keystroke " "
keystroke post_text
-- keystroke " "
-- keystroke return
end tell
end tell
end tell
end if
end clicked
I can’t look at your entire code but would like see if I can help you through the fairly simple matter of getting the content of a text field in your window.
Assuming that you have a text field in your window (let’s say window “main” for instance), that text field should have a name in the IB. There you will see a box entitled “Name”. This is the important item that you need, not the attribute name (which you set as “title” of your text field in your “on launched theObject” handler). Remember, it is the “name” of the object that you will have to reference later in your code.
Now, if you have a named text field in the IB, let’s say “myTextField” then it is easy to get the content of that text field. If all you need is the content, you may not even need to click the link to the script at all. However, if your program aims to manipulate the content of the text field then you will need to click that link to the script at the bottom so that the text field can be manipulated any which way you want.
To get the content, just code it like so:
set thisText to the content of text field “myTextField” of window “main”
The variable “thisText” should give you the content of the text field “myTextField” and you can then use it.
I hope this sets you on the way to getting the contents of your text fields.