KMTools_OC.m 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //
  2. // KMTools_OC.m
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2023/3/27.
  6. //
  7. #import "KMTools_OC.h"
  8. #import <AppKit/AppKit.h>
  9. @implementation KMTools_OC
  10. + (BOOL)isDefaultRoleHandlerForContentType:(NSString*)fileExtension {
  11. NSString *app = (NSString*)CFBridgingRelease(LSCopyDefaultRoleHandlerForContentType((CFStringRef)CFBridgingRetain([self UTIforFileExtension:fileExtension]),
  12. kLSRolesAll));
  13. if ([app isEqualToString:[[NSBundle mainBundle] bundleIdentifier]]) {
  14. return YES;
  15. }
  16. return NO;
  17. }
  18. + (BOOL)setMyselfAsDefaultApplicationForFileExtension:(NSString *) fileExtension {
  19. OSStatus returnStatus = LSSetDefaultRoleHandlerForContentType ((CFStringRef)CFBridgingRetain([self UTIforFileExtension:fileExtension]),
  20. kLSRolesAll,
  21. (CFStringRef) CFBridgingRetain([[NSBundle mainBundle] bundleIdentifier]));
  22. if (returnStatus != 0) {
  23. return NO;
  24. }
  25. return YES;
  26. }
  27. + (BOOL) setPreviewAsDefaultApplicationForFileExtension:(NSString *) fileExtension {
  28. OSStatus returnStatus = LSSetDefaultRoleHandlerForContentType ((CFStringRef)CFBridgingRetain([self UTIforFileExtension:fileExtension]),
  29. kLSRolesAll,
  30. (CFStringRef)@"com.apple.Preview");
  31. if (returnStatus != 0) {
  32. return NO;
  33. }
  34. return YES;
  35. }
  36. #pragma mark --SetDefaultRoleHandlerForContentType
  37. + (NSString *)UTIforFileExtension:(NSString *) extension {
  38. NSString * UTIString = (NSString *)CFBridgingRelease(UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension,
  39. (CFStringRef)CFBridgingRetain(extension),
  40. NULL));
  41. return UTIString;
  42. }
  43. + (KMAppTarget)getAppTarget {
  44. // VERSION_DMG=1
  45. // VERSION_FREE=1
  46. // VERSION_PRO=1
  47. #if VERSION_FREE
  48. return KMAppTargetFree;
  49. #endif
  50. #if VERSION_PRO
  51. return KMAppTargetPro;
  52. #endif
  53. #if VERSION_DMG
  54. return KMAppTargetDMG;
  55. #endif
  56. return KMAppTargetFree;
  57. }
  58. @end
  59. @implementation KMPageSizeTool
  60. + (NSString *)conversionWithUnit:(NSString *)unit value:(CGFloat)value
  61. {
  62. CGFloat f = value;
  63. if ([unit isEqualToString:@"cm"]) {
  64. f = value/10.0;
  65. } else if([unit isEqualToString:@"in"]){
  66. f = value/25.4;
  67. } else {
  68. f = value;
  69. }
  70. if (fmodf(f, 1)==0) {//如果有一位小数点
  71. return [NSString stringWithFormat:@"%.0f",f];
  72. } else if (fmodf(value*10, 1)==0) {//如果有两位小数点
  73. return [NSString stringWithFormat:@"%.1f",f];
  74. } else {
  75. return [NSString stringWithFormat:@"%.2f",f];
  76. }
  77. }
  78. @end