CuuDuongThanCong.com https://fb.com/tailieudientucntt NW.js Essentials Build native desktop applications for Windows, Mac OS, or Linux using the latest web technologies Alessandro Benoit BIRMINGHAM - MUMBAI CuuDuongThanCong.com https://fb.com/tailieudientucntt NW.js Essentials Copyright © 2015 Packt Publishing All rights reserved No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews Every effort has been made in the preparation of this book to ensure the accuracy of the information presented However, the information contained in this book is sold without warranty, either express or implied Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals However, Packt Publishing cannot guarantee the accuracy of this information First published: May 2015 Production reference: 1190515 Published by Packt Publishing Ltd Livery Place 35 Livery Street Birmingham B3 2PB, UK ISBN 978-1-78528-086-3 www.packtpub.com CuuDuongThanCong.com https://fb.com/tailieudientucntt Credits Author Alessandro Benoit Reviewers Project Coordinator Harshal Ved Proofreaders Dan Bendell Stephen Copestake Marco Fabbri Safis Editing Julio Freitas Indexer Commissioning Editor Mariammal Chettiyar Amarabha Banerjee Graphics Acquisition Editor Reshma Raman Content Development Editor Gaurav Sharma Technical Editor Humera Shaikh Disha Haria Abhinash Sahu Production Coordinator Alwin Roy Cover Work Alwin Roy Copy Editor Sarang Chari CuuDuongThanCong.com https://fb.com/tailieudientucntt About the Author Alessandro Benoit is a 31-year-old web developer from Italy He currently works both with backend and frontend technologies, ranging from PHP development, mostly on WordPress and Laravel, to web design and building open source jQuery plugins He's also an early adopter of NW.js and the developer of Nuwk!, an open source application that simplifies the building process of NW.js applications on Mac OS X CuuDuongThanCong.com https://fb.com/tailieudientucntt Acknowledgments NW.js Essentials is my first attempt at becoming a technical book writer Writing it has been fun and exciting but also quite harsh There were moments between work and personal issues when I thought I couldn't make it But here we are! What I want to now is thank all the wonderful people who have accompanied me on this journey First of all, I want to thank the whole open source community These people are the reason I'm in information technology in the first place I can still remember when I was 14 and some guy on IRC was teaching me about breaking NetBios to gain access to remote hosts of Microsoft Windows (I swear, I've never done any harm); that was the first time I realized how thrilling technology was to me I really can't help being grateful to Roger Wang for all the effort he's taken to make NW.js an easy and stable environment to bring web technologies to our desktops I want to personally thank Reshma Raman and Gaurav Sharma, my editors at Packt Publishing I have never met them in person, but still they have been positive and supportive during the whole writing process Thanks to Marco Fabbri, Dan Bendell, and Julio Freitas, for the great job they have done in reviewing this book I have to thank Abramo Capozzolo, my employer at Comodolab, for being so open to the use of the latest technologies and for putting people and research above revenues And of course, I would like to thank my colleague and friend, Christian Pucci, for bringing me in, in the first place Thanks to my life partner, Elisa Rocchi, for being present and always understanding despite my recent mood swings And thanks to my best friend, Andrea Valli, for still being my friend after months of absence Thanks to my mother, Patrizia Capacci, for loving me unconditionally Finally, I want to dedicate the publication of this book to an old friend who unfortunately is no longer here to share this joy This is also for you, Andrea Benvenuti CuuDuongThanCong.com https://fb.com/tailieudientucntt About the Reviewers Dan Bendell is a budding young developer currently at the University of Plymouth studying computing and game development He is set to finish his studies in 2016 after completing a year of work in the industry Upon finishing his academic endeavors, he wishes to pursue his dream of either creating a start-up or working within a game company that will allow him to create games that truly engage his audience and work with a variety of new people on a daily basis Marco Fabbri is an experienced software engineer and former professor of distributed systems (middleware for multi-agent systems, ReST architecture, and Node.js) at the University of Bologna, Italy He's also an active contributor in NW.js codebase Julio Freitas completed his graduation in computer science with specialization in information systems and technology He has been a developer of web applications since the year 2000 He worked as a developer and Unix systems administrator in projects of grid computing with Java and PHP for years at the Center for Weather Forecasting and Climate Studies/National Institute for Space Research (CPTEC/ INPE), Brazil He currently resides in England, where he works in a web systems company, and he is now creating his own start-up and acting as a full-stack web developer in projects focused on API development and security and applications for mobile devices using the MEAN stack and Ionic Julio has also reviewed JavaScript Regular Expressions, Loiane Groner, Packt Publishing CuuDuongThanCong.com https://fb.com/tailieudientucntt www.PacktPub.com Support files, eBooks, discount offers, and more For support files and downloads related to your book, please visit www.PacktPub.com Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub com and as a print book customer, you are entitled to a discount on the eBook copy Get in touch with us at service@packtpub.com for more details At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks https://www2.packtpub.com/books/subscription/packtlib Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library Here, you can search, access, and read Packt's entire library of books Why subscribe? • Fully searchable across every book published by Packt • Copy and paste, print, and bookmark content • On demand and accessible via a web browser Free access for Packt account holders If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view entirely free books Simply use your login credentials for immediate access CuuDuongThanCong.com https://fb.com/tailieudientucntt CuuDuongThanCong.com https://fb.com/tailieudientucntt Table of Contents Preface v Chapter 1: Meet NW.js NW.js under the hood Features and drawbacks of NW.js NW.js – usage scenarios Popular NW.js applications Downloading and installing NW.js Installing NW.js on Mac OS X Installing NW.js on Microsoft Windows Installing NW.js on Linux Development tools Writing and running your first "Hello World" app 10 Running NW.js applications on Sublime Text 12 Running NW.js applications on Microsoft Windows 12 Running NW.js applications on Mac OS 13 Running NW.js applications on Linux 13 Summary 14 Chapter 2: NW.js Native UI APIs The App API – the core of your applications Opening a file in your application natively Accessing the application data folder path Accessing the manifest file data Best practices for closing applications Registering system-wide hotkeys Other app APIs The Window API – working with windows on NW.js Instantiating a new window object Window – setting size and position of windows [i] CuuDuongThanCong.com https://fb.com/tailieudientucntt 15 17 18 19 20 21 22 23 24 24 26 ... styles, and grunt-node-webkit-builder for building node-webkit-tomster-seed Download link: https://github.com/Kerrick/node-webkit-tomster-seed This is a fork of node-webkit-hipster-seed that uses... boilerplates about 159 angular-desktop-app 160 node-webkit-boilerplate 160 node-webkit-hipster-seed 159 node-webkit-tomster-seed 160 nw-boilerplate 160 Browser Web APIs 63 built-in Mac menu 40 C Cellist... peer-to-peer NW.js applications: http://laike9m.com/blog/atutorial-on-using-peerjs-in-node-webkit-app,57/ • Getting started with NW.js apps: http://thejackalofjavascript.com/ getting-started-with-node-webkit-apps/