4

I wasn't sure what to call the title, but I'm working on an Applescript that pulls/pushes information from iTunes and save it. During the development of my Applescript I figured that if I'm going to learn a language, I'm going learn Swift as I want to develop more advanced programs down the road. Even though Applescript is as useful, and what I'm doing is working in Applescript... I wanted to take the next step in Learning to develop programs and scripts using Swift. I want to be able to replicate what I was able to do in Applescript using Swift.

What I wanted to know is if there is a tutorial/guide on how to read data from the accessibility inspector/etc and control programs like applescript is capable of doing, etc...

To give you a background on what I was able to do in Applescript, it's a really basic version of Batch Apple ID Creator. I work for a small school district that is now using a MDM to manage the i-devices. Well, with the MDM, each device needs to have it's own apple ID and manually creating 500+ apple ID is not as much fun as it sounds. These are some one the steps that I recreated in Applescript.

  • Check to see if iTunes is running
  • Check to see if there is a current user logged in.
  • Click "Sign Out" under menu bar if current user is logged in.
  • Goto "Create Apple ID..."
  • etc.

I took the current script that is up on that Git repo and made some changes to it so it works with the latest version of iTunes (12.1.2). After making some changes, I figures I wanted to learn and started rewriting the script to fully understand how it works and to learn Applescript syntax.

Let me know if you need more information about what I'm trying to do.

Throdne

Throdne
  • 619
  • 2
  • 9
  • 27
  • Please include [what you have tried](http://mattgemmell.com/what-have-you-tried/) in your post. – milo526 Apr 30 '15 at 19:14
  • If you want to automate Mac applications, learn AppleScript. If you want to write Mac apps, learn Swift. There are plenty of [books](http://www.amazon.com/gp/search/keywords=applescript) on [both](http://www.amazon.com/gp/search/keywords=swift+language); I suggest you start with one of those. – foo Apr 30 '15 at 23:42
  • I suggest you learn both. If you want to control other applications from an application you wrote in swift you are most likely going to include applescript using applescript bridging syntax or commands in swift to run applescript commands. – markhunte May 01 '15 at 09:27
  • 1
    Thanks you for your posts! Like I said, "I want to be able to replicate what I was able to do in Applescript using Swift." I just need a little help figuring out what I need to look for as I don't know the proper terms. I was unable to come up with anything about Swift and application manipulation like searches. – Throdne May 01 '15 at 14:17
  • 1
    That's because application scripting in Swift is crap. The only supported option is Apple's Scripting Bridge framework, which is a pain to use in ObjC due to being a piece of broken, obfuscated, badly-supported crap that few people use and even fewer understand, and even more of a pain to use in Swift because it's not Swift-friendly. As to directly calling OS X's Accessibility APIs, those are plain C so not very Swift-friendly either. For what you're doing, stick to AppleScript. Yes it's a rubbish language for general coding, but blame Apple as they botched all the alternatives. – foo May 01 '15 at 15:53
  • Foo, Thanks for the info! I guess I'll have to continue this script in Applescript. The one thing I don't like about Applescript is there is really no real debugger. Xcode and Script editor doesn't have any Breakpoint functionality (etc.) for applescript. The only thing that I was able to find is a program called "Script Debugger" I did give that software a try. I really like it, but not for the $200 price point. Unless someone else has any recommendations?!?! Thanks again, Throdne – Throdne May 01 '15 at 16:48
  • 1
    Script Debugger is your only option; however, every AppleScripter who uses it absolutely swears by it. It certainly puts Apple's own Script Editor utterly to shame. BTW, $200 is not an unreasonable amount for a professional product: an MS Office business licence will set you back a similar amount, for example. Ask yourself how much an hour of your time is worth and divide into $200. If you regularly do AppleScript development, it should pay for itself in headache reduction and time saved. – foo May 01 '15 at 21:35
  • For those insisting on using Applescript: the unstated part of the problem is that Applescript has been barely-maintained since the 90s, way before OS X. It feels like a miracle that it's still limping along. So any easy way to use AppleEvents from an officially maintained language, like Swift, would be GREAT. (on the other hand, given that using other languages for AppleEvent management keeps being so hard, sounds like at any moment both AppleEvents and AppleScript might disappear...) – hmijail Oct 31 '22 at 03:56

2 Answers2

6

I am investigating this sort of thing myself. What amazes me is that you can use Swift as a scripting language. I've written several short scripts that use the simple 'shebang' line #!/usr/bin/swift

To access AppleScript functionality you almost have to call AppleScript (through Cocoa) from your Swift code.

Here's an example I found elsewhere:

Swift Code

let myAppleScript = "..."
var error: NSDictionary?
if let scriptObject = NSAppleScript(source: myAppleScript) {
    if let output: NSAppleEventDescriptor = scriptObject.executeAndReturnError(
                                                                       &error) {
        println(output.stringValue)
    } else if (error != nil) {
        println("error: \(error)")
    }
}

try searching for 'call AppleScript from Swift' (or Cocoa) or 'call AppleEvents from Swift' (or Cocoa).

  • Can you also provide, please an Apple Script to the example. I get an error for every Apple Script.. Thanks :) – Guy Kahlon Nov 03 '16 at 11:20
3

Each language has a specific purpose. AppleScript's strength is interprocess scripting through AppleEvents, while Swift's strength is programming large full-featured applications. With AppleScript, you can get the contents of a webpage from Safari using JavaScript, parse it with shell script, and send it in an e-mail with Mail. Most languages can't do such a thing as easily as you can do it in AppleScript (Swift included). They are complementary. You can do amazing things by learning both, but they each have their own limitations and strengths.