Method Lag With Apple Script

Discussion in 'Mac Programming' started by Darkroom, Jul 24, 2008.

  1. Darkroom Guest


    Dec 15, 2006
    Montréal, Canada
    i have an IBAction that executes an Apple Script, flashes the screen and plays a sound, but there is often a lag between the Apple Script and the cocoa functions. if i continuously repeat this method, it eventually becomes in sync... is there anyway to make it always in sync?

    here is apart of the IBAction:
    	NSString *myStringScript = [NSString stringWithFormat:
    								set LIGHTdesktop to POSIX file \"%@\"\n\
    								tell application \"Finder\"\n\
    								set desktop picture to alias LIGHTdesktop\n\
    								end tell", usedPath];
    	NSDictionary* errorDict;
    	NSAppleEventDescriptor* returnDescriptor = NULL;
    	NSAppleScript* scriptObject = [[NSAppleScript alloc] initWithSource:myStringScript];
    	returnDescriptor = [scriptObject executeAndReturnError: &errorDict];
    	if ([[soundRadioGroup cellWithTag:1] state] == NSOnState)
    		NSSound *desktopSound = [NSSound soundNamed:@"Desktop.aif"];
    		[desktopSound play];
    	[NSAppleScript release];
    i think i would be able to make it always in sync if i could combine the FlashMenuBar(0) and maybe the sound effect with the Apple Script, i can't seem to find any information on how to use Apple Script to perform cocoa's FlashMenuBar(0); function... or maybe there is a better way of doing this that i don't know about?

  2. cMacSW macrumors regular

    Mar 20, 2006
    The lag could be related to compiling the applescript, try compiling the applescript first with:

    - (BOOL)compileAndReturnError:(NSDictionary **)errorInfo
    then call the execute method. You may want to create and compile once before your IBAction as well.
  3. Darkroom thread starter Guest


    Dec 15, 2006
    Montréal, Canada
    thanks... i tried this as per your suggestion and it still didn't work... but it made me realize that it's the nature of the applescript that's the problem. the applescript changes the desktop picture, and since i have no control over how fast the computer does this, the desktop background change is often out of sync with the other functions in my method (especially the first time it's executed). the only thing i can think of how to remedy this is to have some sort of listener that will flash the screen when the desktop picture is changed, but there's no guarantee that too would be in sync...

    i'm defeated...

Share This Page