I’m trying to build my first AppleScript application and doing it in Xcode.
Essentially what I’m trying to do is when the application is launched it is just a large window with some text. Below the text is an “OK” button. When this OK button is clicked the application should then quit.
Also I would like this application to quit by itself after 20 or 30 seconds.
How would I go about doing this and also is there somewhere I can be pointed for some really basic AppleScript info?
Thanks for the help.
I could make that example. but which verssion of XCode do you have?
I think a ‘display dialog’ could be easier:
on will finish launching theObject
display dialog "Big Long Message" giving up after 20
quit
end will finish launching
But if you REALLY insist:
on clicked theObject
quit
end clicked
on idle theObject
delay 20
quit
end idle
For some reading, go to (Xcode menu bar) Help > Documentation. Find the Applescript section, and read Getting Started.
You also have to set up the connections from the objects to the script in Interface Builder first.
Double-click the .nib file in your AS Studio project (probably MainMenu.nib) and it’ll open in IB. Modify the window the way you want it (drag a button from the Cocoa - Controls window into your application window). Select your newly created button, then open the Inspector window if it is not yet visible (Window : Show Inspector). From the popup menu at the top of the Inspector window, choose AppleScript. I recommend you give your button a name (such as quitBtn). Now expand the Action item in Event Handlers and put a check next to “clicked”. This tells the button to send a message to a script when it is clicked. To pick which script to send the message to, check the appropriate box in the Scripts section at the bottom of the window (you should only have one script listed).
You also need to connect the application’s idle handler. In the nib window, find “File’s Owner” (with an application icon), select it, and choose AppleScript in the Inspector window again. Connect idle (under Application) to your script.
Hint: once you connect an event, click “Edit Script” and the appropriate handler will automatically be created in your script.
If you have more than one button, you can distinguish them by the name you entered in IB like this:
on clicked theObject
if name of theObject is "quitBtn" then
quit
else if name of theObject is "someOtherBtn" then
-- processing here
end
end
To specify how often the idle handler is called, return that number of seconds at the end. Remember it will be called when the app initially launches, so I would do this:
property idleWasCalled: false
on idle
if idleWasCalled then quit
set idleWasCalled to true
return 20
end idle
Thankf ro the help everyone. I’m using Xcode 2.1.
I want to make this a universal binary and it only needs to run on Tiger and above.
Thanks again!
I want to thank everyone for their help. I really appreciate it.
I got the onclick working because of this, I’ve even kind of gotten the return key to trigger the onclick.
However, it doesn’t register the first “return” Only the second (any subsequent ones when testing the interface)
Is there a way to automatically have the OK button highlighted or focused when the application launches so hitting the return key has the same effect as clicking the button?
Also none of the scriplets setting the application to quit after 20 seconds seem to work. I assume it is something I’m doing wrong. Any suggestions?
Thanks again for all of the help.