Using Xcode Cocoa-AppleScript (ASObjC) app to Provide Rich GUI

For a long time I have been looking for a means to provide rich, complex GUI for my AppleScripts. Some time ago, I tried Xcode, but found it to complex, overwhelming, with too steep of a learning curve.

But thanks to this very helpful tutorial video by Ben Waldie, I have just created my first Xcode GUI app in less than 15 minutes:
Giving your AppleScripts a Face Lift with AppleScriptObjC - Ben Waldie

Although Ben’s video is a bit dated (2013), and Xcode has had a few changes, including its own GUI, I was able to create a Xcode Cocoa-AppleScript app from scratch, relying only on my memory from the just-watched video.

Of course, it is a very simple “hello world” type app, but I am still very impressed with how easy it is to create a standard Mac window, and add various GUI elements like labels, text block, and buttons.

I even figured out how to do two things that were not in the video:

  1. Bind a shortcut key to a button
  2. Quit the app when the window was closed.

It was mostly drag/drop, set a few properties in the UI Builder, and I had to write only 4 lines of code.

If you are interested in rich, complex GUI for your AppleScripts, then you might want to view Ben’s video. He includes demo of both simple and very complex GUI, step-by-step instructions, and a download of all examples and other material.

Of course I am at the very beginning, and have a lot to learn about Xcode. I hope to find others here that are also interested, and we can help one another on our journey.

@alldritt, have you considered creating a “ASObjC” Forum Category?
Or at least create a tag for it. An Xcode tag would be useful also.
I see a tag for “asobjc” in the tag list, but it won’t let me assign/select that tag for this topic.

1 Like

OK, just learned a valuable lesson.

in Xcode, you cannot use this script library statement that works in normal AppleScript:

--- THIS FAILS TO BUILD ---
use myLib : script "JMTX TEST Lib AS"

But you can use this:

--- THIS WORKS ---
property myLib : script "JMTX TEST Lib AS"

and then this call to a handler in the script library works fine:

getLibInfo("Test from Xcode") of myLib

I believe this is huge!

This will allow us to develop the core AppleScript code, that has nothing to do with the Xcode UI, using Script Debugger. We just need to put the core script(s) in a Script Library file.

The Xcode “script AppDelegate” locates the files in the Script Library standard locations just fine.

Just for reference, here is my entire “AppDelegate.applescript” code:

--
--  AppDelegate.applescript
--  Test Xcode ASObjc
--
--  Created by JMichaelTX on 1/12/17.
--  Copyright © 2017 JMichaelTX. All rights reserved.
--

script AppDelegate
    property parent : class "NSObject"
    property theName : ""
    property myLib : script "JMTX TEST Lib AS"
    	
	-- IBOutlets
	property theWindow : missing value
	
	on applicationWillFinishLaunching_(aNotification)
		-- Insert code here to initialize your application before any files are opened 
	end applicationWillFinishLaunching_
	
	on applicationShouldTerminate_(sender)
		-- Insert code here to do any housekeeping before your application quits 
		return current application's NSTerminateNow
	end applicationShouldTerminate_
    
    on goButton_(sender)
        display alert "Hello " & theName & "?"
        getLibInfo("Test from Xcode") of myLib
        
    end goButton_
    
    --- QUIT APP WHEN WINDOW IS CLOSED ---
    on applicationShouldTerminateAfterLastWindowClosed_(sender)
        return true
    end applicationShouldTerminateAfterLastWindowClosed_
    
	
end script

Example Results

If any of you Xcode and ASObjC gurus have any tips, or see any issues with what I have posted, please jump in and share your knowledge.

Thanks.

Sure you can. You just need to put it outside the script object, not inside. And you’ll also need a use scripting additions if you’re going to use any addition commands.

You can also include use framework... statements. They won’t actually load the frameworks – you use the normal Xcode methods for that – but it means you will have access to the relevant code-completion when you use Script Debugger as an external editor (and if you don’t use an external editor, you’re making your life a lot harder than it need be).

OK, thanks for the clarification, Shane. I’ll give that a try tomorrow.

Is there an advantage of one approach or the other?

IAC, by putting the core AppleScript code in a separate script file, this allows us to debug it using Script Debugger.

Yes: a use statement is preferable here for the same reason its preferable outside Xcode: it can shield you from uncommon but very-hard-to-debug terminology issues.

OK, confirmed that it using a “use” statement works for me:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
use myLib : script "JMTX TEST Lib AS"


script AppDelegate
  property parent : class "NSObject"
  property theName : ""
  
  -- IBOutlets
  property theWindow : missing value
  
--- REMOVED A BUNCH OF LINES FOR THIS POST ---

  
  on goButton:sender
    display alert "Hello " & theName & "?"

   --- CALL HANDLER IN MY SCRIPT LIB ---
    getLibInfo("Test from Xcode") of myLib
    
  end goButton:
  
  
