SlideShare a Scribd company logo
1 of 41
Download to read offline
Automate with Swift
CocoaConf DC, April 10, 2015
Tony Ingraldi
GitHub: tingraldi • Twitter: @TonyIngraldi
Itinerary
• Motivation: Why Automate?
• A bit of history
• Why Swift?
• Nuts and bolts
Show of Hands?
• Have you used…
• AppleScript?
• Automator?
• Other form of application scripting?
Why Automate?
• Improve productivity
• Avoid repetitive tasks
• Streamline workflows
• Opportunities for experimentation
• Add missing features to OS X apps!
Scriptable Applications
Demo
Sneak peak
Open Scripting Architecture
• Introduced way back in October 1993
• The state of the Mac looked like this
Beige!
Open Scripting Architecture
• Survived the transition to OS X
• Leveraged by OS X to launch applications, open
documents, etc.
• A standard mechanism for interapplication
communication
Language Options
• AppleScript (there from the beginning)
• JavaScript for Automation (as of Yosemite)
• Python (PyObjC, py-appscript)
• Ruby (RubyCocoa, RubyOSA)
• And now Swift!
Language Options
• AppleScript (there from the beginning)
• JavaScript for Automation (as of Yosemite)
• Python (PyObjC, py-appscript)
• Ruby (RubyCocoa, RubyOSA)
• And now Swift!
AppleScript
• A “natural language” programming model
• Primarily geared toward being the “glue”
between apps
• Limited inherent functionality
AppleScript
• Can be frustrating to people who are fluent in
“normal” programming languages
• Verbosity is a hallmark
set fileName to "name.jpg"
set sansExtension to text 1 thru ((offset of "." in fileName) - 1) of fileName
Swift
• A “primary” language
• Ready access to Cocoa
• Can run Swift scripts using #! convention
• A familiar coding style
let fileName = "name.jpg"
let sansExtension = fileName.stringByDeletingPathExtension
Scripting Bridge
• Introduced in Mac OS X Leopard (version 10.5)
• Provides high-level Objective-C access to
scriptable applications
• Can be leveraged in Swift, with a bit of extra
work
Scripting Bridge Recipe
• 1 part sdef
• 1 part sdp
• A pinch of manual intervention
• Automate to taste
Scripting Definition
• Scriptable applications include resources that
describe their scripting interface
• Can be in a variety of forms
• The sdef command-line utility extracts the
scripting definition
• Writes to standard output in XML format
Scripting Definition
• Using sdef
sdef /path/to/App.app > App.sdef
• For the most part, using sdef is “run and forget”
• Until you find out what’s wrong when you run sdp
• Some sdp warnings can be ignored
Scripting Definition
• Using sdp
sdp -fh --basename App App.sdef
Demo
sdef and sdp
Kicking the Tires
• Xcode
• Swift and ObjC command line targets
• Includes the header generated by sdp
• Experiment in main.swift and main.m
Demo
Kicking the tires in Xcode
Don’t Run with Scissors
• Using Objective-C header leads to pervasive
AnyObject typing
• Works, but can lead to ambiguity
• For properties, sometimes have to resort to
using valueForKey
• Sometimes leads to awkward method
invocation
An Alternative
• Create Swift protocols that cover the generated
Objective-C API
• 👍 Supports rich set of types
• 👍 API translation can be automated
• 👎 Requires optional declaration of all
properties and methods
Objective-C Excerpt
@interface AcornApplication : SBApplication
@property (copy, readonly) NSString *name;
- (SBElementArray *) windows;
- (NSString *) taunt;
@end
SBObject
@objc public protocol SBObjectProtocol:
NSObjectProtocol {
func get() -> AnyObject!
}
SBApplication
@objc public protocol SBApplicationProtocol:
SBObjectProtocol {
func activate()
var delegate:SBApplicationDelegate! { get set }
}
Application Protocol
@objc public protocol AcornApplication:
SBApplicationProtocol {
optional var name: String { get }
optional func windows() -> SBElementArray
optional func taunt() -> String
}
extension SBApplication : AcornApplication {}
Putting it Together
import ScriptingBridge
@objc public protocol AcornApplication : … {…}
extension SBApplication : AcornApplication {}
let acorn = SBApplication
.applicationWithBundleIdentifier(
“com.flyingmeat.Acorn4"
) as! AcornApplication
acorn.taunt!()
Acorn Taunt
Plan of Attack
• Create frameworks for target applications
• Install in /Library/Frameworks
• Write scripts instead of compiled code
By the Way
• Playgrounds don’t fit well
• The Swift REPL could spontaneously combust at
any moment
• What’s a scripter to do?
• Use a text editor
• Edit, run, repeat
Script Invocation
• Hash-bang on the first line
#!/usr/bin/xcrun swift -F /Library/Frameworks
• chmod +x SomeScript.swift
A Little Help
https://github.com/tingraldi/SwiftScripting
• Header conversion utilities
• Sample Application Scripting Frameworks
• Sample Scripts
• Scripting Utilities Framework
Demo
A Scripting Framework
A Little More Help
import ScriptingBridge
@objc public protocol AcornApplication {…}
extension SBApplication : AcornApplication {}
let acorn = SBApplication
.applicationWithBundleIdentifier(
"com.flyingmeat.Acorn4"
) as! AcornApplication
acorn.taunt!()
A Little More Help
import ScriptingBridge
import AcornScripting // Acorn Swift protocols
let acorn = SBApplication
.applicationWithBundleIdentifier(
"com.flyingmeat.Acorn4"
) as! AcornApplication
acorn.taunt!()
A Little More Help
import ScriptingUtilities // Helper framework
import AcornScripting // Acorn Swift protocols
let acorn = Application(name: "Acorn")
as! AcornApplication
acorn.taunt!()
Where to Put Scripts?
• Short answer: anywhere
• Some applications have special script folders
• Scripting Menu
• Invoke via an Automator Service
Demo
Scripting Menu, etc.
Summary
• Automate for productivity
• Add “missing features”
• Reinforce/leverage Swift expertise
Questions?

