Can i create a mac application using xcode that runs scripts? - xcode

I am very new to making applications, and using Xcode, so I don't know much about it... I'm using Xcode 5.0.2 to attempt to create a basic osx application which will allow the user to search through a list of built in scripts, select the ones they would like to run, and push a button to run the scripts. I have several scripts already, all of which have the extension .scpt. i wrote the scripts using applescript editor.
From searching this website, and the rest of the internet, i haven't found a clear, step by step answer of what to do. I know that in order to run the shell script, there must be an interface button linked to an action in the implementation file, which is what will execute the script.
What is the action i have to put in the implementation file?
How do i link the button to the action?
where do i put my .scpt files?
I have absolutely no code written in Xcode, it is nothing but a blank new project.

I don't have much experience with Cocoa apps, if you have experience with Visual Studio this is nothing like it, I started by looking some videos from the link below, they're really helpful, he also provide a full course for Objective-C.
http://www.youtube.com/playlist?list=PLE83F832121568D36
I hope this helps!

There is a huge amount of aspects to the problem you are trying to solve. Honestly, connecting the NSButton to an Action is just taking the first (small) step! Just a few of the aspects you may encounter:
How are the files executed? (your main question)
How is the selection interface set up?
How to read from the files? How to process the data you read?
What kind of actions will your scripts perform? What is in fact allowed from within a Cocoa app? (I have no idea myself to be honest)
Each one of these suggests potential SO questions in itself.
I would suggest that you familiarise yourself with the environment and the language a bit before taking big leaps. Take some time (a few days at least) and follow some tutorials, program some simple apps. The link provided in the other answer is in fact a good one, AppleProgramming tutorials are easy to follow. You may also find Apple's Objective-C intro useful.

Related

Interact with GUI Elements of a Windows Application

First of all, I want to appreciate the work for the SCIDvsPC Project. I know that the basic SCID one has been discontinued many years back and the developer have done a great job with expanding it and doing his share for the Chess Field. We have a Minor Project to do in this 6th semester of our college. We've decided to start a project on a Chess Next Move Analyzer that is based on variety of filters and implements Self Learning and Machine Learning.
I've been researching over the project idea for the last 2 months. Actually we need to import several games defined on some filters and read and analyze from the PGN file generated. For example, if the user chooses to get the next best move predicted according to the rating range of 2000-2500, our program should only export and analyze the PGN files that have both the opponents from this range only. I know the project can do all this but I'm confused over how to automate this. I mean I have to manually enter the moves and then click on 'Generate PGN' but how to make my program do this ie take input from the user (like first 3 moves), make the project run these moves (what I had to manually) and then generate the PGN file and keep it in a folder.
I've surfed the net about interacting with GUI elements in Windows (we have no problem in working with Linux either) and came to know about Microsoft UI Automation, Python, Java and C# softwares and something like COM. Do the software support COM or any one of these or have you already developed some functionality like this? Please can you guide me over this?
If asked to Generalize this what I want to do is to interact with GUI Elements, be it any application. Take Notepad as an example. Suppose I want to open a file on it, find and replace a particular word. Now, I know how to do this manually but when I have over thousands of file I need some kind of program to do this for me. Do some specific programs like SCID in my case has some feature (read bit about COM) pre-built to handle this? In which programming language domain does this come into? Is using Linux help me more?
Take Notepad as an example. Suppose I want to open a file on it, find
and replace a particular word. Now, I know how to do this manually but
when I have over thousands of file I need some kind of program to do
this for me. Do some specific programs like SCID in my case has some
feature (read bit about COM) pre-built to handle this?
Your situation sounds to be quite specific so I doubt whether you will be able to find a pre-existing program to do this for you. Meaning: you'll have to code it yourself.
In which programming language domain does this come into?
Well, this could probably be done in many, many different programming languages. A simple shell script would be able to achieve the Notepad example you gave.
Is using Linux help me more?
No, your goals seem to be pretty achievable by a simple shell script, whether you write it in a Windows, macOS or a Linux distro.
#SB87 gave you some useful hints, I'd like to expand his answers.
Sorry, I don't think you know what you're talking about. Reinforcement learning (better term than self-learning) and machine learning are not something suitable for a college project. It's at the PhD or research level, consider getting yourself into university before even thinking about anything like that.
UI automation is possible, but error prone and slow. If you want to do it, you'd write a console program. You mentioned something about user inputs, do you mean you want to apply machine learning on user mouse-keyboard inputs? It's not going to work. Machine learning for chess requires hundreds and thousands of training set.
I think you should downplay the project and focus on something you can achieve.

How to record Mail GUI actions on Mac using AppleScript Editor

