/r/macprogramming

Photograph via snooOG

This sub is obsolete, please use /r/macosprogramming/ instead.

/r/macprogramming

3,076 Subscribers

4

Install HomeBrew from OS X application

My application requires nmap which can only be installed using brew. How do I make sure my OS X app installs these packages before running my app? Should I use sandboxing to test it?

10 Comments
2020/04/19
15:31 UTC

6

My mac application doesn't work on other mac's

I created a mac cocoa application written in swift when i run it in my mac it works but when i build it for release mode and then run the application on other mac's where the UI is supposed to be is just a blank window. I used xcode to build it and it's deployment target was set to 10.10 the mac i checked it on had 10.10.5 and it didn't work.

does anyone what's going wrong or if there's any way for me to check why it's behaving like that. I'd be grateful for any help thanks.

The os version on my mac is 10.13.

3 Comments
2020/04/16
08:27 UTC

3

How is macOS "Word of the Day" screensaver built?

Hi!

I'm trying to build a macOS screensaver that has animations similar to the "Word of the Day" built-in screensaver, namely that there's a background gradient and text floating across the screen. I'm fairly experienced with SpriteKit and UIKit (but a total newbie to AppKit), and I've built one screensaver with SpriteKit before successfully, but I figured I would try to build this one using only things available to the ScreenSaver framework and I'm having some trouble.

I found a guide over here that suggests just using the built in `draw()` and `animateOneFrame()` methods to draw the background and moving objects in every frame, but I can already tell that performance of drawing the NSGradient in every frame is very poor at high resolutions, and animations appears to be choppy and inconsistent due to the fluctuating frame rate.

I could just 'give up' and build the screensaver in SpriteKit to get the fluid animation I want, but I assume that the built-in "Word of the Day" screensaver is not using SpriteKit and I am wondering how they got such silky-smooth animations at any resolution if not.

Can anyone shed some insight on this?

7 Comments
2020/04/16
01:32 UTC

3

USB Driver tutorial

Hey everyone, I got an LED light strip that plugs in via usb. It's meant to be used for a TV but I am curious to see if I can write a driver to control it from within a Mac app. Are there any good resources for that kind of thing? i couldn't find much info. And it looks like apple wants me to get some entitlement to write drivers when really i just wanna mess with hardware on my own machine.

6 Comments
2020/04/13
21:33 UTC

1

How to detect if NSWindow is dragged?

Hey all, I am using SwiftUI. I have a view called SideBar, that's consisting of VStack and buttons inside it.

I tried to attach some gestures, such as DragGesture() and TapGesture() onended on the VStack, hoping it would detect when a user drags the view, but this doesn't work all the time and I also think this is super hackish. Would anyone be able to help? Thanks a lot.

Code:

VStack{blabla}.frame(width: 40, height: 320)

.contentShape(Rectangle())

.gesture(

TapGesture().onEnded{_ in

print("end click")

}

)

.gesture(

DragGesture().onEnded{_ in

print("end drag")

}

)

1 Comment
2020/04/13
11:02 UTC

18

HBO Westworld canonizes AppKit

2 Comments
2020/04/13
07:33 UTC

3

Updating Perian project to Catalina

Hello, unfortunately I'm not a programmer, I tried building the Perian project (https://github.com/MaddTheSane/perian) on macOS Catalina but I really can't manage to make it through, even after cloning all the right git repositories and updating the Xcode project configuration.

Since it's definitely a useful project I would like to ask if anybody with more knowledge about macOS programming than me could take over the effort of updating the project to the latest versions of ffmpeg and libmatroska and port the whole project to Catalina.

Thanks :)

6 Comments
2020/04/11
12:50 UTC

9

Learning partner wanted

Hi there! I’m Tommaso a designer and web developer with the dream of building amazing iOS and especially macOS products.

I’m looking for someone who is at the beginning of his/her macOS/iOS journey with my same dream and who is willing to join me for learning, experimenting and building cool stuffs together.

I’m an absolute beginner in this field but I’m full of determination trust me.

If you are interested write me a DM. Thanks!

7 Comments
2020/04/10
22:21 UTC

5

What am I doing wrong with NSTableView? (Swift, Story Board)

I boiled my problem down the following screenshot. I've for a ViewController.swift, it inherits the NSTableViewDataSource and NSTableViewDelegate. In the story board, I clicked on Identity and Type and set the Custom Class to the ViewController (inside ViewController.swift). viewDidLoad() runs, but numberOfRows(in:) never runs. I kind of expected it to produce a bunch of rows (123 of them, in my case) that look like the table cell prototype.

