MyScriptsGlue.c

 
/*
    File:       MyScriptsGlue.c
    
    Description:
    routines providing the c glue code for calling the AppleScripts
    defined in the file AddOnScripts.applescript.  The routines in this
    file are constructed to provide an interface between your
    application and the scripts defined in AddOnScripts.applescript.  Though
    the scripts themselves may be changed dramatically for different purposes,
    requirements, and server applications that are called by the scripts, there
    is no need to change any of the routines defined in this file (unless, of
    course, you're adding new ones or additional functionality).
 
    Author:     JM
 
    Copyright:  Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
    
    Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
                ("Apple") in consideration of your agreement to the following terms, and your
                use, installation, modification or redistribution of this Apple software
                constitutes acceptance of these terms.  If you do not agree with these terms,
                please do not use, install, modify or redistribute this Apple software.
 
                In consideration of your agreement to abide by the following terms, and subject
                to these terms, Apple grants you a personal, non-exclusive license, under AppleÕs
                copyrights in this original Apple software (the "Apple Software"), to use,
                reproduce, modify and redistribute the Apple Software, with or without
                modifications, in source and/or binary forms; provided that if you redistribute
                the Apple Software in its entirety and without modifications, you must retain
                this notice and the following text and disclaimers in all such redistributions of
                the Apple Software.  Neither the name, trademarks, service marks or logos of
                Apple Computer, Inc. may be used to endorse or promote products derived from the
                Apple Software without specific prior written permission from Apple.  Except as
                expressly stated in this notice, no other rights or licenses, express or implied,
                are granted by Apple herein, including but not limited to any patent rights that
                may be infringed by your derivative works or by other works in which the Apple
                Software may be incorporated.
 
                The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
                WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
                WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
                COMBINATION WITH YOUR PRODUCTS.
 
                IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
                CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
                GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
                OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
                (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
                ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                
    Change History (most recent first):
        Fri, Aug 29, 2000 -- created
*/
 
#include "MyScriptsGlue.h"
#include "ScriptSupport.h"
 
 
    /* we store our persistent state for routines defined in
    this file in a global variable. */
LoadedScriptInfoPtr gScriptsInfo = NULL;
 
 
 
 
OSStatus OpenMyScriptsGlue(void) {
    return LoadCompiledScript(CFSTR("AddOnScripts"), &gScriptsInfo);
}
 
 
 
OSStatus CloseMyScriptsGlue(void) {
    UnloadCompiledScript( gScriptsInfo );
    gScriptsInfo = NULL;
    return noErr;
}
 
 
 
    /* script_callnamedhandler is the c glue code for the simple handlers with no
    parameters in the script file:
    
        on handlername()
            ....
            return "optional result"
        end handlername
        
    set theResult to NULL if you're not interested in receiving a result back from
    the handler. 
    */
OSStatus script_callnamedhandler(char* handlername, AEDesc *theResult) {
        
    OSStatus err;
    AEDescList theParameters;
 
        /* set up our parameter list for the subroutine, none in this case */
    err = AEBuildDesc( &theParameters, NULL, "[]");
    if ( noErr == err ) {
        
            /* call the subroutine */
        err = CallScriptSubroutine( gScriptsInfo, handlername, &theParameters, theResult, NULL );
        
            /* clean up storage allocated for parameters */
        AEDisposeDesc(&theParameters);
    }
    return err;
}
 
 
 
 
 
 
 
 
 
 
    /* script_selectfile is the c glue code for the following script in
    our scripts file:
        on selectfile()
            set x to choose file
            return x
        end selectfile
     */
