Post

Replies

Boosts

Views

Activity

Reply to SwiftData Model object added to multiple parent objects moves ownership to only last parent
I figured it out. I'd either misunderstood what I'd read somewhere, or it was just wrong, when it said that SwiftData would figure out all the relationships when the models were so simple. So I didn't think I needed the Genre to also contain an array of all the Discs that use it. Turns out it still needs to be specified, just like with CoreData.
1w
Reply to Binding Picker to enum value in SwiftData struct
Nevermind. The confusion of how SwiftData migrates the store when new fields are added made the data bad. After throwing out the store, I can now use the picker in a simple way: Picker("Media", selection:$item.media) { ForEach(Media.allCases, id:\.self) { media in Text(media.rawValue); } } .labelsHidden() Not sure if it needs that id in the ForEach.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
2w
Reply to How to send a message from menu item in SwiftUI App to ContentView
I found that I can send a custom notification from the function that receives the menu item action to my ContentView, and that works, getting around the whole "this is not the view that you're searching for" junk. Pretty sure this will be the last SwiftUI app I'll ever write. I tried one before that should've been simple, but again I hit a wall of complexity and things that just aren't possible and went back to the glory that is Cocoa for that. At least things show up in the debugger there.
Topic: UI Frameworks SubTopic: SwiftUI
Jul ’26
Reply to How to send a message from menu item in SwiftUI App to ContentView
Well, I need more than a refresher course, since I've just been trying to understand how each piece of SwiftUI works when I come to it. I never been so stymied and angry at a language in all my 48 years of programming. Here's the relevant code. Comments say what works and what doesn't: @main struct SpeakotronikApp: App { @State public var contentView = ContentView(); @State var showFileImporter:Bool = false; var body:some Scene { WindowGroup { self.contentView .environment(langModel) } .commands { CommandGroup(after:.newItem) { Button("Import…") { self.showFileImporter = true; } .fileImporter(isPresented:$showFileImporter, allowedContentTypes:[.text], allowsMultipleSelection:false) { result in switch result { case .success(let urls): for url in urls { // Tell the content view to read the file and add new terms that will be translated: self.contentView.importTerms(url:url); } case .failure(let error): print("Error selecting file: \(error.localizedDescription)"); } } } } } } struct ContentView: View { @Environment(LangViewModel.self) private var langModel; @State private var selectedLang:Locale.Language = ...a language...; // This variable causes .translationTask to run when called from code contained in ContentView (.onChange), which works: @State var sequenceTranslationConfig:TranslationSession.Configuration?; var body: some View { VStack { ...buttons and stuff... } .onChange(of:selectedLang) { // Trigger the sequence translations to run: sequenceTranslationConfig = .init(source:Locale.current.language, target:selectedLang, preferredStrategy:.highFidelity); } .translationTask(sequenceTranslationConfig) { session in await langModel.translateSequence(using:session, terms:self.terms); } } public func importTerms(url:URL) { // Gain temporary permission to read security-scoped sandbox files guard url.startAccessingSecurityScopedResource() else { return } // Defer release of the system permission defer { url.stopAccessingSecurityScopedResource() } do { ...read file and do stuff with it... // Trigger my sequence translations to happen, which does NOT work: sequenceTranslationConfig = .init(source:Locale.current.language, target:selectedLang, preferredStrategy:.highFidelity); } catch { print("Error reading file: \(error.localizedDescription)") } } }
Topic: UI Frameworks SubTopic: SwiftUI
Jul ’26
Reply to How to send a message from menu item in SwiftUI App to ContentView
OK, so now the fileImporter is being run from the command in the app. It calls a function on the ContentView to import files. That function needs to do some other things to itself, so it sets the value of a @State TranslationSession.Configuration in the ContentView to a new instance, which is supposed to kick off a translationTask. Nothing happens, I assume because it's not being called from within a View hierarchy, but from the app. Why is it so difficult to make things work in SwiftUI?
Topic: UI Frameworks SubTopic: SwiftUI
Jul ’26
Reply to How to send a message from menu item in SwiftUI App to ContentView
I tried moving showFileImporter to the app and sharing it in the environment, but Swift hates that. struct SpeakotronikApp: App { @State var showFileImporter:Bool = false; var body:some Scene { WindowGroup { ContentView() .environment(showFileImporter) } } Instance method 'environment' requires that 'Bool' be a class type Instance method 'environment' requires that 'Bool' conform to 'Observable'
Topic: UI Frameworks SubTopic: SwiftUI
Jul ’26
Reply to How to send a message from menu item in SwiftUI App to ContentView
You could use environment variables for showFileImporter and remove the contentView state var to solve the issue. I'd thought about that, but it seems like bad practice to keep a variable in the app when only the ContentView actually uses it. But then there are many things about Swift that are illogical. Note: in Swift, you don't need to end statements with semicolon. I know, yet I do, because I always have in all other languages I've ever used, and always will. They add important visual endings to each line of code. I missed one in my editing of the example in my original post.
Topic: UI Frameworks SubTopic: SwiftUI
Jul ’26
Reply to Why do random errOSAInternalTableOverflow errors return when running AppleScripts via ScriptingBridge?
Today I noticed that when SB fails, the SBApplication object says isRunning is NO, but the InDesign application it's pointing to is still very much alive and running. If I create a new SBApplication when this happens and run the same script that just failed, it works fine. Something is screwing up the SBApplication in some way. I'll add this to the bug.
Apr ’26
Reply to Is calling different SBApplication objects from different threads bad?
What's the "architecture" of that helper app? Is it a true "app" (meaning, in an app bundle, running an NSApplication loop, and simply marked "faceless")? Or is it something like an XPCService or a command-line tool? Also, how was it launched? The applescriptrunner helper app is just a command line app launched with NSTask, using a NSXPCConnection, et al. That app's main() creates an NSOperation subclass that handles all of the XPC communication and running of scripts (formerly via NSAppleScript, but now via ScriptingBridge). I assume this is communicating with the "server app", so there isn't a user-visible app either? If so, then my best guess would be that we're suspending the app or otherwise stopping/pausing its normal execution. It happens with both InDesign Desktop (UI) and InDesignServer (no UI). How long does it stay stuck like this? It doesn't actually "stick", it just returns nothing from InDesign when the script should return a result or throws an error. From the reply I got in my ticket for that situation (FB22065804), it's InDesign's bug that's causing this.
Mar ’26
Reply to Is calling different SBApplication objects from different threads bad?
I've made a change to our app. We have a faceless helper app that we used to use for running all our scripts. It uses XPC for communication between it and the main app. It was developed many years ago when we needed to stop blocking the main thread when multiple jobs were running scripts. I updated it a few days ago to use ScriptingBridge (like we'd previously done to the main app). This has been used in production now for a couple days. We no longer experience crashes caused by the low level AE system (AECreateEmptyEvent, etc). That's the good news—the AE errors for one job no longer take out the app and any other jobs. I do still get problems that appear to be empty replies from telling the InDesignApplication (an SBApplication subclass) to doScript:ourScript. This mostly happens when running multiple jobs at once and when I switch our main app in and out of the foreground. I assume doing that jiggles a lot of Jell-o. I don't know what would cause that; the AE system, SB, or InDesign.
Mar ’26
Reply to SwiftData Model object added to multiple parent objects moves ownership to only last parent
I figured it out. I'd either misunderstood what I'd read somewhere, or it was just wrong, when it said that SwiftData would figure out all the relationships when the models were so simple. So I didn't think I needed the Genre to also contain an array of all the Discs that use it. Turns out it still needs to be specified, just like with CoreData.
Replies
Boosts
Views
Activity
1w
Reply to Binding Picker to enum value in SwiftData struct
Nevermind. The confusion of how SwiftData migrates the store when new fields are added made the data bad. After throwing out the store, I can now use the picker in a simple way: Picker("Media", selection:$item.media) { ForEach(Media.allCases, id:\.self) { media in Text(media.rawValue); } } .labelsHidden() Not sure if it needs that id in the ForEach.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
2w
Reply to SwiftUI design options needed
I was using a NavigationSplitView, but the huge mess of different ways it can be used was throwing a wrench into the works. The linked article really helped and showed different things that actually worked. Thanks!
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
2w
Reply to How to send a message from menu item in SwiftUI App to ContentView
I found that I can send a custom notification from the function that receives the menu item action to my ContentView, and that works, getting around the whole "this is not the view that you're searching for" junk. Pretty sure this will be the last SwiftUI app I'll ever write. I tried one before that should've been simple, but again I hit a wall of complexity and things that just aren't possible and went back to the glory that is Cocoa for that. At least things show up in the debugger there.
Topic: UI Frameworks SubTopic: SwiftUI
Replies
Boosts
Views
Activity
Jul ’26
Reply to How to send a message from menu item in SwiftUI App to ContentView
Well, I need more than a refresher course, since I've just been trying to understand how each piece of SwiftUI works when I come to it. I never been so stymied and angry at a language in all my 48 years of programming. Here's the relevant code. Comments say what works and what doesn't: @main struct SpeakotronikApp: App { @State public var contentView = ContentView(); @State var showFileImporter:Bool = false; var body:some Scene { WindowGroup { self.contentView .environment(langModel) } .commands { CommandGroup(after:.newItem) { Button("Import…") { self.showFileImporter = true; } .fileImporter(isPresented:$showFileImporter, allowedContentTypes:[.text], allowsMultipleSelection:false) { result in switch result { case .success(let urls): for url in urls { // Tell the content view to read the file and add new terms that will be translated: self.contentView.importTerms(url:url); } case .failure(let error): print("Error selecting file: \(error.localizedDescription)"); } } } } } } struct ContentView: View { @Environment(LangViewModel.self) private var langModel; @State private var selectedLang:Locale.Language = ...a language...; // This variable causes .translationTask to run when called from code contained in ContentView (.onChange), which works: @State var sequenceTranslationConfig:TranslationSession.Configuration?; var body: some View { VStack { ...buttons and stuff... } .onChange(of:selectedLang) { // Trigger the sequence translations to run: sequenceTranslationConfig = .init(source:Locale.current.language, target:selectedLang, preferredStrategy:.highFidelity); } .translationTask(sequenceTranslationConfig) { session in await langModel.translateSequence(using:session, terms:self.terms); } } public func importTerms(url:URL) { // Gain temporary permission to read security-scoped sandbox files guard url.startAccessingSecurityScopedResource() else { return } // Defer release of the system permission defer { url.stopAccessingSecurityScopedResource() } do { ...read file and do stuff with it... // Trigger my sequence translations to happen, which does NOT work: sequenceTranslationConfig = .init(source:Locale.current.language, target:selectedLang, preferredStrategy:.highFidelity); } catch { print("Error reading file: \(error.localizedDescription)") } } }
Topic: UI Frameworks SubTopic: SwiftUI
Replies
Boosts
Views
Activity
Jul ’26
Reply to How to send a message from menu item in SwiftUI App to ContentView
OK, so now the fileImporter is being run from the command in the app. It calls a function on the ContentView to import files. That function needs to do some other things to itself, so it sets the value of a @State TranslationSession.Configuration in the ContentView to a new instance, which is supposed to kick off a translationTask. Nothing happens, I assume because it's not being called from within a View hierarchy, but from the app. Why is it so difficult to make things work in SwiftUI?
Topic: UI Frameworks SubTopic: SwiftUI
Replies
Boosts
Views
Activity
Jul ’26
Reply to How to send a message from menu item in SwiftUI App to ContentView
I tried moving showFileImporter to the app and sharing it in the environment, but Swift hates that. struct SpeakotronikApp: App { @State var showFileImporter:Bool = false; var body:some Scene { WindowGroup { ContentView() .environment(showFileImporter) } } Instance method 'environment' requires that 'Bool' be a class type Instance method 'environment' requires that 'Bool' conform to 'Observable'
Topic: UI Frameworks SubTopic: SwiftUI
Replies
Boosts
Views
Activity
Jul ’26
Reply to How to send a message from menu item in SwiftUI App to ContentView
You could use environment variables for showFileImporter and remove the contentView state var to solve the issue. I'd thought about that, but it seems like bad practice to keep a variable in the app when only the ContentView actually uses it. But then there are many things about Swift that are illogical. Note: in Swift, you don't need to end statements with semicolon. I know, yet I do, because I always have in all other languages I've ever used, and always will. They add important visual endings to each line of code. I missed one in my editing of the example in my original post.
Topic: UI Frameworks SubTopic: SwiftUI
Replies
Boosts
Views
Activity
Jul ’26
Reply to Why do random errOSAInternalTableOverflow errors return when running AppleScripts via ScriptingBridge?
Today I noticed that when SB fails, the SBApplication object says isRunning is NO, but the InDesign application it's pointing to is still very much alive and running. If I create a new SBApplication when this happens and run the same script that just failed, it works fine. Something is screwing up the SBApplication in some way. I'll add this to the bug.
Replies
Boosts
Views
Activity
Apr ’26
Reply to Is calling different SBApplication objects from different threads bad?
What's the "architecture" of that helper app? Is it a true "app" (meaning, in an app bundle, running an NSApplication loop, and simply marked "faceless")? Or is it something like an XPCService or a command-line tool? Also, how was it launched? The applescriptrunner helper app is just a command line app launched with NSTask, using a NSXPCConnection, et al. That app's main() creates an NSOperation subclass that handles all of the XPC communication and running of scripts (formerly via NSAppleScript, but now via ScriptingBridge). I assume this is communicating with the "server app", so there isn't a user-visible app either? If so, then my best guess would be that we're suspending the app or otherwise stopping/pausing its normal execution. It happens with both InDesign Desktop (UI) and InDesignServer (no UI). How long does it stay stuck like this? It doesn't actually "stick", it just returns nothing from InDesign when the script should return a result or throws an error. From the reply I got in my ticket for that situation (FB22065804), it's InDesign's bug that's causing this.
Replies
Boosts
Views
Activity
Mar ’26
Reply to Is calling different SBApplication objects from different threads bad?
I've made a change to our app. We have a faceless helper app that we used to use for running all our scripts. It uses XPC for communication between it and the main app. It was developed many years ago when we needed to stop blocking the main thread when multiple jobs were running scripts. I updated it a few days ago to use ScriptingBridge (like we'd previously done to the main app). This has been used in production now for a couple days. We no longer experience crashes caused by the low level AE system (AECreateEmptyEvent, etc). That's the good news—the AE errors for one job no longer take out the app and any other jobs. I do still get problems that appear to be empty replies from telling the InDesignApplication (an SBApplication subclass) to doScript:ourScript. This mostly happens when running multiple jobs at once and when I switch our main app in and out of the foreground. I assume doing that jiggles a lot of Jell-o. I don't know what would cause that; the AE system, SB, or InDesign.
Replies
Boosts
Views
Activity
Mar ’26