I'm a newbie to Mac and trying to use AppleScript to ease my daily job. One of the things I want to achieve is to automatically create Smart Mailbox based on the groups in my Address Book. While I have no problem automate Address Book, there's no much article about how to automatically create Smart Mailbox. After some searches on the web I realized it can only be done through GUI scripting. But most of the answers are about how to bring up the new Smart Mailbox dialog but no further information about e.g. how to select different rules. I tried to use AppleScript Editor to record my activity so that I can learn from that but found out after recording, there's nothing get recorded! I tried with other apps like Finder the recording does work! If anyone knows how to record it, or how to get more information on this type of GUI scripting would be very appreciated!
Unfortunately Mail's Applescript support is more than a little maddening at times due to being so limited. Control of smart mailboxes is but one example. I'd hoped SL would have fixed this but Apple's thinking about Applescript can be pretty schizophrenic at times. I'm not sure they have decided what to do with it.
I'd got a Smart Mailbox creation script working but it never worked reliably so I never deployed it. If you really want it I can give you the code - although it's written in Python + Appscript rather than pure Applescript.
Were I you I'd submit this as a bug to Apple and hopefully it'll be fixed with Lion.
The problem with GUI scripting as I'm sure you discovered is that figuring out how to get the name of each particular element can be maddening. There are a few utilities out there that will help, but unless you are doing a lot of GUI scripting they probably aren't worth the cost.
Regarding Applescript recording this was a really nice feature often supported under the old MacOS. However with the switch to OSX almost no application supports it so it's basically a dead feature for all practical intents.

Creating an OS X Address Book Plugin

I'm taking my first steps toward learning to write desktop code for the Mac and, thinking it would be a baby step, I decided to write an Address Book plugin to "fix" something that's annoyed me for a long time. Embarrassingly, I can't even get the boilerplate plugin to work the way I think it's supposed to work.
My understanding from the Programming Guide is that creating a new Address Book Action Plugin actually creates a simple, but functional, plugin. If I create a build and copy the bundle to ~/Library/Address Book Plug-Ins/, I should be able to open Address Book and see a custom context menu item when right-clicking on a phone number. I don't see that.
I'm brand new to the entire Mac development ecosystem. I've never used XCode, never written any Objective-C, never worked within the Cocoa framework. It's entirely possible--maybe probable--that I'm missing the most obvious of answers because I simply don't know what I'm looking for. Unfortunately, there's quite the dearth of beginner tutorials on these things. The programming guide itself isn't particularly beginner-friendly.
Any guidance would be much appreciated.
Sounds like a 32/64 bit problem.
Find the Target in Xcode's sidebar that represents your plugin bundle. Click on it, then click Info in the toolbar. On the Build tab of the info window, you should find Architectures: change that to "64 bit intel".

Learning AppleScript

