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

只愿长相守 提交于 2019-12-11 12:10:35

问题


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 :)


回答1:


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);
}



回答2:


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}



回答3:


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.



来源:https://stackoverflow.com/questions/25366014/script-auto-correcting-incorrectly-when-i-compile-and-key-down-key-up-not-workin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!