Skip to content

Commit

Permalink
Add xcode/app/AppleScript
Browse files Browse the repository at this point in the history
  • Loading branch information
tomocy committed Feb 17, 2024
1 parent efc5df0 commit 27788de
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 48 deletions.
4 changes: 4 additions & 0 deletions ccodoc.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
F52641E22B7F651F00BB540F /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = F52641E12B7F651F00BB540F /* main.swift */; };
F557AFFE2B809950005CD00D /* App.swift in Sources */ = {isa = PBXBuildFile; fileRef = F557AFFD2B809950005CD00D /* App.swift */; };
F557B0002B809960005CD00D /* RunCcodoc.swift in Sources */ = {isa = PBXBuildFile; fileRef = F557AFFF2B809960005CD00D /* RunCcodoc.swift */; };
F557B0022B80BC4C005CD00D /* AppleScript.swift in Sources */ = {isa = PBXBuildFile; fileRef = F557B0012B80BC4C005CD00D /* AppleScript.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand All @@ -37,6 +38,7 @@
F52641E92B7F7B4D00BB540F /* ccodoc.command */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = ccodoc.command; sourceTree = BUILT_PRODUCTS_DIR; };
F557AFFD2B809950005CD00D /* App.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = App.swift; sourceTree = "<group>"; };
F557AFFF2B809960005CD00D /* RunCcodoc.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RunCcodoc.swift; sourceTree = "<group>"; };
F557B0012B80BC4C005CD00D /* AppleScript.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppleScript.swift; sourceTree = "<group>"; };
F5A6A6A42B7E684A00EB8014 /* env_linux.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = env_linux.h; sourceTree = "<group>"; };
F5A6A6A52B7E684A00EB8014 /* main.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = main.c; sourceTree = "<group>"; };
F5A6A6A62B7E684A00EB8014 /* memory.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = memory.c; sourceTree = "<group>"; };
Expand Down Expand Up @@ -100,6 +102,7 @@
isa = PBXGroup;
children = (
F557AFFF2B809960005CD00D /* RunCcodoc.swift */,
F557B0012B80BC4C005CD00D /* AppleScript.swift */,
F557AFFD2B809950005CD00D /* App.swift */,
F52641E12B7F651F00BB540F /* main.swift */,
F5B6A55D2B7FB9A30002287A /* app.entitlements */,
Expand Down Expand Up @@ -340,6 +343,7 @@
buildActionMask = 2147483647;
files = (
F557B0002B809960005CD00D /* RunCcodoc.swift in Sources */,
F557B0022B80BC4C005CD00D /* AppleScript.swift in Sources */,
F557AFFE2B809950005CD00D /* App.swift in Sources */,
F52641E22B7F651F00BB540F /* main.swift in Sources */,
);
Expand Down
49 changes: 49 additions & 0 deletions xcode/app/AppleScript.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// tomocy

import Foundation

struct AppleScript {
typealias Raw = NSAppleScript

let raw: Raw
}

extension AppleScript {
init(source: String) {
self.init(
raw: .init(source: source)!
)
}
}

extension AppleScript {
static func result(from: () throws -> Void) -> Result<Void, Error> {
return Result {
try from()
}.mapError { $0 as! Error }
}
}

extension AppleScript {
func compile() throws {
var error: NSDictionary?
raw.compileAndReturnError(&error)
if let error = error {
throw Error(reason: error as! [String: Any])
}
}

func run() throws {
var error: NSDictionary?
raw.executeAndReturnError(&error)
if let error = error {
throw Error(reason: error as! [String: Any])
}
}
}

extension AppleScript {
struct Error: Swift.Error {
let reason: [String: Any]
}
}
74 changes: 26 additions & 48 deletions xcode/app/RunCcodoc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,37 @@ class RunCcodoc: Thread {
NSLog("ccodoc: Started")
}

override func cancel() {
do {
do {
var error: NSDictionary?
Self.stopScript.executeAndReturnError(&error)
if error != nil {
return
}
}
override func main() {
let result = AppleScript.result(from: Self.startScript.run)
switch result {
case .success:
NSLog("ccodoc: Completed")
completion?()
case .failure(let error):
NSLog("ccodoc: Error: {Reason: \(error)}")
}

NSLog("ccodoc: Canceled")

super.cancel()
}

override func main() {
do {
var error: NSDictionary?
Self.startScript.executeAndReturnError(&error)
if error != nil {
return
}
override func cancel() {
let result = AppleScript.result(from: Self.stopScript.run)
if case .failure(let error) = result {
NSLog("ccodoc: Error: {Reason: \(error)}")
}

NSLog("ccodoc: Completed")
completion?()
NSLog("ccodoc: Canceled")
super.cancel()
}

private var completion: Handler?
}

extension RunCcodoc {
private static let name = "ccodoc"

private static var startScript: NSAppleScript {
private static var startScript: AppleScript {
let executablePath = Bundle.main.path(forAuxiliaryExecutable: "\(name).command")!

let script = NSAppleScript.init(
let script = AppleScript.init(
source: """
tell application "Terminal"
activate
Expand All @@ -72,22 +64,15 @@ extension RunCcodoc {
do script "exit" in currentTab
end tell
"""
)!

do {
var error: NSDictionary?
let compiled = script.compileAndReturnError(&error)
if let error = error {
NSLog("error: \(error)")
assert(compiled)
}
}
)

try! script.compile()

return script
}

private static var stopScript: NSAppleScript {
let script = NSAppleScript.init(
private static var stopScript: AppleScript {
let script = AppleScript.init(
source: """
tell application "Terminal"
set myWindows to my filterListByClass(windows as list, window)
Expand All @@ -113,16 +98,9 @@ extension RunCcodoc {
return filtered
end filterListByClass
"""
)!

do {
var error: NSDictionary?
let compiled = script.compileAndReturnError(&error)
if let error = error {
NSLog("error: \(error)")
assert(compiled)
}
}
)

try! script.compile()

return script
}
Expand Down

0 comments on commit 27788de

Please sign in to comment.