More Related Content

What's hot

java8-patterns
java8-patternsjava8-patterns
java8-patternsJustin Lin
 
Ruby, the language of devops
Ruby, the language of devopsRuby, the language of devops
Ruby, the language of devopsRob Kinyon
 
A Taste of Pharo 7.0
A Taste of Pharo 7.0A Taste of Pharo 7.0
A Taste of Pharo 7.0ESUG
 
TypeScript for Alfresco and CMIS - Alfresco DevCon 2012 San Jose
TypeScript for Alfresco and CMIS - Alfresco DevCon 2012 San JoseTypeScript for Alfresco and CMIS - Alfresco DevCon 2012 San Jose
TypeScript for Alfresco and CMIS - Alfresco DevCon 2012 San JoseSteve Reiner
 
Why I don’t want to develop iOS apps in Objective C
Why I don’t want to develop iOS apps in Objective CWhy I don’t want to develop iOS apps in Objective C
Why I don’t want to develop iOS apps in Objective CSeniorDevOnly
 
Type Profiler: An Analysis to guess type signatures
Type Profiler: An Analysis to guess type signaturesType Profiler: An Analysis to guess type signatures
Type Profiler: An Analysis to guess type signaturesmametter
 
Practical byteman sample 20131128
Practical byteman sample 20131128Practical byteman sample 20131128
Practical byteman sample 20131128Jooho Lee
 
Cookpad Hackarade #04: Create Your Own Interpreter
Cookpad Hackarade #04: Create Your Own InterpreterCookpad Hackarade #04: Create Your Own Interpreter
Cookpad Hackarade #04: Create Your Own Interpretermametter
 
Wonders of Golang
Wonders of GolangWonders of Golang
Wonders of GolangKartik Sura
 
Functional Programming in JavaScript
Functional Programming in JavaScriptFunctional Programming in JavaScript
Functional Programming in JavaScriptTroy Miles
 
Reflection in Pharo5
Reflection in Pharo5Reflection in Pharo5
Reflection in Pharo5Marcus Denker
 
Alberto Maria Angelo Paro - Isomorphic programming in Scala and WebDevelopmen...
Alberto Maria Angelo Paro - Isomorphic programming in Scala and WebDevelopmen...Alberto Maria Angelo Paro - Isomorphic programming in Scala and WebDevelopmen...
Alberto Maria Angelo Paro - Isomorphic programming in Scala and WebDevelopmen...Codemotion
 
Clang Analyzer Tool Review
Clang Analyzer Tool ReviewClang Analyzer Tool Review
Clang Analyzer Tool ReviewDoug Schuster
 
