Sunday, November 23, 2014

#1 2008-09-02 04:21:09 pm

Ka Ora!
Member
Registered: 2008-09-02
Posts: 7

Applescript for multiple keyboards

I dont know if this is the best place to start for a Script Noobie but here go, I am running Two keyboards one is a English (primary) and the other French both are Bluetoooth so each has its own Device ID what I would like to do is set up a script that sense's which keyboard is pressed and switches to the appropriate input language as Cmd Space means I have to look up to check the current  language and clicking the flag is a pain when shifting for a couple of special French Characters. Is this in the realms of possible.?  Im new to scripting but dont mind putting the time in to learn as I suspect it will help me understand how these machines work.

Cheers

Ka

Model: Mac Pro, MBP, iMac
Browser: Firefox 2.0.0.16
Operating System: Mac OS X (10.5)

Offline

 

#2 2008-09-02 06:58:37 pm

Adam Bell
Administrator
From: Nova Scotia, Canada
Registered: 2005-10-04
Posts: 4635

Re: Applescript for multiple keyboards

Welcome. I hope someone will be able to help, but most of us don't have two keyboards to try this on.

This script will tell you your keyboard layout name:

Applescript:

set myPrefs to POSIX path of (path to preferences folder from user domain as text)
set MAC to primary Ethernet address of (get system info)
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ":"
set MAC to (text items of MAC)
set AppleScript's text item delimiters to tid
set MAC to MAC as text

set L to paragraph -2 of (do shell script "defaults read " & myPrefs & "ByHost/com.apple.HIToolbox." & MAC & " | grep -A 4 AppleCurrentAsciiInputSource")


iMac & MBP both running OS X 10.9.1

Offline

 

#3 2008-09-02 07:34:38 pm

Craig Williams
Administrator
From: Ft. Smith, AR
Registered: 2006-12-06
Posts: 887

Re: Applescript for multiple keyboards

Hi Ka,

There are two ways that I am familiar with to change the input source. One is with AppleScript, see this post 'http://bbs.macscripter.net/viewtopic.php?pid=70174', and the other is with Objective-C.

Either way requires some method to fire the script. The bluetooth API is where you will need to start and since you will need to verse yourself in Obj-C I would suggest using that method as well to change the input.

Regards,

Craig

Offline

 

#4 2008-09-03 02:58:25 am

Ka Ora!
Member
Registered: 2008-09-02
Posts: 7

Re: Applescript for multiple keyboards

Thanks both, I am going to pursue this as It struck me as something that could be done.

Offline

 

#5 2008-09-03 11:23:19 am

Craig Williams
Administrator
From: Ft. Smith, AR
Registered: 2006-12-06
Posts: 887

Re: Applescript for multiple keyboards

Ka,

Here is the code I use on Leopard to change the input between English and Hebrew.
If you are on Tiger look up NSInputManager and check out this thread
http://www.cocoabuilder.com/archive/mes … 1/8/192549

Post back once you get it working. I am sure others would like to know how it is
accomplished. smile

Good luck!

Craig

#import <Foundation/Foundation.h>
#import <Carbon/Carbon.h>

/*
# author:     Craig Williams
# date:        2008-08-05
#
# Calling with AppleScript
# Changes input between English and Hebrew

property file_path : "/path/to/this/fileName"

do shell script "./" & file_path & " hebrew"

*/

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    // get array of arguments
    NSArray *args = [[NSProcessInfo processInfo] arguments];
    NSString *chosenInput = [args objectAtIndex:1];

    // get array of input sources
    CFArrayRef inputArray = TISCreateInputSourceList ( NULL, false);
    TISInputSourceRef english = CFArrayGetValueAtIndex(inputArray, 0);
    TISInputSourceRef hebrew = CFArrayGetValueAtIndex(inputArray, 1);
   
    if ( [chosenInput isEqual:@"english"])
    {
        OSStatus err = TISSelectInputSource (english);
        NSLog(@"Error changing to English: %@", err);
    } else {
        OSStatus err = TISSelectInputSource (hebrew);
        NSLog(@"Error changing to Hebrew: %@", err);
    }
   
    [pool drain];
    return 0;
}


Filed under: source, input, keyboard, language

Offline

 

#6 2008-09-04 04:04:13 am

Ka Ora!
Member
Registered: 2008-09-02
Posts: 7

Re: Applescript for multiple keyboards

Thanks very much going to follow this up....




Craig Williams wrote:

Ka,

Here is the code I use on Leopard to change the input between English and Hebrew.
If you are on Tiger look up NSInputManager and check out this thread
http://www.cocoabuilder.com/archive/mes … 1/8/192549

Post back once you get it working. I am sure others would like to know how it is
accomplished. smile

Good luck!

Craig

#import <Foundation/Foundation.h>
#import <Carbon/Carbon.h>

/*
# author:     Craig Williams
# date:        2008-08-05
#
# Calling with AppleScript
# Changes input between English and Hebrew

property file_path : "/path/to/this/fileName"

do shell script "./" & file_path & " hebrew"

*/

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    // get array of arguments
    NSArray *args = [[NSProcessInfo processInfo] arguments];
    NSString *chosenInput = [args objectAtIndex:1];

    // get array of input sources
    CFArrayRef inputArray = TISCreateInputSourceList ( NULL, false);
    TISInputSourceRef english = CFArrayGetValueAtIndex(inputArray, 0);
    TISInputSourceRef hebrew = CFArrayGetValueAtIndex(inputArray, 1);
   
    if ( [chosenInput isEqual:@"english"])
    {
        OSStatus err = TISSelectInputSource (english);
        NSLog(@"Error changing to English: %@", err);
    } else {
        OSStatus err = TISSelectInputSource (hebrew);
        NSLog(@"Error changing to Hebrew: %@", err);
    }
   
    [pool drain];
    return 0;
}

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.022 seconds, 10 queries executed ]

RSS (new topics) RSS (active topics)