Post

Replies

Boosts

Views

Activity

Printing to a label printer (from macOS)
Bit of a stream of consciousness below. I discovered after writing all that that I needed to switch width & height when setting values, and print landscape. That gets things to print as expected. But I really need to be able to set the Brother's notion of tape width in the Basic Options portion of the print panel. I've got this Brother QL-800 USB label printer that takes spools of either die-cut labels, or continuous paper of a given width. It can cut between labels or the continuous paper at any point. My app is intended to print small labels for small parts drawers. The app has a DB of parts, and I want to print dozens of unique labels of a specific height and width. Ideally, you'd set things up once, and then print one or more specific part labels without further UI interaction. The NSPrintPanel is far too complex, and allows the user to screw things up, so I'd like to forego it altogether. I've written some simple code that does succeed in printing via the NSPrintPanel, but it’s positioning the text ultimately incorrectly. I can't figure out how to properly size a view to print a label. I can get an NSPrintInfo object that knows the label printer is selected (e.g. it shows the right name for NSPrintInfo.printer.name). But it states the paperSize is (612.0, 792.0), which if I assume 72 ppi, comes out to 8.5" x 11", which this paper clearly is not. func printLabel() { Task { let printInfo = NSPrintInfo() let printer = printInfo.printer print("Paper size: \(printInfo.paperSize)") // printInfo.paperSize = CGSize(width: 47 / 25.4 * 72.0, height: 12.0 / 25.4 * 72.0) printInfo.topMargin = 0.0 printInfo.bottomMargin = 0.0 printInfo.leftMargin = 0.0 printInfo.rightMargin = 0.0 printInfo.orientation = .landscape printInfo.isHorizontallyCentered = false printInfo.isVerticallyCentered = false printInfo.scalingFactor = 1.0 let view = NSHostingView(rootView: createLabelPrintView()) view.frame.size = CGSize(width: 25 / 25.4 * 72.0, height: 12.0 / 25.4 * 72.0) let op = NSPrintOperation(view: view, printInfo: printInfo) op.showsPrintPanel = true op.showsProgressPanel = true op.printPanel.options.insert(.showsPaperSize) op.printPanel.options.insert(.showsOrientation) op.run() } } @ViewBuilder func createLabelPrintView() -> some View { VStack(spacing: 0) { Text("Line 1") Text("Line 2") } .padding(0) } If I set the pageSize explicitly (uncommenting the line above), The printer offers a long list of available sizes (although none I've found corresponds to the 0.47"/12 mm x continuous spool). By the way, it never remembers the size I selected, and always reverts to 8.5 x 11, which it will then refuse to print, hanging in Print Center with an error about mismatched page sizes. I don’t know why the available page sizes have multiple sizes that don’t seem to match. E.g. 0.47" In the printer’s “Basic Options,” it shows the notion of a Width that's "not found," if I use a custom size: I don't really understand where those printer options can be found and set. Hmm, maybe I found a clue: I ran an NSPrintPanel by itself and examined the NSPrintInfo afterward: let panel = NSPrintPanel() panel.options.insert(.showsPaperSize) panel.options.insert(.showsOrientation) let result = await panel.beginSheet(using: printInfo, on: NSApplication.shared.windows.first!) print("Result: \(result)") print("Paper size: \(printInfo.paperSize)") print("Settings: \(printInfo.printSettings)") Paper size: (283.44000244140625, 82.08000183105469) Settings: { "AP_D_InputSlot" = ""; BrAutoTapeCut = ON; BrBiDiPrint = ON; BrBrightness = 0; BrContrast = 0; BrCutAtEnd = ON; BrCutLabel = 1; BrHalftonePattern = BrErrorDiffusion; BrMargin = "3.0"; BrMultiColor = BrMultiColorMonochrome; BrRedLevel = 0; BrRemoveBlkSpace = OFF; BrResolution = BrSpeed300x300dpi; BrTapeLength = "69.1"; ColorModel = Gray; Duplex = None; PaperInfoIsSuggested = 0; Resolution = 300x300dpi; "com_apple_print_DialogDismissedBy" = Print; "com_apple_print_DocumentTicket_PMSpoolFormat" = "application/pdf"; "com_apple_print_PDEsUsed" = "Printer Options"; "com_apple_print_PageToPaperMappingMediaName" = 29mm; "com_apple_print_PageToPaperMappingType" = 1; "com_apple_print_PrintSettings_PMCopies" = 1; "com_apple_print_PrintSettings_PMCopyCollate" = 1; "com_apple_print_PrintSettings_PMDestinationType" = 1; "com_apple_print_PrintSettings_PMDuplexing" = 1; "com_apple_print_PrintSettings_PMFirstPage" = 1; "com_apple_print_PrintSettings_PMLastPage" = 2147483647; "com_apple_print_PrintSettings_PMPageRange" = ( 1, 2147483647 ); "com_apple_print_ticket_type" = "com.apple.print.PrintSettingsTicket"; "com_apple_print_totalPages" = 2147483647; } It has added a bunch of Br-prefixed keys that clearly correspond to some of the options in the UI. But I'm not sure if I can set those programmatically and get things to print the way I expect. And I don't see anything for tape width. How do I get an NSPrintPanel to show the preview without an NSPrintOperation? I want to react to any changes the user might make before things go to print.
Topic: UI Frameworks SubTopic: AppKit
0
0
538
Sep ’24
/Users/USER/*/python3.8 is crashing
Every now and again, I see that Python 3.8 crashes. Now, I tried to get rid of all Python 3.8 interpreters on my system. But the crash log for this one gives a Path of /Users/USER/*/python3.8. What does that mean? That's not a real path on my filesystem. The crash is due to Python not finding a dylb it needs: Process: python3.8 [2946] Path: /Users/USER/*/python3.8 Identifier: python3.8 Version: ??? Code Type: X86-64 (Translated) Parent Process: launchd [1] User ID: 501 Date/Time: 2024-09-20 17:03:49.4230 -0700 OS Version: macOS 14.6.1 (23G93) Report Version: 12 Anonymous UUID: <redacted> Sleep/Wake UUID: <redacted> Time Awake Since Boot: 520000 seconds Time Since Wake: 43861 seconds System Integrity Protection: enabled Notes: PC register does not match crashing frame (0x0 vs 0x202E45A78) Crashed Thread: 0 Exception Type: EXC_CRASH (SIGABRT) Exception Codes: 0x0000000000000000, 0x0000000000000000 Termination Reason: Namespace DYLD, Code 1 Library missing Library not loaded: /usr/local/opt/gettext/lib/libintl.8.dylib Referenced from: <redacted> /Users/USER/*/python3.8 Reason: tried: '/usr/local/opt/gettext/lib/libintl.8.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/usr/local/opt/gettext/lib/libintl.8.dylib' (no such file), '/usr/local/opt/gettext/lib/libintl.8.dylib' (no such file), '/usr/local/lib/libintl.8.dylib' (no such file), '/usr/lib/libintl.8.dylib' (no such file, not in dyld cache) (terminated at launch; ignore backtrace) Now, this only happens (I think) when I'm running VS Code, which I normally hate doing, but I’m trying to develop an Autodesk Fusion plug-in in Python, and that’s how it’s done. I don’t know if this Python was installed by Apple, or if I got via one of the dozens of SDKs I've installed over the years. VS Code has a way to search for Python interpreters, and I used that to delete all the old ones I could find. And I'm pretty sure macOS is on a more recent version.
0
0
473
Sep ’24
Select All in a NavigationListView on macOS?
There are a couple of questions in here. I want to do a basic master-detail split view, as so many SwiftUI apps do. But I want to let the user select an arbitrary subset of items in the list, select them all, and delete them (or do other operations). Select All, by default, is excruciatingly slow. I have a List view with a selection binding to a Set<PersistentIdentifier>. A ForEach inside renders the items from a SwiftData @Query. I have a couple hundred items, each with a String and Date property. The list item renders the String and nothing else. I click on an item, hit Command-A, and the app locks up for several seconds while it messes with the selection. It never highlights all the items, but sometimes highlights one (usually different from the one I clicked on).I have an .onChange(of: self.selection) in there to debug the selection. It is called many, many times (2-3 times per second, very slowly), and print the selection count, which starts at 135, and goes down by one, to about 103. If I scroll the list, the selection onChange gets called a bunch more times. Sometimes you see the selection highlights change. My SwiftUI view hierarchy looks like this: NavigationSplitView OrdersList struct OrdersList : View { var body: some View { List(selection: self.$selection) { ForEach(self.orders) { order in NavigationLink { Text("Order ID: \(order.vendorOrderID ?? "<none>")") } label: { Text("Order ID: \(order.vendorOrderID ?? "<none>")") } } .onDelete(perform: self.deleteOrders) .onChange(of: self.selection) { print("Selection changed: \(self.selection.count)") } } } func deleteOrders(offsets: IndexSet) { withAnimation { for index in offsets { self.modelContext.delete(self.orders[index]) } } } @State var selection = Set<PersistentIdentifier>() @Query var orders : [Order] @Environment(\.modelContext) var modelContext } If I use the mouse to select a single item, it behaves as expected. The item is selected, the onChange gets called, and if I choose Edit->Delete, the onDelete handler is called. If I use the mouse to shift-select multiple items, the behavior is similar to Select All above, but the selection Set count starts at the number of items I selected, and then is slowly whittled down to 0 or 1, with some random item being selected. I’d like for the Edit->Delete command to work with a keystroke (delete). How can I set the shortcut on an existing menu item without having to reimplement its behavior?
Topic: UI Frameworks SubTopic: SwiftUI
0
0
351
Sep ’24
API to use for high-level audio playback to a specific audio device?
I'm working on a little light and sound controller in Swift, driving DMX lights and audio. For the audio portion, I need to play a bunch of looping sounds (long-duration MP3s), and occasionally play sound effects (short-duration sounds, varying formats). I want all of this mixed into selected channels on specific devices. That is, I might have one audio stream going to the left channel, and a completely different one going to the right channel. What's the right API to do this from Swift? Core Audio? AVPlayer stuff?
0
0
407
Oct ’24
Video playback performance suffers unless I wiggle the mouse
For years (since I got my M1 MacBook Pro) I've experienced this weird performance degradation. I've tried posting about it to numerous channels, always without a reply. I figured I'd try here. The symptom is: video playback (of visual frames, not audio; audio is unaffected) stutters if I just let the player play. I'll start a video playing, it will play normally, but if I leave the computer alone to play back, the frame rate will drop dramatically (one or two frames per second, even slower). If I wiggle the mouse (generating events), playback resumes normally. After a few seconds, it will stutter again. It's as if the computer were throttling back on system clocks or something because there’s no user input. As you can imagine, this is a frustrating user experience when trying to watch videos. It doesn't matter if it's a YouTube video playing in Safari, or a WWDC video playing in the WWDC.app or Developer.app, or any other video in QuickTime Player.app. And it doesn't always happen. I have my own video playback app that doesn't seem to suffer from this, even though it plays multiple videos simultaneously. I'm on power adapter, and settings are generally set to maximum performance when on power adapter. Any ideas?
0
0
383
Oct ’24
CloudKit sharing limits?
When you share records, they get put into a new zone. Creating a zone for the share makes sense to me, but I thought I read that there was a limit to the number of zones one could have (something like 1024). Does this mean a user can’t share more than 1024 separate items with 1024 different people? I assume any other items shared with the same group end up in an existing zone.
0
0
390
Oct ’24
Suppressing contextual menu on AVPlayerView in SwiftUI app
My app plays videos. At first I used SwiftUI’s VideoPlayer, but that didn't give me enough control over the underlying AVPlayerView, so I created my own NSViewRepresentable. That works well, and I can adjust the AVPlayerView as I see fit. But it seems to have a contextual menu that still appears instead of the one I try to apply using SwiftUI. If I put a non-opaque color over the AVPlayerView in a ZStack, I'm able to then add a .contextMenu that works, but only if the color is non-opaque (e.g. Color(red: 0, green: 0, blue: 0, opacity: 0.00001)). This feels like a pretty hacky solution, and doesn’t work if I set opacity: to 0. Is there a better way to keep AVPlayerView from handling events? It also gets scroll events (which scrubs through the movie) that I'd like to suppress.
Topic: UI Frameworks SubTopic: SwiftUI
0
0
271
Dec ’24
Serial port speed limited to 3 Mbps
Six months ago I wrote FB14122473, detailing how the built-in CDC (or FTDI) VCP serial port driver is limited to 3 Mbps or less. Thing is, there are some FTDI devices that can do 12 Mbps (maybe more), and I have devices I need to communicate with at 4 Mbps. I had to use the FTDI SDK to be able to communicate with these. I was hoping this post might help draw attention to that bug report.
1
0
480
Jan ’25
Scaling a collection of positioned views to parent (macOS)?
I have a view that lets the user position and size a bunch of subviews. I use .frame() and .position() to accomplish this. Right now, if the user resizes the window, the views stay where they are, anchored to the top-left corner. What I'd like is for the views to scale as a whole with window, maintaining their relative position, and their aspect ratios. I can apply .scaleEffect(_:anchor:) to the containing view, and it scales them the way I want, but I'm not sure how to tie it to the window. My first thought is to use a GeometryReader, but I don't really know what the "original" size would have been in order to compute a scale factor. How else might I accomplish this?
Topic: UI Frameworks SubTopic: SwiftUI
1
0
246
Jan ’25
"Duplicate output file"/"Unable to build node" for localized folders
We have some HTML content embedded in our app. These live in two top-level folders. These folders have been localized, and so live in Resources/en.lproj/HelpContent and Resources/fr.lproj/HelpContent (for example). Similarly, we have a bunch of localized .storyboard files.Xcode 8 builds this just fine. Xcode 9b6 complains:duplicate output file '/Users/me/Library/Developer/Xcode/DerivedData/MyApp- fdskkibuvbsubudkmqtgsjxmyqme/Build/Products/Debug-iphoneos/MyApp.app/ HelpContent' on task: CpResource /Users/me/Projects/Clients/MyCompany/repo/mp_vision/iOS/Controller/MyApp /Resources/fr.lproj/HelpContent /Users/me/Library/Developer/Xcode/DerivedData/MyApp- fdskkibuvbsubudkmqtgsjxmyqme/Build/Products/Debug-iphoneos/MyApp.app/ HelpContent (in target 'MyApp')unable to build node: '/Users/me/Library/Developer/Xcode/DerivedData/MyApp- fdskkibuvbsubudkmqtgsjxmyqme/Build/Products/Debug-iphoneos/MyApp.app/ HelpContent' (node is produced by multiple commands; e.g., 'b1132708c20f997e752faabcee01d49c82a500833121dcd848c3954357d9f7b1: CpResource /Users/me/Projects/Clients/MyCompany/repo/mp_vision/iOS/Controller/MyApp /Resources/en.lproj/HelpContent /Users/me/Library/Developer/Xcode/DerivedData/MyApp- fdskkibuvbsubudkmqtgsjxmyqme/Build/Products/Debug-iphoneos/MyApp.app/ HelpContent' and 'b1132708c20f997e752faabcee01d49c82a500833121dcd848c3954357d9f7b1: CpResource /Users/me/Projects/Clients/MyCompany/repo/mp_vision/iOS/Controller/MyApp /Resources/fr.lproj/HelpContent /Users/me/Library/Developer/Xcode/DerivedData/MyApp- fdskkibuvbsubudkmqtgsjxmyqme/Build/Products/Debug-iphoneos/MyApp.app/ HelpContent')The Files &amp; Groups list shows it correctly (e.g. "HelpContent" with "HelpContent (English)" and "HelpContent (French)" as sub-folders. The folders only show up once in the Copy Bundle Resources phase.But the build stops almost immediately on this error.
12
0
18k
Jan ’22
Getting the current queue and thread name?
The Xcode console logs an identifier indicating the current thread that's running when a message is print()ed from Swift. Some more complex logging systems (e.g. swift-log and console-kit) don't provide that information, and I'm trying to write a logging back-end for swift-log that will. Thing is, I don't see any way to get the current queue name in Swift. I tried dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL) But I get "cannot convert value of type '()' to expected argument type 'DispatchQueue?'". Passing nil instead results in "'dispatch_queue_get_label' has been replaced by property 'DispatchQueue.label'". This is not an unreasonable thing to want to do, and it poses no safety concerns.
4
0
5.6k
Nov ’22
Ignoring Command-Q in LSUIElement app (macOS, SwiftUI)?
I've got this LSUIElement app (no menu bar/dock icon). It puts up a window in certain circumstances to give the user control, but otherwise just runs in the background. Today I noticed that with its window frontmost, you can type Command-Q and it will quit. Is there a way to suppress that? I have explicit controls for quitting the app, but normally the user wants it to run in the background always, and I don't want it to be so easy to accidentally quit.
2
0
751
Dec ’23
How can I detect if my SwiftUI Window has been closed?
My macOS SwiftUI app has a single Window scene. .onAppear() is called when it is created, but if it is closed, .onDisappear() is not called. How can I detect when the window is closed? Note, I need to distinguish between closed and inactive or background. scenePhase is not updated for the window or any of its subviews when the window is deactivated. controlPhase is, however. Note that my app delegate implements: func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool { return false }
0
0
910
Dec ’23