Accessing Mac OS X Frameworks with F-Script

Revised April 2010

Articles in this series

     Fundamentals
  1. Exploring Cocoa with F-Script
  2. Learn F-Script in 20 Minutes and Have Fun Playing with Core Image
  3. Scripting Cocoa with F-Script
  4. Creating Cocoa Classes with F-Script
  5. System-wide Scripting with F-Script
  6. Embedding F-Script into Cocoa Applications
  7. Accessing Mac OS X Frameworks with F-Script
     Miscellaneous
  1. Google Chart API Fun with Cocoa and F-Script
  2. The Game of Reverse in F-Script

Introduction

F-Script.app, the standalone F-Script environment, is automatically linked with a number of Mac OS X frameworks. This is convenient for quickly exploring Mac OS X's capabilities and directly using the frameworks without any further configuration.

A practical example: accessing the Image Kit framework

We will illustrate this by accessing the Image Kit, a recent addition to the standard Mac OS X frameworks. We can launch F-Script.app and directly enter the following code:

IKPictureTaker pictureTaker orderFront:nil

It will activate the IKPictureTaker panel, a component of Image Kit with which users can, among other things, browse images in the file system and take snapshots with digital cameras (including the built-in iSight).

Then, once we've got the image we want, we can get the image objet (an NSImage instance) from the picture taker by typing the following instruction in the F-Script console:

myImage := IKPictureTaker pictureTaker outputImage

This F-Script code defines a variable named "myImage" and assigns it the NSImage object held by the picture taker.

Linked frameworks

The following table lists the frameworks that are automatically liked with F-Script.app:

          Framework           Description
Address Book Access to the contacts database
Application Kit User interface
Automator Automator plugin system
Calendar Store Access to the calendar database
Collaboration Identity information management
Core Animation Graphical animation
Core Audio Kit Audio Units custom views
Core Data Data model management (object persistence, etc.)
Core Image Image processing
Core Location Determining the geographical location of a computer
Core Video Video processing
Core WLAN Wireless networks access and management
Disc Recording CD and DVD burning
Disc Recording UI User interface components for CD and DVD burning
Disk Arbitration Hard disk events monitoring and management
Exception Handling Exception handling configuration
Foundation Kit Core services (root class, string handling, collections, networking, file management, threading, etc.)
Image Capture Core Access to digital devices such as scanners and cameras
Image Kit User interface components for image browsing and manipulation
Input Method Kit Input methods management
Instant Message Online status of instant messaging users
IOBluetooth Access to Bluetooth devices
IOBluetooth UI User interface components for access to Bluetooth devices
JavaVM Access to the Java environment
Open Directory Management of Open Directory information
OSA Kit Management and execution of OSA-compliant scripts
Preference Panes Implementing plugins for the System Preferences application
PDF Kit PDF handling
PubSub RSS and ATOM handling
QT Kit Rendering and manipulation of QuickTime content
Quartz Composer Access to Quartz Composer compositions
Quartz Filters Managing and applying filter effects to a graphics context
Screen Saver Implementing screen savers
Scripting Bridge Control of scriptable applications
Security Foundation Users authorization
Security Interface User interface components for users authorization
Server Notification Sending and receiving server-based notifications
Sync Services Data synchronization between multiples computers and devices
WebKit Web content rendering
Xgrid Foundation Cluster management

Loading other frameworks and bundles

You can dynamically load other frameworks and bundles, including some you might have created, using standards Cocoa techniques. The NSBundle class comes in handy here. For example, the following code loads the SenTesting Kit, a framework installed along with the Mac OS X developer tools:

(NSBundle bundleWithPath:'/Developer/Library/Frameworks/SenTestingKit.framework') load

Frameworks and bundles requiring 64 bits execution must be loaded in F-Script running in 64 bit (the default). For 32 bit bundles, you must launch F-Script in 32 bit mode (you do that just like with other applications: open the Finder inspector on F-Script.app and check "open in 32 bit mode").

If your bundle requires running with automatic garbage collection only or in reference counting mode only, you must make sure F-Script is the running in the correct mode, or your bundle won't load. You can configure F-Script's memory management mode from its preference panel.

If you encounter problems loading a bundle, you can use the method loadAndReturnError: instead od load to get error information. The code below shows how you can use this method in F-Script. In this example, we try to load the TextEdit application, but since this is not a loadable bundle, we get an error:

errorPointer := FSObjectPointer objectPointer.
(NSBundle bundleWithPath:'/Applications/TextEdit.app') loadAndReturnError:errorPointer.
errorPointer at:0.

The last instruction in the example above returns the NSError object and displays it when executed in the F-Script shell:

Error Domain=NSCocoaErrorDomain Code=3584 UserInfo=0x2004990e0 "The bundle "TextEdit" couldn't be loaded because its executable isn't loadable."

You can have F-Script automatically load additional frameworks or bundles at launch time by putting them in the F-Script repository. This repository is created by the F-Script.app application when launched for the first time. By default it will be at ~/Library/Application Support/F-Script (where ~ stands for your home directory). You can put our bundles and frameworks in the ~/Library/Application Support/F-Script/classes directory to have them loaded automatically by F-Scrit.app.

Alternatively, you can just create symbolic links, in the file system, pointing to the frameworks and bundles. For example, to create a link from the F-Script repository to the SenTesting Kit, you can execute the following F-Script code:

NSFileManager defaultManager createSymbolicLinkAtPath:'~/Library/Application Support/F-Script/classes/SenTestingKit.framework' stringByExpandingTildeInPath
                             withDestinationPath:'/Developer/Library/Frameworks/SenTestingKit.framework'
                             error:nil

Another option is to add explicit loading code to the latent script. When F-Script is launched, a text file named "fs_latent" is searched for at the top level of the F-Script repository. If this file exists it must contain the textual representation of a block literal. This block, the latent script, is then instantiated and evaluated. You can use this script to do whatever you want before the interactive F-Script.app session begins. For example, to load the SenTesting Kit, and if nothing else is done in the latent script, the fs_latent file will just contain the loading code wrapped in a block:

[(NSBundle bundleWithPath:'/Developer/Library/Frameworks/SenTestingKit.framework') load]

Constants

Most constants and enums defined in the Objective-C-based frameworks of Mac OS X are directly available in F-Script in their symbolic form. Currently, F-Script does not dynamically lookup new constants. Thus, if a constant we want to use (maybe defined in a framework you load dynamically) is not provided by F-Script in its symbolic form, you should fall back to directly using its raw value. You can usualy find constants definitions in the public header files of the frameworks.


Copyright © 2009-2010 Philippe Mougin