Does ASObjC work with Core Foundation?




Does ASObj-C work with core foundation? If so, is there any reason to learn about the framework? I looked at the functions in core foundation (it seems like it’s all functions in core foundation) and nothing in there seemed all that useful given ASObj-C users have access to AppleScript functionality and the foundation framework.

I tried woking with core foundation in AppleScript but I could never get anything to work. I tried a lot of things, here is a list of a few of them to show the kind of errors I got. I thought the tool free __NSCFString could somehow be coerced to a CFString but I could never get it to work.

use framework "CoreFoundation"

-- This got an error trying to convert to type CFString
set TheVar to current application's NSString's stringWithString:"test" --> (NSString) "test"
TheVar's className() as __CFString --> (NSString) "__NSCFString"
current application's CFString's CFStringGetLength(TheVar)

--This got the error CFStringCreateWithCharacters unable to set argument 1 - the AppleScript value
-- <NSAppleEventDescriptor: 'utxt'("TheVar")> could not be coerced to type S.
CFStringCreateWithCharacters(missing value, "TheVar", 4)

-- Got error CFString doesn’t understand the “CFStringCreateWithCharacters_” message.
current application's CFString's CFStringCreateWithCharacters_(missing value, "Test", 4)


(Shane Stanley) #2

Not from an AppleScriptObjC perspective, no. The biggest problem is that the scripting bridge doesn’t support the C-style value types it uses.

There’s not a lot in it that isn’t available in Foundation anyway. Sometimes it’s been used in preference to Foundation for speed, but the emphasis these days is on Foundation. In fact the Sierra release notes say this:

Many types in Foundation and CF are bridged such that they have a C interface exposed by CF, and an Objective-C class interface exposed by Foundation. In previous releases of macOS and iOS, which of these has better performance has been somewhat unintuitive, and has varied over the years. In macOS 10.12 and iOS 10.0, we’ve simplified this: the recommended API (Foundation) is also the fastest API in nearly all cases now.



I’ve been working on a way to find all the frameworks that work with ASObj-C. Since ASObj-C only imports Objective-C classes I used BBedit and searched the header files in Xcode for an occurrence of @interface to find what headers files that contain a declaration of an objective-C class. Of course if @interface appears in a comment in the file it would also appear in the list.

I ended up with a list of 78 frameworks that declare 1 or more Objective-C class. That means there are 78 frameworks that might have something ASObj-C can use. Any framework that has something that works with ASObj-C should appear in this list. The trick is figuring out which ones in the list actually work work with ASObj-C.

I did inclose a text file with a list of the 78 frameworks in this post.

Do you have an idea of how to figure out which of the 78 frameworks work with ASObj-C?

Possible classes that work with (1.2 KB)

(Shane Stanley) #4

The limitation is not the framework or whether it involves Objective-C, but whether the arguments used in methods or functions can be bridged. So I can run this:

use framework "CoreFoundation"

current application's CFTimeZoneCopyKnownNames()

because there are no arguments that can’t be bridged. But the result type is also not bridged, so the result is useless. And that holds for most, but not all, C-style functions. You can use NSHomeDirectoryForUser(), from Foundation, for example, because it takes an NSString as an argument and returns an NSString.

Other methods can’t be used because they use blocks.

Script Debugger includes terminology files for code-completion for the following system frameworks: Foundation, AppKit, AddressBook, AVFoundation, Contacts, CoreImage, CoreLocation, CoreWLAN, EventKit, JavascriptCore, MapKit, OSAKit, Quartz, and WebKit.

(Harry Arora) #5

Instead of using Applescript, you can use its counterpart Javascript. Apple has made all the functionality of Applescript available in Javascript and even more. You can use C-style values as well.

Following is the snippet of the code that will work in the “Script Editor” if you change Applescript to Javascript(upper-left corner). To learn more about Javascript Automation in MacOS go to this link

nil = $()
cfstringref = $.CFStringCreateWithCString(nil, "/path/to/test", $.kCFStringEncodingUTF8)
cfurlref = $.CFURLCreateWithFileSystemPath(nil, cfstringref, $.kCFURLPOSIXPathStyle, false);

(Jim Underwood) #6

Thanks for the example, but it is unclear to me what it is supposed to do, and how to use it. Also, it is best to put all code/scripts in a forum Code Block.
You script contains fancy quotes that can’t be used.