• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Problem with NSAppleScript execution
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Problem with NSAppleScript execution


  • Subject: Re: Problem with NSAppleScript execution
  • From: Jerry Krinock <email@hidden>
  • Date: Mon, 12 Sep 2011 17:55:35 -0700

On 2011 Sep 12, at 08:50, Nava Carmon wrote:

> I'm trying to run a simple Apple script, that gets the text from the frontmost application.

"Simple" is the way people who have never done this would describe it.

> It doesn't work consistently. Sometimes it works and sometimes - doesn't.

I presume you've tried making those delays longer than 0.1.

> Same script in the ScriptEditor btw works just fine :(

Well, then, rather than hard-coding it, include that .scpt file in your project, and in your Copy Resources Build Phase.  Like this… (error checking omitted)

NSString* scriptPath = [[NSBundle mainBundle] pathForResource:exformat
                                                       ofType:@"scpt"
                                                  inDirectory:nilOrWhatever] ;
NSURL* scriptUrl = [NSURL fileURLWithPath:scriptPath] ;
NSAppleScript* script = [[NSAppleScript alloc] initWithContentsOfURL:scriptUrl
                                                               error:&errorDic] ;
NSAppleEventDescriptor* descriptor = [script executeAndReturnError:&error] ;
[script release] ;

> Did somebody bump into such a thing before?

When I wanted to do this, I ended up specifying the menu items by number.  It's probably more fragile than keyboard shortcuts, but System Events are pretty fragile anyhow.  The AppleScript workflow is to keep trying different things that should work until you find one that does work.

Here is part of the script which I use:

tell application "System Events"
    set oldClipboard to the clipboard
    set the clipboard to ""
    tell process "Whatever"
        set frontmost to true
        set editCopyMenuItem to a reference to menu item 5 of menu of menu bar item 4 of menu bar 1
        set isEnabled to enabled of editCopyMenuItem as boolean
        if (isEnabled is true) then
            click editCopyMenuItem
        end if
    end tell
    delay 0.1
    copy (the clipboard) to selectedText
    set the clipboard to oldClipboard
end tell
-- Make sure that none of your property keys are AppleScript reserved words, nor used in the target app's terminology, or the following will give unexpected results…
return {whatever:whatever textIWant:selectedText, whateverElse:whateverElse}

Comment regarding the delay 0.1:

-- Sometimes, copy (the clipboard) gets previous clipboard data unless we use a delay.  The more, the better.  We chose 0.1 as a good compromise.  Even without this delay, this script takes 0.2 seconds to execute anyhow.

Regarding your code for processing the script output, it looks different than mine.  I use -[NSAppleEventDescriptor stringValue].  This works for me:

NSMutableDictionary* infoFromScript = [[NSMutableDictionary alloc] init] ;
NSInteger i ;
for (i=1; i<=[descriptor numberOfItems]; i++) {
    // This loop should only execute once; [descriptor numberOfItems] = 1
    NSAppleEventDescriptor* subdescriptor = [descriptor descriptorAtIndex:i] ;
    NSInteger nItems = [subdescriptor numberOfItems] ;
    if ((nItems > 0) && (nItems%2 == 0)) {
        NSUInteger j ;
        for(j=1; j<=[subdescriptor numberOfItems]/2; j++) {
            NSString* key = [[subdescriptor descriptorAtIndex:(2*j-1)] stringValue] ;
            NSString* value = [[subdescriptor descriptorAtIndex:(2*j)] stringValue] ;
            if (key && value) {
                [infoFromScript setObject:value
                                   forKey:key] ;
            }
        }
        break ;
    }
}

> More than that, when I run same script from Script editor, it works perfect. Is it a known problem or do I do something wrong?

Well, some would say that AppleScript is kind of a known problem :))

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Problem with NSAppleScript execution
      • From: Nava Carmon <email@hidden>
References: 
 >Problem with NSAppleScript execution (From: Nava Carmon <email@hidden>)

  • Prev by Date: Re: NSScanner to find multiple occurrences of a prefix?
  • Next by Date: Re: any way to know where a UIBarButtonItem has been drawn?
  • Previous by thread: Problem with NSAppleScript execution
  • Next by thread: Re: Problem with NSAppleScript execution
  • Index(es):
    • Date
    • Thread