Page 1 of 1

PB Cocoa Gurus Help Please

Posted: Thu Dec 04, 2025 4:10 pm
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;
}

Re: PB Cocoa Gurus Help Please

Posted: Thu Dec 04, 2025 6:46 pm
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 :/

Re: PB Cocoa Gurus Help Please

Posted: Sat Dec 06, 2025 2:06 am
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…