-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add AppleScript support * feat: get commands command * feat: add execute command to AppleScript * feat: implement custom coding keys to support AppleScript * feat: support old way of encoding data * feat: minor code improvements for AppleScript
- Loading branch information
1 parent
d2dbed5
commit bf8ef43
Showing
17 changed files
with
602 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// | ||
// ExecuteCommand.swift | ||
// MiniSim | ||
// | ||
// Created by Oskar Kwasniewski on 15/08/2023. | ||
// | ||
|
||
import Foundation | ||
import Cocoa | ||
|
||
class ExecuteCommand: NSScriptCommand { | ||
override func performDefaultImplementation() -> Any? { | ||
guard | ||
let platformArg = self.property(forKey: "platform") as? String, | ||
let platform = Platform(rawValue: platformArg), | ||
let tag = self.property(forKey: "commandTag") as? String, | ||
let commandName = self.property(forKey: "commandName") as? String, | ||
let deviceName = self.property(forKey: "deviceName") as? String, | ||
let deviceId = self.property(forKey: "deviceId") as? String | ||
else { | ||
scriptErrorNumber = NSRequiredArgumentsMissingScriptError; | ||
return nil; | ||
} | ||
|
||
let device = Device(name: deviceName, ID: deviceId, platform: platform) | ||
let rawTag = Int(tag) ?? 0 | ||
|
||
if platform == .android { | ||
if let menuItem = AndroidSubMenuItem(rawValue: rawTag) { | ||
DeviceService.handleAndroidAction(device: device, commandTag: menuItem, itemName: commandName) | ||
} | ||
|
||
return nil; | ||
} | ||
|
||
|
||
if let menuItem = IOSSubMenuItem(rawValue: rawTag) { | ||
DeviceService.handleiOSAction(device: device, commandTag: menuItem, itemName: commandName) | ||
} | ||
|
||
|
||
return nil; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// | ||
// GetCommands.swift | ||
// MiniSim | ||
// | ||
// Created by Oskar Kwasniewski on 09/07/2023. | ||
// | ||
|
||
import Foundation | ||
import Cocoa | ||
|
||
class GetCommands: NSScriptCommand { | ||
override func performDefaultImplementation() -> Any? { | ||
guard | ||
let argument = self.property(forKey: "platform") as? String, | ||
let platform = Platform(rawValue: argument) | ||
else { | ||
scriptErrorNumber = NSRequiredArgumentsMissingScriptError; | ||
return nil; | ||
} | ||
|
||
do { | ||
var commands: [Command] = [] | ||
|
||
switch platform { | ||
case .android: | ||
commands = AndroidSubMenuItem.allCases.compactMap { $0.CommandItem } | ||
case .ios: | ||
commands = IOSSubMenuItem.allCases.compactMap { $0.CommandItem } | ||
} | ||
|
||
let customCommandTag = platform == .android ? AndroidSubMenuItem.customCommand.rawValue : IOSSubMenuItem.customCommand.rawValue | ||
|
||
let customCommands = DeviceService.getCustomCommands(platform: platform).map({ | ||
Command(id: $0.id, name: $0.name, command: $0.command, icon: $0.icon, platform: $0.platform, needBootedDevice: $0.needBootedDevice, bootsDevice: $0.bootsDevice, tag: customCommandTag) | ||
}) | ||
|
||
commands.append(contentsOf: customCommands) | ||
|
||
return try self.encode(commands) | ||
|
||
} catch { | ||
scriptErrorNumber = NSInternalScriptError; | ||
return nil | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// | ||
// GetDevicesCommand.swift | ||
// MiniSim | ||
// | ||
// Created by Oskar Kwasniewski on 09/07/2023. | ||
// | ||
|
||
import Foundation | ||
import Cocoa | ||
|
||
class GetDevicesCommand: NSScriptCommand { | ||
override func performDefaultImplementation() -> Any? { | ||
guard | ||
let argument = self.property(forKey: "platform") as? String, | ||
let platform = Platform(rawValue: argument) | ||
else { | ||
scriptErrorNumber = NSRequiredArgumentsMissingScriptError; | ||
return nil; | ||
} | ||
|
||
do { | ||
if platform == .android { | ||
return try self.encode(DeviceService.getAndroidDevices()) | ||
} else { | ||
return try self.encode(DeviceService.getIOSDevices()) | ||
} | ||
|
||
} catch { | ||
scriptErrorNumber = NSInternalScriptError; | ||
return nil | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// | ||
// LaunchDeviceCommand.swift | ||
// MiniSim | ||
// | ||
// Created by Oskar Kwasniewski on 09/07/2023. | ||
// | ||
|
||
import Foundation | ||
import Cocoa | ||
|
||
class LaunchDeviceCommand: NSScriptCommand { | ||
override func performDefaultImplementation() -> Any? { | ||
guard let deviceName = self.property(forKey: "deviceName") as? String else { | ||
scriptErrorNumber = NSRequiredArgumentsMissingScriptError; | ||
return nil; | ||
} | ||
|
||
do { | ||
var devices: [Device] = [] | ||
try devices.append(contentsOf: DeviceService.getIOSDevices()) | ||
try devices.append(contentsOf: DeviceService.getAndroidDevices()) | ||
|
||
guard let device = devices.first(where: { $0.name == deviceName }) else { | ||
scriptErrorNumber = NSInternalScriptError; | ||
return nil | ||
} | ||
|
||
if device.booted { | ||
DeviceService.focusDevice(device) | ||
return nil | ||
} | ||
|
||
DispatchQueue.global(qos: .userInitiated).async { | ||
if device.platform == .android { | ||
try? DeviceService.launchDevice(name: device.name) | ||
} else { | ||
try? DeviceService.launchDevice(uuid: device.ID ?? "") | ||
} | ||
} | ||
|
||
return nil | ||
} catch { | ||
scriptErrorNumber = NSInternalScriptError; | ||
return nil | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE dictionary SYSTEM "file://localhost/System/Library/DTDs/sdef.dtd"> | ||
<dictionary title="MiniSim Terminology"> | ||
<suite name="Standard Suite" code="????" description="Common classes and commands for all applications."> | ||
<command name="quit" code="aevtquit" description="Quit the application."> | ||
<cocoa class="NSQuitCommand"/> | ||
</command> | ||
</suite> | ||
|
||
<suite name="MiniSim Commands Suite" code="MinS" description="MiniSim Commands Suite"> | ||
<command name="getDevices" code="MinSDevi"> | ||
<cocoa class="MiniSim.GetDevicesCommand"/> | ||
<access-group identifier="*"/> | ||
<parameter code="plat" name="platform" description="Platform to get. Can be either android | ios." type="text"> | ||
<cocoa key="platform"/> | ||
</parameter> | ||
<result type="text" description="JSON string with devices."/> | ||
</command> | ||
|
||
<command name="launchDevice" code="MinSLaun"> | ||
<cocoa class="MiniSim.LaunchDeviceCommand"/> | ||
<access-group identifier="*"/> | ||
<parameter code="devi" name="deviceName" description="Device name to launch." type="text"> | ||
<cocoa key="deviceName"/> | ||
</parameter> | ||
<result type="text" description="Response code"/> | ||
</command> | ||
|
||
<command name="getCommands" code="MinSGetc"> | ||
<cocoa class="MiniSim.GetCommands"/> | ||
<access-group identifier="*"/> | ||
<parameter code="plat" name="platform" description="Platform to get. Can be either android | ios." type="text"> | ||
<cocoa key="platform"/> | ||
</parameter> | ||
<result type="text" description="Response code"/> | ||
</command> | ||
|
||
<command name="executeCommand" code="MinExecu"> | ||
<cocoa class="MiniSim.ExecuteCommand"/> | ||
<access-group identifier="*"/> | ||
<parameter code="plat" name="platform" description="Platform to get. Can be either android | ios." type="text"> | ||
<cocoa key="platform"/> | ||
</parameter> | ||
<parameter code="comm" name="commandName" description="Name of the command." type="text"> | ||
<cocoa key="commandName"/> | ||
</parameter> | ||
<parameter code="ctag" name="commandTag" description="Tag of the command to execute. Look for: AndroidSubMenuItem or IOSSubMenuItem" type="text"> | ||
<cocoa key="commandTag"/> | ||
</parameter> | ||
<parameter code="devN" name="deviceName" description="Name of the device." type="text"> | ||
<cocoa key="deviceName"/> | ||
</parameter> | ||
<parameter code="devI" name="deviceId" description="Unique identifier of the device." type="text"> | ||
<cocoa key="deviceId"/> | ||
</parameter> | ||
<result type="text" description="Response code"/> | ||
</command> | ||
</suite> | ||
</dictionary> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// | ||
// NSScriptCommand+utils.swift | ||
// MiniSim | ||
// | ||
// Created by Oskar Kwasniewski on 18/06/2023. | ||
// | ||
|
||
import Foundation | ||
import Cocoa | ||
|
||
|
||
extension NSScriptCommand { | ||
func property(forKey key: String) -> Any? { | ||
if let evaluatedArguments = self.evaluatedArguments { | ||
if evaluatedArguments.keys.contains(key) { | ||
return evaluatedArguments[key] | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func encode<T>(_ value: T) throws -> String? where T : Encodable { | ||
let encoded = try JSONEncoder().encode(value) | ||
return String(data: encoded, encoding: .utf8) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.