You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.6 KiB

  1. #import "MyAppDelegate.h"
  2. #import "PreferencesWindowController.h"
  3. #import <Carbon/Carbon.h>
  4. #import <ApplicationServices/ApplicationServices.h>
  5. @implementation MyAppDelegate
  6. - (id)init
  7. {
  8. self = [super init];
  9. initial_action_done = NO;
  10. should_terminate = NO;
  11. return self;
  12. }
  13. - (IBAction)showPreferences:(id)sender
  14. {
  15. [PreferencesWindowController getPreferencesWindow];
  16. }
  17. - (void)applicationDidFinishLaunching:(NSNotification *)notification
  18. {
  19. // Test that the file mappings are correct
  20. [self testFileTypeBinding];
  21. // If we were opened because of a file drag or doubleclick
  22. // we've set initial_action_done in shouldShowUI
  23. // Otherwise we open a preferences dialog.
  24. if (!initial_action_done) {
  25. initial_action_done = YES;
  26. [self showPreferences: self];
  27. }
  28. }
  29. - (BOOL)shouldShowUI
  30. {
  31. // if this call comes before applicationDidFinishLaunching: we
  32. // should terminate immedeately after starting the script.
  33. if (!initial_action_done)
  34. should_terminate = YES;
  35. initial_action_done = YES;
  36. if( GetCurrentKeyModifiers() & optionKey )
  37. return YES;
  38. return NO;
  39. }
  40. - (BOOL)shouldTerminate
  41. {
  42. return should_terminate;
  43. }
  44. - (BOOL)applicationShouldOpenUntitledFile:(NSApplication *)sender
  45. {
  46. return NO;
  47. }
  48. - (void)testFileTypeBinding
  49. {
  50. NSURL *ourUrl;
  51. OSStatus err;
  52. FSRef appRef;
  53. NSURL *appUrl;
  54. static NSString *extensions[] = { @"py", @"pyw", @"pyc", NULL};
  55. NSString **ext_p;
  56. int i;
  57. if ([[NSUserDefaults standardUserDefaults] boolForKey: @"SkipFileBindingTest"])
  58. return;
  59. ourUrl = [NSURL fileURLWithPath: [[NSBundle mainBundle] bundlePath]];
  60. for( ext_p = extensions; *ext_p; ext_p++ ) {
  61. err = LSGetApplicationForInfo(
  62. kLSUnknownType,
  63. kLSUnknownCreator,
  64. (CFStringRef)*ext_p,
  65. kLSRolesViewer,
  66. &appRef,
  67. (CFURLRef *)&appUrl);
  68. if (err || ![appUrl isEqual: ourUrl] ) {
  69. i = NSRunAlertPanel(@"File type binding",
  70. @"PythonLauncher is not the default application for all " \
  71. @"Python script types. You should fix this with the " \
  72. @"Finder's \"Get Info\" command.\n\n" \
  73. @"See \"Changing the application that opens a file\" in " \
  74. @"Mac Help for details.",
  75. @"OK",
  76. @"Don't show this warning again",
  77. NULL);
  78. if ( i == 0 ) { // Don't show again
  79. [[NSUserDefaults standardUserDefaults]
  80. setObject:@"YES" forKey:@"SkipFileBindingTest"];
  81. }
  82. return;
  83. }
  84. }
  85. }
  86. @end