Take Flight - Using Fly with the Play Framework
Take Flight - Using Fly with the Play FrameworkTake Flight - Using Fly with the Play Framework
Take Flight - Using Fly with the Play FrameworkAsher Glynn
 
javerosmx-2015-marzo-groovy-java8-comparison
javerosmx-2015-marzo-groovy-java8-comparisonjaverosmx-2015-marzo-groovy-java8-comparison
javerosmx-2015-marzo-groovy-java8-comparisonDomingo Suarez Torres
 

What's hot (20)

java8-patterns
java8-patternsjava8-patterns
java8-patterns
 
Intro dotnet
Intro dotnetIntro dotnet
Intro dotnet
 
Roslyn
RoslynRoslyn
Roslyn
 
Golang
GolangGolang
Golang
 
Ruby, the language of devops
Ruby, the language of devopsRuby, the language of devops
Ruby, the language of devops
 
A Taste of Pharo 7.0
A Taste of Pharo 7.0A Taste of Pharo 7.0
A Taste of Pharo 7.0
 
Variables in Pharo5
Variables in Pharo5Variables in Pharo5
Variables in Pharo5
 
TypeScript for Alfresco and CMIS - Alfresco DevCon 2012 San Jose
TypeScript for Alfresco and CMIS - Alfresco DevCon 2012 San JoseTypeScript for Alfresco and CMIS - Alfresco DevCon 2012 San Jose
TypeScript for Alfresco and CMIS - Alfresco DevCon 2012 San Jose
 
Why I don’t want to develop iOS apps in Objective C
Why I don’t want to develop iOS apps in Objective CWhy I don’t want to develop iOS apps in Objective C
Why I don’t want to develop iOS apps in Objective C
 
Type Profiler: An Analysis to guess type signatures
Type Profiler: An Analysis to guess type signaturesType Profiler: An Analysis to guess type signatures
Type Profiler: An Analysis to guess type signatures
 
Practical byteman sample 20131128
Practical byteman sample 20131128Practical byteman sample 20131128
Practical byteman sample 20131128
 
Crystal
CrystalCrystal
Crystal
 
Cookpad Hackarade #04: Create Your Own Interpreter
Cookpad Hackarade #04: Create Your Own InterpreterCookpad Hackarade #04: Create Your Own Interpreter
Cookpad Hackarade #04: Create Your Own Interpreter
 
Wonders of Golang
Wonders of GolangWonders of Golang
Wonders of Golang
 
Functional Programming in JavaScript
Functional Programming in JavaScriptFunctional Programming in JavaScript
Functional Programming in JavaScript
 
Reflection in Pharo5
Reflection in Pharo5Reflection in Pharo5
Reflection in Pharo5
 
Alberto Maria Angelo Paro - Isomorphic programming in Scala and WebDevelopmen...
Alberto Maria Angelo Paro - Isomorphic programming in Scala and WebDevelopmen...Alberto Maria Angelo Paro - Isomorphic programming in Scala and WebDevelopmen...
Alberto Maria Angelo Paro - Isomorphic programming in Scala and WebDevelopmen...
 
Clang Analyzer Tool Review
Clang Analyzer Tool ReviewClang Analyzer Tool Review
Clang Analyzer Tool Review
 
Take Flight - Using Fly with the Play Framework
Take Flight - Using Fly with the Play FrameworkTake Flight - Using Fly with the Play Framework
Take Flight - Using Fly with the Play Framework
 
javerosmx-2015-marzo-groovy-java8-comparison
javerosmx-2015-marzo-groovy-java8-comparisonjaverosmx-2015-marzo-groovy-java8-comparison
javerosmx-2015-marzo-groovy-java8-comparison
 

Similar to CocoaConf DC - Automate with Swift - Tony Ingraldi

Try! Swift Tokyo2017
Try! Swift Tokyo2017Try! Swift Tokyo2017
Try! Swift Tokyo2017Amy Cheong
 
iOS Application Security
iOS Application SecurityiOS Application Security
iOS Application SecurityEgor Tolstoy
 
[2015/2016] Apache Cordova
[2015/2016] Apache Cordova[2015/2016] Apache Cordova
[2015/2016] Apache CordovaIvano Malavolta
 
Build Great Networked APIs with Swift, OpenAPI, and gRPC
Build Great Networked APIs with Swift, OpenAPI, and gRPCBuild Great Networked APIs with Swift, OpenAPI, and gRPC
Build Great Networked APIs with Swift, OpenAPI, and gRPCTim Burks
 
State ofappdevelopment
State ofappdevelopmentState ofappdevelopment
State ofappdevelopmentgillygize
 
Dependent things dependency management for apple sw - slideshare
Dependent things   dependency management for apple sw - slideshareDependent things   dependency management for apple sw - slideshare
Dependent things dependency management for apple sw - slideshareCavelle Benjamin
 
Xcode, Basics and Beyond
Xcode, Basics and BeyondXcode, Basics and Beyond
Xcode, Basics and Beyondrsebbe
 
Shifting landscape of mobile automation, and the future of Appium - Jonathan ...
Shifting landscape of mobile automation, and the future of Appium - Jonathan ...Shifting landscape of mobile automation, and the future of Appium - Jonathan ...
Shifting landscape of mobile automation, and the future of Appium - Jonathan ...Applitools
 
Mobile Fest 2018. Алексей Лизенко. Make your project great again
Mobile Fest 2018. Алексей Лизенко. Make your project great againMobile Fest 2018. Алексей Лизенко. Make your project great again
Mobile Fest 2018. Алексей Лизенко. Make your project great againMobileFest2018
 
Bootstrapping iPhone Development
Bootstrapping iPhone DevelopmentBootstrapping iPhone Development
Bootstrapping iPhone DevelopmentThoughtWorks
 
API workshop: Introduction to APIs (TC Camp)
API workshop: Introduction to APIs (TC Camp)API workshop: Introduction to APIs (TC Camp)
API workshop: Introduction to APIs (TC Camp)Tom Johnson
 
차세대컴파일러, VM의미래: 애플 오픈소스 LLVM
차세대컴파일러, VM의미래: 애플 오픈소스 LLVM차세대컴파일러, VM의미래: 애플 오픈소스 LLVM
차세대컴파일러, VM의미래: 애플 오픈소스 LLVMJung Kim
 
Rapid application development with spring roo j-fall 2010 - baris dere
Rapid application development with spring roo   j-fall 2010 - baris dereRapid application development with spring roo   j-fall 2010 - baris dere
Rapid application development with spring roo j-fall 2010 - baris dereBaris Dere
 
A Brief History of OWIN
A Brief History of OWINA Brief History of OWIN
A Brief History of OWINRyan Riley
 
Cordova: APIs and instruments
Cordova: APIs and instrumentsCordova: APIs and instruments
Cordova: APIs and instrumentsIvano Malavolta
 
Hyperion EPM APIs - Added value from HFM, Workspace, FDM, Smartview, and Shar...
Hyperion EPM APIs - Added value from HFM, Workspace, FDM, Smartview, and Shar...Hyperion EPM APIs - Added value from HFM, Workspace, FDM, Smartview, and Shar...
Hyperion EPM APIs - Added value from HFM, Workspace, FDM, Smartview, and Shar...Charles Beyer
 

Similar to CocoaConf DC - Automate with Swift - Tony Ingraldi (20)

Try! Swift Tokyo2017
Try! Swift Tokyo2017Try! Swift Tokyo2017
Try! Swift Tokyo2017
 
iOS Application Security
iOS Application SecurityiOS Application Security
iOS Application Security
 
[2015/2016] Apache Cordova
[2015/2016] Apache Cordova[2015/2016] Apache Cordova
[2015/2016] Apache Cordova
 
iOS Application Exploitation
iOS Application ExploitationiOS Application Exploitation
iOS Application Exploitation
 
Build Great Networked APIs with Swift, OpenAPI, and gRPC
Build Great Networked APIs with Swift, OpenAPI, and gRPCBuild Great Networked APIs with Swift, OpenAPI, and gRPC
Build Great Networked APIs with Swift, OpenAPI, and gRPC
 
Xamarin v.Now
Xamarin v.NowXamarin v.Now
Xamarin v.Now
 
Apache Cordova 4.x
Apache Cordova 4.xApache Cordova 4.x
Apache Cordova 4.x
 
State ofappdevelopment
State ofappdevelopmentState ofappdevelopment
State ofappdevelopment
 
Apache Cordova
Apache CordovaApache Cordova
Apache Cordova
 