What resources would you recommend to pick up AppleScript. I come with a traditional C/C++ with Objective-C background.
I am also looking for tips on how to develop better and get faster documentation from the script editor. A sample tip would be 'lookup the sdef file of the app you want to script.
I learned from AppleScript: the Definitive Guide. The free documentation available online at that time was quite confusing and incomplete, but that book taught me everything I needed to know. I'm not sure if the docs have improved since then (2005-ish).
As for tips on getting documentation, the script editor's "Open Dictionary" command is about the only documentation you'll get for most applications.
I recommend Applescript: The Definitive Guide like Ryan Ballantyne. Apple's own documentation is very good: Scripting and Automation: Applescript. I would seriously recommend reading lots of Applescript code as well, this is as important, if not more important, than reading these other resources. It is important to get a feel for what makes good and bad Applescript because there is an ocean of difference in readability between the good and the bad. You can find lots of code to read in Apple's material and at macscripter.net.
If you are going to be doing any, even remotely, serious development with Applescript, I highly recommend getting a copy of Script Debugger from Late Night Software.
Apple's own Script Editor and Applescript Studio (which is simple Script Editor wrapped in Xcode) offers no debugging tools, you'll rely solely on their (sometimes esoteric) error messages and your own cowboy debugging (the "log" command). Script Debugger picks up where Script Editor leaves off—proper debugging, code stepping, code completion—and also runs scripts much faster. I can learn a lot about how a an unfamilar application implements Applescript with Script Debugger because the dictionary reader will actually give the exact syntax needed to use a command in most cases.
I agree with the folks above. Start with Soghoian's book. Very good. And then The Definitive Guide. Also, get on the Applescript Discussion list. Very good, with the author of the Definitive Guide being a major contributor to that.
And get ScriptDebugger 5. Absolutely necessary for serious Applescripting. I use it to automate Photoshop, Filemaker, and my Mac do to pretty complex things in managing photos and metadata for my employer.
Another suggestion: while reading up on AppleScript, take a look at Appscript which makes the Apple Event technology underneath AppleScript available in Python, Ruby, or even Objective-C. Why? Many people find the AppleScript language itself lacking compared to more modern languages like Python or Ruby. But whichever way you choose, be prepared for a certain level of frustration. Much of the power of Apple Events and scripting comes from the richness of the data models that AppleScript-able applications can implement. Unfortunately, every AppleScript-able application has its own implementation quirks and often there is a fair amount of hunting and pecking guesswork until you find the incantation that works for a particular operation. HAS touches on some of this here.
Appscript also provides some developer tools, like ASDictionary which exports applications' terminology as plain text or HTML files and in AppleScript or appscript formats.
http://osxnotes.net/applescript.html: notes, scripts, and AppleScript oneliners
https://developer.apple.com/library/mac/documentation/applescript/conceptual/applescriptlangguide/AppleScriptLanguageGuide.pdf
http://www.apeth.net/matt/downloads/ASTDG2Scripts.txt: example scripts from AppleScript: The Definitive Guide
http://www.amazon.com/Learn-AppleScript-Comprehensive-Scripting-Automation/dp/1430223618: my favorite book about AppleScript
http://www.cs.utexas.edu/~wcook/Drafts/2006/ashopl.pdf
http://www.cs.utexas.edu/~wcook/papers/AppleScript/AppleScript95.pdf
http://dl.acm.org/ft_gateway.cfm?id=1238845&type=pdf&path=%2F1240000%2F1238845%2Fsupp%2FAppleScript%2Epdf&supp=1&dwn=1&CFID=334275816&CFTOKEN=16694215
This isn't exactly a good place to start, but the document that I found most useful was a discussion on the design of AppleScript written by one of its original designers.
It mostly talks about the evolution of AppleScript from an idea to an implementation. There's a technical part in the middle, though, that really glued a lot of things together for me. I was interested to learn that most of AppleScript's design stemmed from limitations in MacOS at the time. There's also a bit at the end reflecting on how AppleScript has fared over the years.
I'm trying to learn as well.
Currently reading http://macscripter.net/viewtopic.php?id=24729
It's really good. But the link in the post is broken, so you have to search for the next post.
And my next stop is: http://mac.appstorm.net/how-to/applescript/the-ultimate-beginners-guide-to-applescript/
I'm in the middle of AppleScript 1-2-3 by Sal Soghoian who is in charge of the AppleScript program at Apple; step-by-step, good

Developer Setup for Starting Out with Cocoa/Mac Programming

