What is applescript and what is it used for? - macos

What is applescript and what is it used for?

Applescript's "big advantage" over other scripting languages (which, frankly are much better to use for most tasks) is that it can automate the action of other applications on your Mac (as long as the developer has provided an Applescript dictionary).
This means that, as Philip Regan described, you can get an application like Photoshop to perform a whole bunch of repetitive photo modifications and then tuck those photos away in a folder, for example.
Other than that, one of the notable features of Applescript is its "English-like" syntax. Apple intended for it to be used by everyday people and tried to make its syntax as close to English as they could. This intention ran into a lot of problems, not the least of which was getting the people who were creating Applescript dictionaries for their applications to respect this goal. Apple also originally intended for there to be an Applescript for other human languages as well and there was work done on a "French Applescript" if I recall correctly.
There are now libraries in scripting languages like Python and Ruby for interfacing with Applescript so you can control the applications using Python or Ruby syntax and not using Applescript syntax (which can be extremely annoying to learn for someone coming from a programming background because of its irregularities and "foreignness").
Apple's latest move to bring application scripting to the everyday user is Automator. I suggest you try it out, as it can be quite useful if your aims are encompassed by things everyone does (keep copies of webpages, etc, etc,)

AppleScript is a scripting language that can be used to automate actions on Macintosh computers.
Examples of actions that can be automated with AppleScript include filesystem operations, textual data parsing, running programs and invoking program functionality.

Applescript Wikipedia Entry is a good start

While I concur with the rest of the responders here—there are far too many other websites that have this information—I will tell you what I, personally, actually do with it to hopefully give a more practical understanding of what it can do as opposed to the academic or marketing definitions you'll get at other sites...
I manage a production department for a publishing company and I have dozens upon dozens of scripts that allow me to streamline and/or automate all sorts of processes including:
fully automated page layout of marketing materials, contact sheets, and simple book designs using Excel, XML, and InDesign.
large scale image conversions (typically hundreds of images) with Illustrator and Photoshop.
automatic clean up of files (names, types, and some folder structures) to prep them for archiving.
Essentially, I use Applescript to automate any repetative task so that my folks can do better things with their time than repeating the same mindless mouse clicks over and over again. We just drop the files onto the slave Mac, start the script, and it beeps when it's done.

Related

Applescript to grab text from a text file and highlight said text in Skim. Two failed attempts?

PDF's ubiquity, specially in academia, makes being able to highlight them (and saving these annotations) extremely important. Some academic journals (specially in law) allows the user to send journal articles to a Kindle reader, which makes reading and taking notes extremely easy. The question is how to take the underlined text from the MyClippings.txt file to the PDF. About a year ago I found that this is possible through an action in Adobe Acrobat Pro X which would parse a text file which is feeded to it and would highlight the relevant sections. The action takes advantage of the Search and Redact tool but instead of redacting, it highlights.
However, I would like to get out of the Adobe environment (for different reasons, one of which is that the Adobe reader is demanding resource wise and not-free). Skim in Mac OSX sounds like a good alternative for its support of AppleScript integration. I found two projects in GitHub which attempt to do this, but with both of them I failed.
my-clippings-to-pdf
Skim-AppleScript
Would anyone with knowledge in AppleScript take a look at that code and tell me if they look sound? It seems this would be a great functionality for integrating PDFs and ePub in a useful and meaningful way specially for academics.

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.

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

Script generation using GUI

Is there an example for a project a GUI fro script generation?
The idea is to let a non-programmer use a front end to enter command and simple logic that will translated to runnable
scripts.
The programming language does not meter.
thanks
I would use Qt + python, but the constrains you give are too general. simple logic could not be simple at all.
One of the best example of a GUI that generates scripts that I've seen so far is Apple's Automator. It's good not in the sense that it is has lots of features but that it is intuitive enough that many non-programmers use it to automate tasks. It is basically a flashy implementation of the unix pipe paradigm.
Another successful tool in term of getting non-programmers to write programs is Adobe Photoshop's Actions. It also basically emulates the unix pipe paradigm.
I've personally never seen anything that breaks the pipe paradigm successfully convince non-programmers to write programs. Apart from Excel spreadsheet equations of course - but that is just horrifying to write programs in.
There's no script behind the scenes, but the Grasshopper plug-in for the Rhinoceros CAD system allows you to create algorithms purely visually.
Grasshopper User Forums
It's got an extensive Video and Image gallery which often features the GUI.
I assume you ask for education purposes. Then LOGO is a good option (targeted towards children though).
Edit:
As you say, you want such a thing for an enterprise-like application. I'd go for some markup format such as YAML or XML. The script would be directly editable and be in a well-known language such as Python or Groovy. There might be existing GUI tools for markup languages. If you take this approach, please let me know good examples of such GUI tools.
Specifically for business rules of course there are things like Drools.

Macro/Scripting language for non-developers with a simple GUI-based editor

We wish to provide people to be able to add some logic to their accounts(say, given a few arguments, how to compute a particular result). So, essentially, this would be tantamount to writing simple business rules with support for conditionals and expressions. However, the challenge is to provide them a simple online editor where they can create the logic (preferably) by completely visual means (drag/drop Expr-tree nodes maybe -- kinda like Y! pipes).
Does anybody know of a scripting/macro/domain-specific language that lets people do this? The challenge is the visual editor, since we don't wish to invest in developing the UI to do the editing. The basic requirements would be:
1. Embedded into another language, or run securely (no reboot -n or <JUNK-DANGEROUS-COMMAND> >> ~/.bashrc)
2. Easily accessible to users without coding background (no need of any advanced features)
3. Preferably have a simple GUI based editor to create the logic programs accessible to non-developers (kinda like spreadsheets)
4. Some ability to generate compile-time warnings (invalid code) would be good (Type safety?)
5. Ability to embed some data before execution which is available to the interpreter (Eg., name, birthday, amount)
Anybody tried doing something like this and got any ideas? I looked at Lua, Io, Python, Ruby and a host of others, but the challenge essentially is that I don't think non-programmers will be able to understand the code all that much. Something that could be added via "meta-programming" to say a Ruby would be good as well, if an editor could be easily developed!
As a matter fact, Microsoft is developing Oslo, which is right up your alley.
Chris Sells has been writing a lot about it recently.
It is designed to be a way to author DSLs and also to visually author these models with a graphical tool called Quadrant. Sounds very very similar to what you are looking for.
Open source wise, Ruby I think can be close, as you can see if you look at _whytheluckystiff's Try Ruby or Hackety.
I don't think you'll find anything that isn't too generic, especially regarding the GUI editor. There's no generic tools as far as I know that will be able to automatically interface with your program and be able to query data from it and interpret the script into commands in your software -- if there is I'd like to have a copy. Not being flippant, but you will have to do some (probably alot) of work to get this working. It will probably result in you writing a custom DSL.
I would take a look at PowerShell. You could surface all the activities a user would like to script in a very readable way.
There is some talk of using PowerShell to create a DSL on the PowerShell team blog and Bruce Payette, the technical lead, talks about this in his book Windows PowerShell in Action from Manning.
At the other end of the scale is to write something simple as a HyperText Application (HTA) -- assuming Windows of course -- along the lines of my Clive tool. The article on the blog doesn't mention the HTA version, but essentially I could enter VBScript-ish code into one textarea and interpret it on the spot, output going into another text area on the form.
With HTAs giving you all the form control of HTML, plus the DOM, you could come up with something interesting fairly quickly.

Resources