How to resize app windows using Swift background task on macOS?

I’m getting into Swift and need some help overcoming issues with an app which uses NotificationCenter observers and the Accessibility API. I’m running macOS 14.4.1 Sonoma. This is my first proper start-from-scratch Swift project, so any help would be very welcome 🙂

What I’m trying to do

I’m making a macOS app called ‘TidyDaemon’ which manipulates the size and position of windows for specified applications (such as Spotify and Mail). This will ensure that all of the windows for the specified applications are the same size and aligned to the screen, which is super helpful for OCD.

It’s important to note that the TidyDaemon is resizing the windows for other applications, not its own window (it doesn’t have a UI). This is why I am using the Accessibility API.

I’m trying to use the NotificationCenter to add observers for application launch notifications, so that the app window is resized and aligned when the app launches, and then you can move the windows around safe in the knowledge that the window positions will be reset next time the app launches. This means that the ‘TidyDaemon’ is constantly running in the background, so I’m trying to keep it as lightweight as possible.

A public GitHub repo for the project can be found here: https://github.com/AmeliaFD/TidyDaemon

What are the issues?

I’ve added several print statements to help with debugging. When I run the app using Xcode, I get the following console output. As you can see, I had Mail, Safari and Xcode open at the time, which is why they have process IDs. Pycharm, Maps, and Spotify weren’t open, which is why they don’t have process IDs.

Adding observer for com.jetbrains.pycharm
Adding observer for com.apple.Maps
Adding observer for com.apple.mail
pid: 28146
got element
created observer for 28146
Adding observer for com.apple.dt.Xcode
pid: 20555
got element
created observer for 20555
Adding observer for com.spotify.client
Adding observer for com.apple.Safari
pid: 27707
got element
created observer for 27707

However, when opening a new app (e.g. closing and reopening Safari) several things don’t happen:

  1. The newly opened window is not the correct size or position. This suggests that there is an issue with the setLayout function.

  2. Nothing is printed to the console. This suggests that the addObserver function was not called, implying that the observer for didLaunchApplicationNotification is not working as planned.

To address the obvious potential issues, yes I have granted TidyDaemon permission in System Settings > Privacy and Security > Accessibility. I have also set Application is Agent (UIElement) to Yes in Xcode.

How you can help

Please feel free to fork the repo to experiment with the code yourself. I would be very grateful if you can suggest fixes for the issues I’m encountering. Thanks in advance!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật