Skip to content

Commit 655aae6

Browse files
Merge pull request #10 from macadmins/no_lanch_services
flag for not rebuilding launch services
2 parents ae16b96 + ac166d4 commit 655aae6

File tree

5 files changed

+26
-10
lines changed

5 files changed

+26
-10
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,11 @@ To set other browsers as the default, use the following identifiers:
1414
- Safari: `com.apple.safari`
1515
- Firefox: `org.mozilla.firefox`
1616
- MS Edge: `com.microsoft.edgemac`
17+
18+
## Known issues
19+
20+
### System Settings may not work correctly
21+
22+
If System Settings doesn't show all sections correctly after running, this tool, restart the machine. This is likely a timing issue with Launch Services, but we haven't reproduced it consistently. If you see that a restart doesn't fix the issue, you way wish to use the `---no-rebuild-launchservices` flag. This will mean that you need to reboot after running the tool.
23+
24+
![System Settings screenshot](assets/system_settings.png)

assets/system_settings.png

236 KB
Loading

main.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,18 @@ var version string
1313

1414
func main() {
1515
var identifier string
16+
var noRebuildLaunchServices bool
1617

1718
var rootCmd = &cobra.Command{
1819
Use: "default-browser",
1920
Short: "A cli tool to set the default browser on macOS",
2021
RunE: func(cmd *cobra.Command, args []string) error {
21-
return setDefault(identifier)
22+
return setDefault(identifier, noRebuildLaunchServices)
2223
},
2324
}
2425

2526
rootCmd.Flags().StringVar(&identifier, "identifier", "com.google.chrome", "An identifier for the application")
27+
rootCmd.Flags().BoolVar(&noRebuildLaunchServices, "no-rebuild-launchservices", false, "Do not rebuild launch services. Only use if you are experiencing issues with System Settings not displaying correctly after a reboot.")
2628

2729
rootCmd.Version = version
2830
rootCmd.SetVersionTemplate("default-browser version {{.Version}}\n")
@@ -33,7 +35,7 @@ func main() {
3335
}
3436
}
3537

36-
func setDefault(identifier string) error {
38+
func setDefault(identifier string, noRebuildLaunchServices bool) error {
3739
if identifier == "" {
3840
return fmt.Errorf("identifier cannot be empty")
3941
}
@@ -48,7 +50,7 @@ func setDefault(identifier string) error {
4850
return err
4951
}
5052

51-
err = launchservices.ModifyLS(c, identifier)
53+
err = launchservices.ModifyLS(c, identifier, noRebuildLaunchServices)
5254
if err != nil {
5355
return err
5456
}

pkg/launchservices/launchservices.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
const lsregister = "/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister"
1111

12-
func ModifyLS(c client.Client, identifier string) error {
12+
func ModifyLS(c client.Client, identifier string, noRebuildLaunchServices bool) error {
1313
plist, err := GetPlist(c.PlistLocation)
1414
if err != nil {
1515
return err
@@ -39,7 +39,8 @@ func ModifyLS(c client.Client, identifier string) error {
3939
fmt.Printf("lsregister does not exist at %s. You should restart the device to rebuild launchservices", lsregister)
4040
return nil
4141
}
42-
err = rebuildLaunchServices(c)
42+
43+
err = rebuildLaunchServices(c, noRebuildLaunchServices)
4344
if err != nil {
4445
return err
4546
}
@@ -52,10 +53,12 @@ func ModifyLS(c client.Client, identifier string) error {
5253
return nil
5354
}
5455

55-
func rebuildLaunchServices(c client.Client) error {
56-
_, err := c.Runner.RunCmd(lsregister, "-kill", "-r", "-domain", "local", "-domain", "system", "-domain", "user")
57-
if err != nil {
58-
return err
56+
func rebuildLaunchServices(c client.Client, noRebuildLaunchServices bool) error {
57+
if !noRebuildLaunchServices {
58+
_, err := c.Runner.RunCmd(lsregister, "-kill", "-r", "-domain", "local", "-domain", "system", "-domain", "user")
59+
if err != nil {
60+
return err
61+
}
5962
}
6063
return nil
6164
}

pkg/launchservices/launchservices_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@ func TestRebuildLaunchServices(t *testing.T) {
1919
Runner: mockRunner,
2020
}
2121

22-
err := rebuildLaunchServices(mockClient)
22+
err := rebuildLaunchServices(mockClient, false)
2323
assert.NoError(t, err, "rebuildLaunchServices should not return an error")
24+
25+
err = rebuildLaunchServices(mockClient, true)
26+
assert.NoError(t, err, "rebuildLaunchServices should not return an error when noRebuildLaunchServices is true")
2427
}
2528

2629
func TestKilllsd(t *testing.T) {

0 commit comments

Comments
 (0)