https://preview.redd.it/mdmqx89qqur41.png?width=3840&format=png&auto=webp&s=d7bb524070f6d86c1f65cca61a9e3272fa09d110

5 Comments
2020/04/09
20:33 UTC

3

How to have a window (view) on mac os to always be on top and also opens on every other desktop (SwiftUI MACOS)

I have a view called sidebar. The idea is to have this view always on top no matter what, whether the user is currently on fullscreen of a different app or different desktop.

I have successfully achieved the always on top on the same desktop when the app got opened, but not when it's on another app's fullscreen. This is my code:

var sidebarWindow:NSWindow

let window = NSRect(x: 0, y: 0, width: 0, height: 0)

sidebarWindow = NSWindow(

contentRect: window,

styleMask: [.titled,.miniaturizable],

backing: .buffered, defer: false)

...

sidebarWindow.level = .floating

sidebarWindow.makeKeyAndOrderFront(nil)

...

if let myScreen = NSScreen.main {

let windowFrame = myScreen.frame

let h = windowFrame.size.height

let w = windowFrame.size.width

sidebarWindow.setFrameOrigin(NSPoint(x: w-55, y: h/2-180))

}

sidebarWindow.contentView = NSHostingView(rootView: SideBar(thisWindow: sidebarWindow))

But, as I described above, this `Sidebar()` (sidebarWindow) is not visible when I'm entering other app in full screen, as well as when I'm changing my desktop to other desktop.

How can I make `SideBar()` to always be on top no matter what, whether the user is currently on fullscreen of a different app and or different desktop?

Sorry if it's unclear.

Thanks a lot, any help would be appreciated!

1 Comment
2020/04/07
05:19 UTC

5

What do you think of CodeRunner IDE ?

Any thought about this IDE ?

Looks really nice, actually using the free 14-days version and thinking about buying a licence... But I'm interested in users' opinions first !

11 Comments
2020/03/31
22:55 UTC

4

How to make a macOS Swift app run even when not active?

I am completely new to Swift and macOS app development and trying to make a small macOS Menu Bar app that needs to listen to .mouseMoved events all the time right after the user starts the app.

I have the code like below:

class EventMonitor {
        
    func mouseMoved(with event: NSEvent) {
        print("mouse moved. x: \(event.absoluteX) y: \(event.absoluteY)")
    }
    
    init() {
        NSEvent.addLocalMonitorForEvents(matching: .mouseMoved) {
            self.mouseMoved(with: $0)
            return $0
        }
    }   
}

This code works but only when I click the icon on the menu bar, i.e when this app is active. But how do I keep listening to .mouseMoved events even when the app is not active?

Any help/ideas would be really appreciated.

7 Comments
2020/03/25
21:02 UTC

6

Disabled SIP for debug, now I can't attach to anything

I used 'csrutil enable --without debug' and now lldb does not attach to anything. This is reverse of the desired effect. General searches and ADF searches are not giving me anything other than, Use csrutil to enable debugging of system applications. Now I cannot debug anything.

Background: Preview has a long-standing issue of refusing to open files with an incorrect no-permission message after a certain number, volume, time or whatever of files have been opened (you can see the error logged from PVImageContainer initWithURL:). It's fixed by restarting the application, but I'd like to find the source of the problem.

Edit to add: this is what I see

11 > sleep 100 &
[1] 20415

12 > lldb
(lldb) attach 20415
error: attach failed: unable to attach
(lldb) ^D

OK, further edit, I can attach to processes that I own the executable... but the question remains: does 'disabling' SIP still not really let us use the debugger generally (and dtrace etc)?

2 Comments
2020/03/24
16:33 UTC

3

How do I diagnose this memory leak further?

Hi there!

I'm currently investigating a memory leak (CGContext cannot allocate ...) in a small library. I've ran the profiler a bit yesterday and identified ~where it/the leak is happening.

The memory increases every time I enter a certain component and doesn't decrease the same amount when leaving certain component.

What I got from instrumentin the allocations is that VM: CG image alongside VM: CG raster data keeps getting more and more persistent allocations and growing and growing in memory.

