PB Cocoa Gurus Help Please

Just starting out? Need help? Post your questions and find answers here.
User avatar
Piero
Addict
Addict
Posts: 1173
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

PB Cocoa Gurus Help Please

Post by Piero »

Help Please!
Is this "console app" easy to "port" to a PB Procedure(s.s)?
It's a text typing thing that works very well (via shell); it types any char 😀!
My PBCocoa is terrible! :oops:
Thanks!

Code: Select all

#import <Foundation/Foundation.h>
#include <CoreGraphics/CGEvent.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        if (argc > 1) {
            NSString *theString = [NSString stringWithUTF8String:argv[1]];
            NSUInteger len = [theString length];
            NSUInteger n, i = 0;
            CGEventRef keyEvent = CGEventCreateKeyboardEvent(nil, 0, true);
            unichar uChars[20];
            while (i < len) {
                n = i + 20;
                if (n>len){n=len;}
                [theString getCharacters:uChars range:NSMakeRange(i, n-i)];
                CGEventKeyboardSetUnicodeString(keyEvent, n-i, uChars);
                CGEventPost(kCGHIDEventTap, keyEvent); // key down
                CGEventSetType(keyEvent, kCGEventKeyUp);
                CGEventPost(kCGHIDEventTap, keyEvent); // key up (type 20 characters maximum)
                CGEventSetType(keyEvent, kCGEventKeyDown);
                i = n;
                [NSThread sleepForTimeInterval:0.003]; // wait 3/1000 of second
            }
            CFRelease(keyEvent);
        }
    }
    return 0;
}
User avatar
Piero
Addict
Addict
Posts: 1173
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: PB Cocoa Gurus Help Please

Post by Piero »

PS:
I just "silently updated" this, but it needs a better "PB typing engine" (as example for the Forum, not for me…)
PPS:
I am also investigating an EASY PBCocoa way to make an "example menu bar app" with global menu shortcuts (Global keyboard hook, without making all the other apps lose keyboard……)
Seems it needs a mix of CGEventTapCreate and PerformKeyEquivalent…
PPPS:
I wonder if there's an EASY PBCocoa way to get selected text (from services-enabled apps) like Automator does (to avoid having to make a service ETC.)… couldn't find it :/
User avatar
Piero
Addict
Addict
Posts: 1173
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: PB Cocoa Gurus Help Please

Post by Piero »

 
Solved!

I also fixed the "console app" (Xcode Objective-C) I found on the Net:

Code: Select all

//  typetext
//
//  main.m

#import <Foundation/Foundation.h>
#include <CoreGraphics/CGEvent.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        if (argc > 1) {
            NSString *theString = [NSString stringWithUTF8String:argv[1]];
            long len = [theString length];
            UniChar uChars[len];
            [theString getCharacters:uChars range:NSMakeRange(0, len)];
            CGEventRef keyEvent = CGEventCreateKeyboardEvent(nil, 0, true);
            CGEventKeyboardSetUnicodeString(keyEvent, len, uChars);
            CGEventPost(kCGHIDEventTap, keyEvent);
            [NSThread sleepForTimeInterval:0.001]; // wait 1/1000 of second
            CFRelease(keyEvent);
        }
    }
    return 0;
}

Code: Select all

Procedure simpleShell(ShellCommand$)
   Protected shell = RunProgram("/bin/zsh","-l","", #PB_Program_Open|#PB_Program_Write)
   WriteProgramStringN(shell,ShellCommand$) : WriteProgramData(shell,#PB_Program_Eof,0) : CloseProgram(shell)
EndProcedure

Procedure.s QP(str$) ; to quote paths and strings for shell
   ProcedureReturn "'" + ReplaceString(str$, "'", "'\''") + "'"
EndProcedure

CocoaMessage(0,CocoaMessage(0,0,"NSWorkspace sharedWorkspace"),"launchApplication:$",@"TextEdit")
Delay(1000) ; wait for textedit to activate
simpleShell("typetext "+ QP(~"🍍🍕😟\n")) ; typetext is in PATH here…
Post Reply