Yes it looks like you can https://majestysoftware.wordpress.com/2015/03/31/swift-scripting-part-1/ However it's not something Apple has made easy.
To automate the Mac desktop you usually would use apple script. https://support.apple.com/en-us/HT202905
As of OSX 10.10 you can use JavaScript syntax to script the Mac. I think this is Apples Attempt to make scripting easier as Apple script can be a pain IMO. https://developer.apple.com/library/mac/releasenotes/InterapplicationCommunication/RN-JavaScriptForAutomation/Articles/Introduction.html