OSStatus script_selectfile(CFStringRef thePromptText, AliasHandle *theFile) {
        
    OSStatus err;
    AEDesc theFileDesc, thePrompt;
    AEDescList theParameters;
    
        /* convert the message string into a unicode AEDesc record */
    err = CFStringToAEDesc( thePromptText, &thePrompt );
    if ( noErr == err ) {
    
            /* set up our parameter list for the subroutine */
        err = AEBuildDesc( &theParameters, NULL, "[@]", &thePrompt);
        if ( noErr == err ) {
        
                /* call the subroutine */
            err = CallScriptSubroutine( gScriptsInfo, "selectfile", &theParameters, &theFileDesc, NULL );
            if ( noErr == err ) {
            
                    /* extract the file reference from the result */
                AliasHandle localAlias;
                err = AEDescToAlias( &theFileDesc, &localAlias);
                if ( noErr == err ) {
                
                        /* save the result */
                    *theFile = localAlias;
                }
                AEDisposeDesc(&theFileDesc);
            }
            
                /* clean up storage allocated for parameters */
            AEDisposeDesc(&theParameters);
        }
        
            /* clean up storage allocated for the prompt */
        AEDisposeDesc(&thePrompt);
    }
    
    return err;
}
 
 
 
 
 
    /* script_displayfile is the c glue code for the following script in
    our scripts file:
    
        on displayfile(theFile)
            ignoring application responses
                tell application "Finder"
                    activate
                    select theFile
                end tell
            end ignoring
        end displayfile
        
    Of interest here is the 'ignoring application responses' block.   Essentially,
    that means that this script will complete and return possibly before the finder
    has completed displaying the file.  In cases where you don't need a result right
    away, this can be useful to speed things up because your app doesn't need to 
    wait for other apps to finish what you're asking them to do. 
    */
OSStatus script_displayfile(AliasHandle targetFile) {
        
    OSStatus err;
    AEDescList theParameters;
 
        /* set up our parameter list for the subroutine */
    err = AEBuildDesc( &theParameters, NULL, "[alis(@@)]", targetFile);
    if ( noErr == err ) {
        
            /* call the subroutine */
        err = CallScriptSubroutine( gScriptsInfo, "displayfile", &theParameters, NULL, NULL );
        
            /* clean up storage allocated for parameters */
        AEDisposeDesc(&theParameters);
    }
    return err;
}
 
 
 
 
 
 
 
 
    /* script_getfolderItems is the c glue code for the following script in
    our scripts file:
        on getfolderitems(promptstring)
            set theFolder to choose folder with prompt promptstring
            set folderPath to theFolder as string
            set folderItems to list folder theFolder
            set theList to {}
            repeat with i in folderItems
                set nthName to i as string
                if nthName does not start with "." then
                    set nthItem to {nthName, (folderPath & nthName) as alias}
                    copy nthItem to the end of theList
                end if
            end repeat
            return theList
        end getfolderItems
    */
OSStatus script_getfolderitems(CFStringRef thePromptText, AEDescList *itemsList) {
        
    OSStatus err;
    AEDesc thePrompt;
    AEDescList theParameters;
    
        /* convert the message string into a unicode AEDesc record */
    err = CFStringToAEDesc( thePromptText, &thePrompt );
    if ( noErr == err ) {
    
            /* set up our parameter list for the subroutine */
        err = AEBuildDesc( &theParameters, NULL, "[@]", &thePrompt);
        if ( noErr == err ) {
        
                /* call the subroutine */
            err = CallScriptSubroutine( gScriptsInfo, "getfolderitems", &theParameters, itemsList, NULL );
            
                /* clean up storage allocated for parameters */
            AEDisposeDesc(&theParameters);
        }
        
            /* clean up storage allocated for the prompt */
        AEDisposeDesc(&thePrompt);
    }
    
    return err;
}
 
 
 
 
    /* script_displaymessage is the c glue code for the following script in
    our scripts file:
        on displaystring(message)
            display dialog message
        end displaystring
     */
