Skip to content

[Cocoa] Replace deprecated LSGetApplicationForInfo in Program.findProgram#3187

Merged
HannesWell merged 1 commit intoeclipse-platform:masterfrom
HeikoKlare:fix/replace-deprecated-LSGetApplicationForInfo
Apr 13, 2026
Merged

[Cocoa] Replace deprecated LSGetApplicationForInfo in Program.findProgram#3187
HannesWell merged 1 commit intoeclipse-platform:masterfrom
HeikoKlare:fix/replace-deprecated-LSGetApplicationForInfo

Conversation

@HeikoKlare
Copy link
Copy Markdown
Contributor

@HeikoKlare HeikoKlare commented Apr 2, 2026

Problem

Program.findProgram(String) on macOS used two deprecated native APIs:

  • LSGetApplicationForInfo — deprecated since macOS 10.10, used to find the default application for a file extension by querying Launch Services
  • CFURLCreateFromFSRef — deprecated since macOS 10.9, used to convert the returned FSRef to a CFURL

Both of these also required the deprecated constants kLSUnknownType, kLSUnknownCreator, and kLSRolesAll.

Solution

The two deprecated calls are replaced by modern NSWorkspace APIs:

On macOS 12.0+ (primary path):

  1. Look up a UTType for the file extension using [UTType typeWithFilenameExtension:] (UniformTypeIdentifiers framework)
  2. Find the default application using [NSWorkspace URLForApplicationToOpenContentType:]

This approach directly queries the Launch Services database by content type and reliably handles all file types, including third-party ones such as .xlsx (Microsoft Excel), which the intermediate URLForApplicationToOpenURL: approach failed to resolve for non-existent dummy files on modern macOS.

Fallback for macOS 10.6–11.x:

  • [NSWorkspace URLForApplicationToOpenURL:] with a dummy file URL carrying the target extension

Changes

  • NSWorkspace.java: Added two new methods following Java camelCase conventions:

    • urlForApplicationToOpenURL(NSURL) — wraps the deprecated-but-still-functional macOS 10.6+ API for older systems
    • urlForApplicationToOpenContentType(long) — wraps the modern macOS 12.0+ API
  • Selector.java / OS.java: Added the required Objective-C selectors URLForApplicationToOpenURL:, URLForApplicationToOpenContentType:, and typeWithFilenameExtension:

  • OS.java: Removed native method declarations for LSGetApplicationForInfo and CFURLCreateFromFSRef, and their associated constants kLSUnknownType, kLSUnknownCreator, kLSRolesAll

  • Program.java: Replaced the deprecated two-step lookup with a call to the new findAppURLForExtension() helper that applies the version-guarded strategy above

  • os.c / os_stats.h: Regenerated to reflect the removed native methods

  • Test_org_eclipse_swt_program_Program.java: Added test_findProgramForTxt() asserting that findProgram(".txt") returns a non-null program with a non-empty name on Windows and macOS. The test is skipped on Linux because applications for MIME types may not be registered in Linux test environments.

Related

Contributes to #3186


🤖 Created with Claude Code

@HeikoKlare HeikoKlare marked this pull request as draft April 2, 2026 20:35
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 2, 2026

Test Results

  170 files  + 1    170 suites  +1   27m 57s ⏱️ + 1m 0s
4 693 tests +58  4 672 ✅ +56   21 💤 +3  0 ❌  - 1 
6 676 runs  +63  6 518 ✅ +58  158 💤 +6  0 ❌  - 1 

Results for commit 608121d. ± Comparison against base commit 58b6d88.

♻️ This comment has been updated with latest results.

@HeikoKlare HeikoKlare force-pushed the fix/replace-deprecated-LSGetApplicationForInfo branch from 3316bdb to 2608ab5 Compare April 2, 2026 21:01
Copy link
Copy Markdown
Member

@HannesWell HannesWell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's nice, thanks for that.
And it even needs less native code 🎉

One nitpick: If the years in the copyright headers are adjusted in some files, it would be nice to increment them in other files too.

@HeikoKlare HeikoKlare marked this pull request as ready for review April 3, 2026 10:01
@HeikoKlare HeikoKlare force-pushed the fix/replace-deprecated-LSGetApplicationForInfo branch 2 times, most recently from 48269e9 to 378b95a Compare April 3, 2026 11:03
@HeikoKlare
Copy link
Copy Markdown
Contributor Author

One nitpick: If the years in the copyright headers are adjusted in some files, it would be nice to increment them in other files too.

The copyright years were actually not updated explicitly but auto-updated by SWT tooling.
But we can of course also update the years in the other files. I have updated the PR accordingly.

@HeikoKlare HeikoKlare force-pushed the fix/replace-deprecated-LSGetApplicationForInfo branch 2 times, most recently from 916ecea to 48230b9 Compare April 9, 2026 15:52
Program.findProgram

LSGetApplicationForInfo (deprecated macOS 10.10) and
CFURLCreateFromFSRef
(deprecated macOS 10.9) are replaced by NSWorkspace APIs:
- On macOS 12.0+: UTType.typeWithFilenameExtension: combined with
  NSWorkspace.urlForApplicationToOpenContentType: for reliable content
  type-based lookup, including third-party file types like .xlsx
- Fallback for older macOS: NSWorkspace.urlForApplicationToOpenURL:
  with a dummy file URL carrying the extension

Adds a test asserting that findProgram(".txt") returns a non-null
program
with a name on Windows and macOS. The test is skipped on Linux because
applications for MIME types may not be registered in Linux test
environments.
@HannesWell HannesWell force-pushed the fix/replace-deprecated-LSGetApplicationForInfo branch from 48230b9 to 608121d Compare April 13, 2026 16:47
@HannesWell HannesWell merged commit 50cbddb into eclipse-platform:master Apr 13, 2026
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants