Re: Problem with NSAppleScript execution
Re: Problem with NSAppleScript execution
- Subject: Re: Problem with NSAppleScript execution
- From: Nava Carmon <email@hidden>
- Date: Tue, 13 Sep 2011 08:01:49 +0300
Thanks for answering, Jerry.
On Sep 13, 2011, at 3:55 AM, Jerry Krinock wrote:
> 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.
Yes, I tried, It didn't make things work more consistent :(
>
>> 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] ;
I have a scpt file, that does it and tried use it also. The script activates the frontmost application
>
>> 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.
0.1 makes you wait reasonable amount of time. And as I use it twice or more, you come to some 0.5 delay which is already something that feels a bit slow.
>
> 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 ;
> }
> }
I found that it brings the unicode characters. I'll try string value.
>
>> 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 :))
I have another question.
My application intercepts clicks/key down events and brings the text from the frontmost application to my application on catching a certain key sequence. Then it evaluates it and pastes fixed text back to the application, that was frontmost.
I tried using accessibility APIs, but not all applications support it. AppleScript seems to be the universal solution in this case, since it's mostly simulates copy & paste key sequences, but may be there are another technologies, that can be used for this purpose. Are you aware of something more robust?
Thanks,
Nava
>
> _______________________________________________
>
> 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
Nava Carmon
email@hidden
"Think good and it will be good!"
_______________________________________________
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