bendan
bendan

Reputation: 11

script auto correcting incorrectly when i compile and key down key up not working as well

alright I'm currently having a lot of issues with my applescript editor. currently when ever i do a Key up key down sequence it will always key the letter A. for example.

tell application "System Events"

key down "u"

key up "u"

    delay 1

end tell

this will keystroke A and i don't know why.

^ thats only a minor issue though my big problem is that i want to swap between three different video game applications. They are all a duplicate of the same game. Heres how my basic script goes.

tell application "Video Game" to activate

tell application "System Events"
keystroke a bunch of stuff
end tell

tell application "Video Game copy" to activate

tell application "System Events"
keystroke a bunch of stuff
end tell

tell application "Video Game copy 2" to activate

tell application "System Events"
keystroke a bunch of stuff
end tell

The problem is when i try to compile the script the application name always corrects to "Video Game".

Any ideas on how to keep it from compiling in correctly?

I have tried putting in a bogus name then when applescript asks me to find the app i click the correct one under the browse search box. and it will still compile incorrectly.

thanks a bunch :)

Upvotes: 1

Views: 439

Answers (3)

Snow
Snow

Reputation: 141

what you describe seems a bug in applescript. Here is a little workaround for that. I wrote it for jxa (javascript for ui automation), so you have to port it to applescript i think:

ObjC.import("Cocoa");

function key(aiKeyCode, aiDelay)
{
    var source = $.CGEventSourceCreate($.kCGEventSourceStateCombinedSessionState),
    keyDown = $.CGEventCreateKeyboardEvent(source, aiKeyCode, true),
    keyUp = $.CGEventCreateKeyboardEvent(source, aiKeyCode, false);
    $.CGEventPost($.kCGAnnotatedSessionEventTap, keyDown);
    delay(aiDelay);
    $.CGEventPost($.kCGAnnotatedSessionEventTap, keyUp);
}

Upvotes: 1

Simon White
Simon White

Reputation: 736

What AppleScript Editor is doing is not “autocorrect” — it’s “compiling.” It’s not doing it incorrectly. You can change the name of an app on the Mac and it doesn’t break the app. AppleScript Editor is still able to find the app “Video Game” even if you change its name to “Video Game copy 1.” This is a feature, not a bug.

Upvotes: 0

double_j
double_j

Reputation: 1716

keystroke will get the key to be pressed once. It helps to tell the process App you are telling System Events to do something, i.e.

tell application "System Events" to tell process "App Name" to keystroke "u"

If you want to do it with command, option, shift, etc. use this:

tell application "System Events" to tell process "App Name" to keystroke "u" using {command down, shift down, option down}

Upvotes: 0

Related Questions