I'd like to start experimenting with Cocoa and programming for Mac OSX. I'm not terribly concerned with Objective C syntax/constructs/bheaviors at this point, but more curious as to an efficient setup on in terms of an editor and/or IDE that will get me going quickly. Is there any IDE even remotely similar to Visual Studio (since that's where I've spent most of my time over the last 7 years) in terms of it's solution/project concept? Any other tools, tips, suggestions and/or resources to get up and experimenting quickly?
I'd like to avoid a lot of the intro stuff and get into things like "If you want to create an Mac desktop application, you can use Acme IDE and set up your project like this."
I was afraid Xcode was going to be the answer! :P I tried playing around with that -- literally just getting it off the DVD and just diving in with no idea what to expect (before I even knew that you used Objective C as the language). I figured, the wise guy that I am, that I could just sort of fumble around and get a simple app working ... wrong.
#Andrew - Thanks for the insight on those config settings. Based on my Xcode first impression, I think those may help.
I'd suggest you pick a fun little product and dive in. If you're looking for a book I'd suggest Cocoa Programming for Max OSX which is a very good introduction both to Objective-C and Cocoa.
XCode is pretty much the de facto IDE and free with OSX. It should be on your original install DVD. It's good but not as good as Visual Studio (sorry, it's really not).
As a long-time VS user I found the default XCode config a little odd and hard to adjust to, particularly the way a new floating window would open for every sourcefile. Some tweaks I found particularly helpful;
Settings/General -> All-In-One (unifies editor/debugger window)
Settings/General -> Open counterparts in same editor (single-window edit)
Settings/Debugging - "In Editor Debugger Controls"
Settings/Debugging - "Auto Clear Debug Console"
Settings/Key-binding - lots of binding to match VS (Ctrl+F5/Shift+F5,Shift+Home, Shift+End etc)
I find the debugger has some annoying issues such as breakpoints not correctly mapping to lines and exceptions aren't immediately trapped by the debugger. Nothing deal-breaking but a bit cumbersome.
I would recommend that you make use of the new property syntax that was introduced for Objective-C 2.0. They make for a heck of a lot less typing in many many places. They're limited to OSX 10.5 only though (yeah, language features are tied to OS versions which is a bit odd).
Also don't be fooled into downplaying the differences between C/C++ and Objective-C. They're very much related but ARE different languages. Try and start Objective-C without thinking about how you'd do X,Y,Z in C/C++. It'll make it a lot easier.
The first document to read and digest is the Mem management guide, understand this before moving on. This is a great guide to objective-c too. Infact the developer site at Apple is very good - but you would probably want to read the Hillegas book first.
In regards to Xcode vs Visual Studio - they are different. I wouldn't say one is better than the other - Windows developers come over from VS and expect it to be the same. This is just an arrogant attitude and please don't fall into this crowd. Having used VS since the AppStudio days and Xcode for a year or so now, both have strengths and weaknesses. Xcode is something that out of the box (and especially when coming from VS) doesn't seem that good, but once you start using and understanding it - it becomes very powerful.
Also, there are a lot more tools included with Xcode et al, such as Instruments and Shark that you simply can't get with VS, unless you open your wallet, and even then IMHO aren't as good.
Anyway, good luck. I still enjoy C#, but Objective-C/Cocoa somehow makes programming fun again once you get into it...
Don't bother digging up your OSX DVD as they've released a new version (3.1) of XCode since then.
First, you'll want to join Apple Developer Connection (it's free, and you need it to access their version of MSDN) - it uses your Apple ID so if you've ever had one for the itunes store etc, it's that same username/password
Once you've done that, click on downloads, then click on developer tools, to view this page, and go for the XCode 3.1 Developer DVD
One other suggestion: If you have feature or enhancement requests, or bugs that you've run into, be sure to file them at Apple's Bug Reporter. It's the best way for developers to communicate their needs to Apple, because every issue is tracked through the system.
You might try the demo of textmate and see how you like it for working with objective-c or any other type of text really. It will import xcode project settings so you can still compile and run from textmate rather than having to go back to xcode.
Xcode is the standard for editing source files, though you can use another editor in conjunction with the command line xcodebuild tool if you really want. I used Vim for all my Cocoa editing before finally giving in to Xcode. It's not the greatest IDE in the world, but it gets the job done, and the recent 3.x releases have had some nice improvements.
The real power tool of Cocoa development is Interface Builder. IB does not generate source code like many UI tools. Instead it manipulates real Cocoa views, controls, and objects which it then bundles into an archive (nib) that is loaded by your program at runtime. Most Cocoa programs use at least one nib file, and often many more.
No matter what IDE/editor combination you choose for hacking on source files, I recommend using IB where you can. Even if you're not a fan of other UI layout/generation tools, I suggest keeping an open mind, giving "the Cocoa way" a chance and at least learning what Interface Builder can do for your development process.
AFAIK, pretty much every OS X developer uses Xcode.
That, and Interface Builder for creating the GUIs.
FWIW, try to get hold of a copy of Hillegas's book, as it's a great introductory tutorial, and the reference Docs Apple provides really aren't. (They are generally very good reference docs, however).
Cocoa is huge. The hardest part of learning how to write apps on Mac is learning Cocoa. By the way. You do not need to know ObjC (though it helps tons). You can write Cocoa apps with Python or Ruby (right in the IDE).
I agree VS is a better IDE then Xcode. But if you throw in Interface Builder and all the other tools, I'm not so sure. Mac development is not about 1 giant IDE for everything. But VS is "kinder" on the developer then Xcode is.
Also if you want to do cross platform apps look at RealBasic. A fine tool (Basic though. But it runs on Linux too.) You'd be surprised how many Mac apps are written with RB.
I've heard the books currently out there are pretty out of date. The whole ecosystem seems to evolve very fast with dramatic changes made in every OS release.
He wrote a tutorial which pulls together some Apple documentation and other tutorials which should get you started. I think it covers the basics of using the IDE, writing simple apps, and then goes on to more advanced stuff.
I've been dabbling in Cocoa for the past couple years, and recently picked up Fritz Anderson's "Xcode 3 Unleashed." Highly recommended for getting into Xcode — especially with some of the big changes 3.0/Leopard brought.
Don't forget Hillegass' defacto Cocoa bible, "Cocoa Programming for Mac OS X - Third Edition."
#peter I don't know why you had trouble with getting a simple app working, right off the bat without doing anything your app gets a lot of benefits from the Cocoa framework. If you mean you were trying to do stuff like connect a button to an action and have it print a alert on screen or something like that then yes I could see where your going with it being difficult.
The problem for me starting with Cocoa many years back is that it was so different from anything else that it had a little bit of a learning curve. Whereas many other systems are compile time oriented Cocoa is very dynamic and runtime oriented. Once you get past learning how actions hook up to classes it just becomes a matter of learning how the Cocoa frameworks work.

Resources