gingerbeardman / stapler

My take on the classic Macintosh app Stapler (with a little bit of LaunchList)
https://blog.gingerbeardman.com/2024/08/10/stapler-i-remade-a-32-year-old-classic-macintosh-app/
MIT License
215 stars 7 forks source link

Better way to tell if Document was launched from Finder #2

Closed gingerbeardman closed 3 weeks ago

gingerbeardman commented 1 month ago

Currently the code to figure out whether the Stapler Document was opened from Finder is not really very good.

Basically, we're checking if the app is activated. That works, because the app does get activated when coming from Finder, but there are other ways the Document can be opened that result in the app being Activated.

let currentEvent = NSApplication.shared.currentEvent
let eventTypeValue = currentEvent?.subtype.rawValue
let isOpenedFromFinder = currentEvent != nil && currentEvent?.type == .appKitDefined && eventTypeValue == 1

// NSEvent.EventSubtype.applicationActivated == 1

So, we need to find a better way to do this. I ran out of time at this point.

gingerbeardman commented 1 month ago

Improved in https://github.com/gingerbeardman/stapler/commit/04398d830999ab03d37ac0430f6c9a5c20ee7030 for 1.2

This is still somewhat undesirable, as it has the side-effect of the items not launching if the app is already active when the document is opened.

gingerbeardman commented 3 weeks ago

Small change: https://github.com/gingerbeardman/stapler/commit/feaa002287e82e1d13f4cc4aac7ef4a35030c9c4

Will live with this for a while.

gingerbeardman commented 3 weeks ago

Reworked in and around commit https://github.com/gingerbeardman/stapler/commit/9fad23ce1ce9680f99ec9e1063e014b6a9a4d9db to check for the following scenarios:

// Define an enum for the different document opening scenarios
enum DocumentOpeningScenario {
    case launchedWithDocument
    case resumedBySystem
    case openedThroughFileMenu
    case openedFromFinderWhileRunning
    case unknown
}

// ...lots more code