Dependent things dependency management for apple sw - slideshare
Dependent things   dependency management for apple sw - slideshareDependent things   dependency management for apple sw - slideshare
Dependent things dependency management for apple sw - slideshare
 
Xcode, Basics and Beyond
Xcode, Basics and BeyondXcode, Basics and Beyond
Xcode, Basics and Beyond
 
Shifting landscape of mobile automation, and the future of Appium - Jonathan ...
Shifting landscape of mobile automation, and the future of Appium - Jonathan ...Shifting landscape of mobile automation, and the future of Appium - Jonathan ...
Shifting landscape of mobile automation, and the future of Appium - Jonathan ...
 
Mobile Fest 2018. Алексей Лизенко. Make your project great again
Mobile Fest 2018. Алексей Лизенко. Make your project great againMobile Fest 2018. Алексей Лизенко. Make your project great again
Mobile Fest 2018. Алексей Лизенко. Make your project great again
 
Bootstrapping iPhone Development
Bootstrapping iPhone DevelopmentBootstrapping iPhone Development
Bootstrapping iPhone Development
 
API workshop: Introduction to APIs (TC Camp)
API workshop: Introduction to APIs (TC Camp)API workshop: Introduction to APIs (TC Camp)
API workshop: Introduction to APIs (TC Camp)
 
차세대컴파일러, VM의미래: 애플 오픈소스 LLVM
차세대컴파일러, VM의미래: 애플 오픈소스 LLVM차세대컴파일러, VM의미래: 애플 오픈소스 LLVM
차세대컴파일러, VM의미래: 애플 오픈소스 LLVM
 
Rapid application development with spring roo j-fall 2010 - baris dere
Rapid application development with spring roo   j-fall 2010 - baris dereRapid application development with spring roo   j-fall 2010 - baris dere
Rapid application development with spring roo j-fall 2010 - baris dere
 
A Brief History of OWIN
A Brief History of OWINA Brief History of OWIN
A Brief History of OWIN
 
Cordova: APIs and instruments
Cordova: APIs and instrumentsCordova: APIs and instruments
Cordova: APIs and instruments
 
Hyperion EPM APIs - Added value from HFM, Workspace, FDM, Smartview, and Shar...
Hyperion EPM APIs - Added value from HFM, Workspace, FDM, Smartview, and Shar...Hyperion EPM APIs - Added value from HFM, Workspace, FDM, Smartview, and Shar...
Hyperion EPM APIs - Added value from HFM, Workspace, FDM, Smartview, and Shar...
 

Recently uploaded

why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Clustering techniques data mining book ....
Clustering techniques data mining book ....Clustering techniques data mining book ....
Clustering techniques data mining book ....ShaimaaMohamedGalal
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfCionsystems
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 

Recently uploaded (20)

why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Clustering techniques data mining book ....
Clustering techniques data mining book ....Clustering techniques data mining book ....
Clustering techniques data mining book ....
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdf
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 

CocoaConf DC - Automate with Swift - Tony Ingraldi

  • 1. Automate with Swift CocoaConf DC, April 10, 2015 Tony Ingraldi GitHub: tingraldi • Twitter: @TonyIngraldi
  • 2. Itinerary • Motivation: Why Automate? • A bit of history • Why Swift? • Nuts and bolts
  • 3. Show of Hands? • Have you used… • AppleScript? • Automator? • Other form of application scripting?
  • 4. Why Automate? • Improve productivity • Avoid repetitive tasks • Streamline workflows • Opportunities for experimentation • Add missing features to OS X apps!
  • 7. Open Scripting Architecture • Introduced way back in October 1993 • The state of the Mac looked like this Beige!
  • 8. Open Scripting Architecture • Survived the transition to OS X • Leveraged by OS X to launch applications, open documents, etc. • A standard mechanism for interapplication communication
  • 9. Language Options • AppleScript (there from the beginning) • JavaScript for Automation (as of Yosemite) • Python (PyObjC, py-appscript) • Ruby (RubyCocoa, RubyOSA) • And now Swift!
  • 10. Language Options • AppleScript (there from the beginning) • JavaScript for Automation (as of Yosemite) • Python (PyObjC, py-appscript) • Ruby (RubyCocoa, RubyOSA) • And now Swift!
  • 11. AppleScript • A “natural language” programming model • Primarily geared toward being the “glue” between apps • Limited inherent functionality
  • 12. AppleScript • Can be frustrating to people who are fluent in “normal” programming languages • Verbosity is a hallmark set fileName to "name.jpg" set sansExtension to text 1 thru ((offset of "." in fileName) - 1) of fileName
  • 13. Swift • A “primary” language • Ready access to Cocoa • Can run Swift scripts using #! convention • A familiar coding style let fileName = "name.jpg" let sansExtension = fileName.stringByDeletingPathExtension
  • 14. Scripting Bridge • Introduced in Mac OS X Leopard (version 10.5) • Provides high-level Objective-C access to scriptable applications • Can be leveraged in Swift, with a bit of extra work
  • 15. Scripting Bridge Recipe • 1 part sdef • 1 part sdp • A pinch of manual intervention • Automate to taste
  • 16. Scripting Definition • Scriptable applications include resources that describe their scripting interface • Can be in a variety of forms • The sdef command-line utility extracts the scripting definition • Writes to standard output in XML format
  • 17. Scripting Definition • Using sdef sdef /path/to/App.app > App.sdef • For the most part, using sdef is “run and forget” • Until you find out what’s wrong when you run sdp • Some sdp warnings can be ignored
  • 18. Scripting Definition • Using sdp sdp -fh --basename App App.sdef
  • 20. Kicking the Tires • Xcode • Swift and ObjC command line targets • Includes the header generated by sdp • Experiment in main.swift and main.m
  • 22. Don’t Run with Scissors • Using Objective-C header leads to pervasive AnyObject typing • Works, but can lead to ambiguity • For properties, sometimes have to resort to using valueForKey • Sometimes leads to awkward method invocation
  • 23. An Alternative • Create Swift protocols that cover the generated Objective-C API • 👍 Supports rich set of types • 👍 API translation can be automated • 👎 Requires optional declaration of all properties and methods
  • 24. Objective-C Excerpt @interface AcornApplication : SBApplication @property (copy, readonly) NSString *name; - (SBElementArray *) windows; - (NSString *) taunt; @end
  • 25. SBObject @objc public protocol SBObjectProtocol: NSObjectProtocol { func get() -> AnyObject! }
  • 26. SBApplication @objc public protocol SBApplicationProtocol: SBObjectProtocol { func activate() var delegate:SBApplicationDelegate! { get set } }
  • 27. Application Protocol @objc public protocol AcornApplication: SBApplicationProtocol { optional var name: String { get } optional func windows() -> SBElementArray optional func taunt() -> String } extension SBApplication : AcornApplication {}
  • 28. Putting it Together import ScriptingBridge @objc public protocol AcornApplication : … {…} extension SBApplication : AcornApplication {} let acorn = SBApplication .applicationWithBundleIdentifier( “com.flyingmeat.Acorn4" ) as! AcornApplication acorn.taunt!()
  • 30. Plan of Attack • Create frameworks for target applications • Install in /Library/Frameworks • Write scripts instead of compiled code
  • 31. By the Way • Playgrounds don’t fit well • The Swift REPL could spontaneously combust at any moment • What’s a scripter to do? • Use a text editor • Edit, run, repeat
  • 32. Script Invocation • Hash-bang on the first line #!/usr/bin/xcrun swift -F /Library/Frameworks • chmod +x SomeScript.swift
  • 33. A Little Help https://github.com/tingraldi/SwiftScripting • Header conversion utilities • Sample Application Scripting Frameworks • Sample Scripts • Scripting Utilities Framework
  • 35. A Little More Help import ScriptingBridge @objc public protocol AcornApplication {…} extension SBApplication : AcornApplication {} let acorn = SBApplication .applicationWithBundleIdentifier( "com.flyingmeat.Acorn4" ) as! AcornApplication acorn.taunt!()
  • 36. A Little More Help import ScriptingBridge import AcornScripting // Acorn Swift protocols let acorn = SBApplication .applicationWithBundleIdentifier( "com.flyingmeat.Acorn4" ) as! AcornApplication acorn.taunt!()
  • 37. A Little More Help import ScriptingUtilities // Helper framework import AcornScripting // Acorn Swift protocols let acorn = Application(name: "Acorn") as! AcornApplication acorn.taunt!()
  • 38. Where to Put Scripts? • Short answer: anywhere • Some applications have special script folders • Scripting Menu • Invoke via an Automator Service
  • 40. Summary • Automate for productivity • Add “missing features” • Reinforce/leverage Swift expertise