end script

OK, thanks for the tip.

I did some searching on “xcode external editor” and didn’t find much.

Are you talking about Xcode > File > Open with External Editor ?
Any special settings I need to make anywhere?
Any caveats/issues/traps I need to be aware of?

It opens the “AppDelegate.applescript” in SD, but not compiled, of course.
I added a line, compiled, saved, closed, and manually returned to Xcode.
The changes were there.

Any other suggestions?
Remember I’m brand new to Xcode (although I’ve used a number of other IDEs), so any/all help is greately appreciated.
Any Xcode tutorials you (or anyone) would suggest, particularly in using he UI Designer?

Thanks. I’m excited about this new journey, and it taking me to new levels in AppleScript and ASObjC.

Yes.

No.

You need to (a) make sure you save before switching back to Xcode, and (b) don’t edit in Xcode if a file is open in SD.

I wrote a book full of them, but unfortunately there wasn’t enough demand to keep it up-to-date – and Xcode is very much a moving target.

My main advice is to treat it as a way of putting front ends on scripts, and try not to get carried away. It’s easy to run into limitations.

Jim,

This is exactly what I have been working on with the Database I’ve been doing. Showing people how to use ASObj-C to it’s fullest effect. You don’t need Xcode at all. Not even for windows. The interface can be build from just code. I’ve been focusing on simple stuff to help people do anything but after reading your posts I did a more complex example.

My favorite thing is to using the spotlight database straight from an ASObj-C script. So I added as sample to the database. It’s a little awkward to use in that it starts a search, asks the notification manager to notify the script when done and then when the search is done a handler gets called and the result can be processed. But while it is waiting in the loop other things can be done inside the loop the script stays in until the the search results get back.

I keep thinking about trying it with threads so the script can keep going on with other things while the script does other things in a much more structured fashion. I keep thinking the thread part wouldn’t work with ASObj-C but I should try it. Currently other things can be done while the search is performed by using a loop in the program which performs other stuff while in the loop then acts on the response when the search result return. It not as clean as I would like but it is pretty useful.

By the way I posted the newest version of the ASObj-C database under the topic “New version of ASObj-C database version a14” if you are interested.

Bill

Hi, the best solution I found for creating amazing Applescript interfaces wasn’t FaceSpan (oldies will remember that!), or Xcode (far too complex for me) or AppleScript studio ( Argh no thanks)… it was, believe it or not, ‘RealBasic’… I read Matt Neubergs AppleScript book and followed it up with his book on RealBasic. For me, then, linking these two solutions together was the holy grail. I could write the code in Applescript and make it all accessible to the enduser through a beautiful and functional RealBasic GUI. A no brainer.

Wow you could create a GUI in moments and embed all the Applescripts into the executable app and attach them to the interface items. Admittedly all this was many years ago (2008/9) and people will probably scoff at me for this comment, but who cares. It was enough to be able to understand the Client’s studio and print workflow as well as his mananagement processes, then automating it all by learning and applescripting InDesign, Finder, Excel, Acrobat etc etc… so I needed the easiest entry point into quality interface design. I had little time and no resources to prove concept, except a supportive boss, and wow did it work. Thanks to Mark Alldritt, Script Debugger made it all possible. :wink:

Paul,

I did play around a bit with RealBasic for a little while. But I was too obsessed with AppleScript and never really looked at it closely enough. It looks like I missed out on some good stuff :thinking: I guess in the future I should learn not to dismiss things to quickly :slight_smile:

Bill

For me the best interface builder in Mac OS X was Mark’s version of FaceSpan. I was very sad when that hit a roadblock.

Before that was HyperCard in Mac OS Classic (which I still use via sheep shaver).

Bill, thanks for your suggestion and your work.

Unfortunately, I find building a GUI using only code to be very tedious, a lot of trial-and-error (mostly error), and time consuming.

At first I also found Xcode to be intimidating. But after this helpful tutorial, I have found it to be much easier:
Giving your AppleScripts a Face Lift with AppleScriptObjC - Ben Waldie
A better title for this tutorial is:
How To Build Great GUI for AppleScripts Using Xcode

In the Xcode UI Designer, it is very simple to build a GUI:

  • Drag/Drop UI elements/objects onto the window from the Object Library
  • Set a few properties in the UI Designer (most of the defaults are fine for me)
  • Drag/Drop between the UI elements and the AppDelegate object to make the needed connections
  • Write a small amount of code in the AppDelegate

Of course, I have not done any complex UI’s yet, so my impression is subject to change with my experience.

A post was split to a new topic: Cannot use asobjc tag

Has Apple removed Cocoa-AppleScript-Apps from Xcode? I can’t find it in Xcode 9.2.

No — it’s just been renamed to AppleScript App.

Oh, I just didn’t realize that I have to create a new project instead of pressing Cmd+N. :wink: