// A script file brought into Cotton Story: picked with Bring in a script, or // opened in Story from Files, Mail or AirDrop ("Open in"; the flavour // declares Fountain, Final Draft, PDF and plain text). The phone only copies // the file where the core can read it and hands over the path // (`story.bring_in`); the core reads it by what it is, makes the story, its // scenes, characters and places, and makes it current. The person is told // how many scenes came in and, for a printed script, to read it through. // The copy and the system's Inbox copy are removed after. #if os(iOS) import CottonKit import CottonValues import Foundation @MainActor enum StoryFountainOpen { /// The file kinds Story reads as a script. static let extensions = ["fountain", "fdx", "pdf", "txt"] /// A file this app opens as a screenplay: Story's own build, a Fountain, /// Final Draft, PDF or plain-text file. static func opens(_ url: URL) -> Bool { url.isFileURL && FlavourManifest.current?.flavour == "story" && extensions.contains(url.pathExtension.lowercased()) } /// Said once a file has come in, so Story's screens read again. static let broughtIn = Notification.Name("story.fountain.brought-in") /// Copies the file where the core can read it and brings it in as a new /// story; says what happened (`done` false for a refusal). The answer /// carries the printed script's check, which Story's home keeps showing. @discardableResult static func bring(_ url: URL, core: any CottonCalling, say: @escaping (String, Bool) -> Void) async -> StoryBroughtIn? { let scoped = url.startAccessingSecurityScopedResource() let copy = FileManager.default.temporaryDirectory .appendingPathComponent("story-bring-in-\(UUID().uuidString)") .appendingPathExtension(url.pathExtension) defer { try? FileManager.default.removeItem(at: copy) if url.path.contains("/Documents/Inbox/") { try? FileManager.default.removeItem(at: url) } } let copied = (try? FileManager.default.copyItem(at: url, to: copy)) != nil if scoped { url.stopAccessingSecurityScopedResource() } guard copied else { say(ShellLines.line("story", "fountain.unreadable"), false) return nil } do { let came = try await StoryShelf(core: core).bringIn(path: copy.path(percentEncoded: false), name: url.lastPathComponent) say(InkWords.counted("fountain.brought_in", came.scenes), true) return came } catch { say(ActionRefused.words(for: error), false) return nil } } } #endif