OSStatus script_displaymessage(CFStringRef theMessageText) {
        
    OSStatus err;
    AEDescList theParameters;
    AEDesc theMessage;
    
        /* convert the message string into a unicode AEDesc record */
    err = CFStringToAEDesc( theMessageText, &theMessage );
    if ( noErr == err ) {
    
            /* set up our parameter list for the subroutine */
        err = AEBuildDesc( &theParameters, NULL, "[@]", &theMessage);
        if ( noErr == err ) {
            
                /* call the subroutine */
            err = CallScriptSubroutine( gScriptsInfo, "displaystring", &theParameters, NULL, NULL );
            
                /* clean up storage allocated for parameters */
            AEDisposeDesc(&theParameters);
        }
        
            /* clean up storage allocated for the message string */
        AEDisposeDesc(&theMessage);
    }
    return err;
}
 
 
 
 
 
 
    /* script_filetostring is the c glue code for the following script in
    our scripts file:
        on filetostring(theFile)
            return (theFile as string)
        end filetostring
     */
OSStatus script_filetostring(AliasHandle targetFile, CFStringRef *theFileString) {
        
    OSStatus err;
    AEDescList theParameters;
    AEDesc theString;
    CFStringRef localString;
 
        /* set up our parameter list for the subroutine */
    err = AEBuildDesc( &theParameters, NULL, "[alis(@@)]", targetFile);
    if ( noErr == err ) {
        
            /* call the subroutine */
        err = CallScriptSubroutine( gScriptsInfo, "filetostring", &theParameters, &theString, NULL );
        if ( noErr == err ) {
        
                /* get the comment text from the result */
            err = AEDescToCFString( &theString, &localString );
            if ( noErr == err ) {
            
                    /* store our result */
                *theFileString = localString;
            }
            
                /* clean up storage returned for the comment */
            AEDisposeDesc(&theString);
        }
        
            /* clean up storage allocated for parameters */
        AEDisposeDesc(&theParameters);
    }
    return err;
}
 
 
 
 
    /* script_getfilecomment is the c glue code for the following script in
    our scripts file:
        on getfilecomment(theFile)
            tell application "Finder"
                set x to comment of theFile
            end tell
            return x
        end getfilecomment
     */
OSStatus script_getfilecomment(AliasHandle targetFile, CFStringRef *theFileComment) {
        
    OSStatus err;
    AEDescList theParameters;
    AEDesc theComment;
    CFStringRef localCommentText;
 
        /* set up our parameter list for the subroutine */
    err = AEBuildDesc( &theParameters, NULL, "[alis(@@)]", targetFile);
    if ( noErr == err ) {
        
            /* call the subroutine */
        err = CallScriptSubroutine( gScriptsInfo, "getfilecomment", &theParameters, &theComment, NULL );
        if ( noErr == err ) {
        
                /* get the comment text from the result */
            err = AEDescToCFString( &theComment, &localCommentText );
            if ( noErr == err ) {
            
                    /* store our result */
                *theFileComment = localCommentText;
            }
            
                /* clean up storage returned for the comment */
            AEDisposeDesc(&theComment);
        }
        
            /* clean up storage allocated for parameters */
        AEDisposeDesc(&theParameters);
    }
    return err;
}
 
 
 
 
    /* script_setfilecomment is the c glue code for the following script in
    our scripts file:
        on setfilecomment(theFile, thecomment)
            tell application "Finder"
                set (comment of theFile) to thecomment
            end tell
            return thecomment
        end setfilecomment
     */
OSStatus script_setfilecomment(AliasHandle targetFile, CFStringRef theFileComment) {
        
    OSStatus err;
    AEDescList theParameters;
    AEDesc theComment;
 
        /* convert the message string into an AEDesc for use as a parameter */
    err = CFStringToAEDesc( theFileComment, &theComment );
    if ( noErr == err ) {
    
            /* set up our parameter list for the subroutine */
        err = AEBuildDesc( &theParameters, NULL, "[alis(@@),@]", targetFile, &theComment);
        if ( noErr == err ) {
            
                /* call the subroutine */
            err = CallScriptSubroutine( gScriptsInfo, "setfilecomment", &theParameters, NULL, NULL );
 
                /* clean up storage allocated for parameters */
            AEDisposeDesc(&theParameters);
        }
            /* clean up storage allocated for the comment text string */
        AEDisposeDesc(&theComment);
    }
    return err;
}