Out of the reported 522mb allocations in the app, 499mb are from the two culprits mentioned above and I can't seem to find it's cause :-(. The App doesn't crash in the End, but images are not refreshing anymore and the component won't load anymore when it is at about 1.5gb allocations for those two.

Any tips how to proceed, would be greatly appreciated!

1 Comment
2020/03/21
11:37 UTC

4

How do you run python script with arguments programmatically using swift

I am working on a MacOS project to turn my iOT device on and off. There is only a working python script to do it. I want to run these scripts from my MacOS project which I am writing in Swift

3 Comments
2020/03/15
19:06 UTC

2

How do programmatically create and manage a window without storyboards?

This is a challenge I would like to do -- to see if I can make an app without storyboards, but I don't know where to start. Apple Documentation doesn't help me much.

3 Comments
2020/03/11
05:11 UTC

2

How do developers find a hidden code? ( or not well-documented code)

CompSci student here.

I wanted to make a program that changes the language input programmatically, but I had a lot of trouble finding a related class/library in the Apple Developer documentation.

Later, an experienced dev showed me an open-source project and it was using `TISInputSources` class in its code. When I looked into it, `TISInputSources` was mentioned but with no further explanation or link.

I was finally able to find a file ` HIToolbox/TextInputSources.h` which had sufficient explanations in it. However, the fact that there was no link in the doc leading up to the file throws me off so hard, and I can fathom how devs even find out about these codes.

So dear experienced devs,

how do you go about finding these codes? I'm deeply curious about it.

Thank you!

2 Comments
2020/03/10
05:29 UTC

17

Calling AppKit developers

Trying to reach out to the Mac community and I guess that means making my first Reddit post! As a small introduction for myself I’m Lucas Derraugh and I run AppleProgramming on YouTube and am looking to start up a series focusing on Mac development. It will likely be half interview of an individual and then the other half talking about a code challenge and how you tackled it looking at the code details behind the implementation. My original Twitter post for some context: https://twitter.com/lucasderraugh/status/1233143222110543872?s=21

If you want to promote your app to a pretty decent audience or just want to show off cool code, I think it would be a fun thing to try! You can reach out via email at first name @ last name .com

2 Comments
2020/02/29
21:30 UTC

2

I can't get the client script to connect to the localhost server the TCP connection never happens for some reason. Can anyone help me please?

Greetings ya'll

ok so I wrote a simple client side program that creates a socket using

CFSteamCreatePairWithSocketToHost function

and connects to the server that runs on the local host on port 8080. It creates the socket just fine but it never connects to the server. I wrote the server in C btw. It didn't work and gave me a

kCFErrorDomainCFNetwork error 72000

and the only information that relays is that apparently the TCP connection couldn't be made don't know why though. So i tried to write the client side script in C too and added it to my swift project bridging header and all but it still doesn't connect. It creates the socket just fine but it fails to connect to the server and I have no idea why. Is mac blocking the libraries from making a TCP connection or something? please help me my dudes what do I do? I don't even know what to search for. the only thing I found was this on r/shittyprogramming.

Here's the code I used to connect to the server using swift 4. The server is running on port 8080 on localhost.

class client:NSObject {
      var inputstream = InputStream!
      var outputstream = OutputStream!

      func setupNetworkCom() {
          var readstream = Unmanaged<CFReadStream>?
          var writestream = Unmanaged<CFWriteStream>?

          CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, "localhost" as CFString, 8080, &readstream, &writestream)

          inputstream = readstream!.takeRetainedValue()
          outputstream = writestream!.takeReatainedValue()

          inputstream.schedule(in: .current, forMode: .common)
          outputstream.schedule(in: .current, forMode: .common)

          inputstream.open()
          outputstream.open()
      }
}

I've also tried changing the local host to 127.0.0.1 which is the IP I specified for the server to run on but it still doesn't work.

2 Comments
2020/02/27
05:24 UTC

9

Enable the internal Xcode 11 debugging menu

0 Comments
2020/02/22
19:38 UTC

2

PDFdocument returns nil

When I run this code in playground in works but when I run it in a macOs NSviewcontroller file, the pdfdocument returns nil. Why is that ? Any ideas ?

var pdfT = PDFDocument(url: URL(fileURLWithPath: locations[file]))
print(pdfToMerge?.pageCount)

Okay so weird , I figured out the problem, the absolute path being passed had the following characters preceding the absolute path - > file:///

for some reason this is acceptable in playground when initiating a pdf object. But in mac os this doesn't fly , so I wrote a simple function to solve the problem.

func perpareAbsoultePath(path:String) -> String{
var index = 0
var pathPrepared = ""
var upToIndex = 7
for letter in path{
if index >= upToIndex{
    pathPrepared.append(letter)
  }
    index+=1
 }
    return pathPrepared
}

function just creates a string minus the preceding characters I mentioned, hahah that was frustrating.

4 Comments
2020/02/19
03:46 UTC

Back To Top