Run AppleScript from Cocoa Application

Solved! Xcode wasn’t saving my script file into app’s resources path. To run an AppleScript code from Cocoa Application, use this: NSString *path = [[NSBundle mainBundle] pathForResource:@”ScriptName” ofType:@”scpt”]; NSURL *url = [NSURL fileURLWithPath:path];NSDictionary *errors = [NSDictionary dictionary]; NSAppleScript *appleScript = [[NSAppleScript alloc] initWithContentsOfURL:url error:&errors]; [appleScript executeAndReturnError:nil]; Swift 5.6.1: import AppleScriptObjC import Cocoa let path = … Read more

Listen to a value change of my text field

You can set a delegate for your NSTextField instance and have the delegate implement the following method: – (void)controlTextDidChange:(NSNotification *)notification { // there was a text change in some control } Your delegate object can be the application delegate, a window controller, a view controller, or some other object in your application. The delegate can … Read more

How to display the current project version of my App to the user?

After further searching and testing, I found the solution myself. NSDictionary* infoDictionary = [[NSBundle mainBundle] infoDictionary]; NSLog(@”%i Keys: %@”, [infoDictionary count], [[infoDictionary allKeys] componentsJoinedByString: @” ,”]); This snipplet gave me the following output: 20 Keys : NSBundleResolvedPath ,CFBundleVersion ,NSBundleInitialPath ,CFBundleIdentifier ,NSMainNibFile ,CFBundleIconFile ,CFBundleInfoPlistURL ,CFBundleExecutable ,DTSDKName ,UIStatusBarStyle ,CFBundleDevelopmentRegion ,DTPlatformName ,CFBundleInfoDictionaryVersion ,CFBundleSupportedPlatforms ,CFBundleExecutablePath ,CFBundleDisplayName ,LSRequiresIPhoneOS ,CFBundlePackageType ,CFBundleSignature … Read more

How to Get the Display Name with the Display ID in Mac OS X?

This gives you the localized display name: static void KeyArrayCallback(const void* key, const void* value, void* context) { CFArrayAppendValue(context, key); } – (NSString*)localizedDisplayProductName { NSDictionary* screenDictionary = [[NSScreen mainScreen] deviceDescription]; NSNumber* screenID = [screenDictionary objectForKey:@”NSScreenNumber”]; CGDirectDisplayID aID = [screenID unsignedIntValue]; CFStringRef localName = NULL; io_connect_t displayPort = CGDisplayIOServicePort(aID); CFDictionaryRef dict = (CFDictionaryRef)IODisplayCreateInfoDictionary(displayPort, 0); CFDictionaryRef names … Read more

+[NSString stringWithString:] — what’s the point?

You might have a NSMutableString (or some home-grown NSString subclass) that you want to duplicate. NSMutableString *buffer = [NSMutableString string]; // do something with buffer NSString *immutableStringToKeepAround = [NSString stringWithString:buffer]; Of course, you can also just make a copy: NSMutableString *buffer = [NSMutableString string]; // do something with buffer NSString *immutableStringToKeepAround = [[buffer copy] autorelease]; … Read more

Writing a privileged helper tool with SMJobBless()

XPC isn’t an option if you’re trying to elevate privileges (from https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingXPCServices.html): By default, XPC services are run in the most restricted environment possible—sandboxed with minimal filesystem access, network access, and so on. Elevating a service’s privileges to root is not supported. SMJobBless will install a helper tool and register it with Launchd, as in … Read more