Apple Script Query

Discussion in 'Mac Programming' started by Suikoden, Feb 11, 2018.

  1. Suikoden macrumors newbie

    Joined:
    Feb 11, 2018
    #1
    Dear All,

    I have a query about AppleScript. I have no programming experience, so if I ask stupid things then please forgive me. I am seeking out your help because I am unable to use AutoHotKeys on my MacBook Pro and I hope that AppleScript is an alternative. I shall let you decide this once you have read my problem.

    Ok, so there is a game on the Playstation 4 called Final Fantasy IX. Within that game are two conditions to be met for two trophies to pop.

    1. Kill 10,000 enemies
    2. jump rope 1000 times

    Now, the first one, in terms of scripting anyway, is fairly simple (hopefully).

    What gamers have found is that they can use an application on their PC or Mac called PS4 Remote Play. This allows a person to play their PS4 remotely on their Mac or PC, with the game coming up on the computer screen.

    Input via keyboard when in remote play is limited but the PS4 “x” button is the “enter” key, and that is all we need for these parts of the game.

    So, at a few parts of the game, enemies come to you (so no movement input is needed). You begin a fight and hit “x” or “enter” to attack and keep doing so until the enemy is dead. Then another enemy comes to you and you rinse and repeat until you have 10,000 enemies dead.

    Therefore, my first question is: Can a script be made that repeatedly presses the “enter” key indefinitely within the remote play app? As it will take a while to get to 10,000 it would run for a while and I can do other things.

    ——

    The second element of jump rope is what is causing people more of a head ache. This is not a straight “hit the button over and over 1000 times”. After 50 jumps, the speed increases and then after 100 jumps the rhythm changes to jump jump, jump jump, jump jump. Then after 300 it changes back to jump, jump, jump forever.

    People have used AutoKeyScript (amongst other apps not available on Mac) to mimic the required input. There have been severe issues with lag as you can imagine and so a delay is added to attempt to sort these issues. It works for some, but others have been having major issues with it.

    Here is link to Playstationtrophies.org, which is the forum that the original AutoHotKey script has been posted to (credit to all those involved in that). You will see it is very in-depth and there are pages of questions etc, now running to nearly 600 pages. The actual script is housed in the first post however.

    So, my question for this second script is: Can what the AutoHotKey script does, be done in AppleScript?

    If so to both my questions: Can anyone help me out? I do not want to put windows on my Mac in order to do this so I turn to you.

    Kind thanks,



    Tom
     
  2. DennisBlah macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #2
    Code:
    display dialog "Bring up your window where you want to hit ENTER 1000000 times, you have 5 seconds to do so"
    delay 5
    tell applicatoin "System Events"
        repeat 1000000 times
            keystroke return
            delay 0.1
        end repeat
    end tell
    
    Something like this ?

    If there is any logic for your jumping... You need to timer perfectly how long the delay is between your button presses, and then write a code around it.
     
  3. Suikoden thread starter macrumors newbie

    Joined:
    Feb 11, 2018
    #3
    Thanks for this...How do you run the code after you have written it? Sorry, i have no idea. And will this hit "enter" 1000000? It says "Keystroke return", is return the enter key?

    And what is the tell application "system events"?

    As for the jumping, there is a specific time between jumps, but there is a lag between the remote play app and the PS4 which requires a delay being built in (due to the internet interference). The code in the link in my first post shows this, but I cannot even begin to understand it.

    thanks for your help so far.
    --- Post Merged, Feb 12, 2018 ---
    Sorry for a second post, but the edit button does not seem to work.

    Ok, i used your code and tested it on Word. I clicked the play icon on the AppleScript and it came up with a prompt saying go to your application. So I opened word and YES it moved the text down the screen. AWESOME.

    However, is there a way of cancelling out of the script? I could not get back on to the script page to turn it off. Couldn't shut down Word or go to a different programme. I had to power off the Mac.

    Am I missing something? Is it possible to put a stop command in there that frees up the computer again? I.e when you press "S" the script stops?

    Many thanks in advance
     
  4. DennisBlah macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #4
    Hi Suikoden,

    I'm sorry :D
    You could have wait till the script finished ;-)
    I like to have my scripts 'readable' so I chose keystroke, you can also use key code but then you need key numbers, like.. key code 13 is also pressing return key
    If you want to write a line to ... web whatsapp ? ;)
    Just change keystroke return to:
    keystroke "Hello there!" & return

    You can do a lot of fun things like that :)

    I'm not going to write a whole 'cheat' for you in AppleScript, I would've written it in C+ or something else ;)
    Below sample, will press the Enter key 1000000 times with a interval of .1 second. (this might be slightly off due key detection)
    If the Shift button is pressed, it will exit the >repeater< not the script.


    I will share you a hackish, but fastest way to detect if the >SHIFT< key has been pressed.
    This post might be usefull to other coders as well :) There are other ways to detect key pressed, most of them use a shell script, which is really slow.

    Code:
    display dialog "Bring up your window where you want to hit ENTER 1000000 times, you have 5 seconds to do so after pressing OK"
    delay 5
    
    repeat 1000000 times
       if shiftKeyPressed() is true then exit repeat
    
       tell application "System Events" to keystroke return
       delay 0.1
    end repeat
    
    display dialog "Script Ended"
    
    --Actual shift key detection below
    use framework "Foundation"
    use framework "AppKit"
    
    on shiftKeyPressed()
       set |DnS| to current application
       set cMods to |DnS|'s class "NSEvent"'s modifierFlags()
       return (cMods div (get |DnS|'s NSShiftKeyMask) mod 2 is 1)
    end shiftKeyPressed
    

    _________
    Don't p155 people off ;-)
     
  5. Suikoden thread starter macrumors newbie

    Joined:
    Feb 11, 2018
    #5
    Code:
    display dialog "Bring up your window where you want to hit ENTER 1000000 times, you have 5 seconds to do so after pressing OK"
    delay 5
    
    repeat 1000000 times
       if shiftKeyPressed() is true then exit repeat
    
       tell application "System Events" to keystroke return
       delay 0.1
    end repeat
    
    display dialog "Script Ended"
    
    --Actual shift key detection below
    use framework "Foundation"
    use framework "AppKit"
    
    on shiftKeyPressed()
       set |DnS| to current application
       set cMods to |DnS|'s class "NSEvent"'s modifierFlags()
       return (cMods div (get |DnS|'s NSShiftKeyMask) mod 2 is 1)
    end shiftKeyPressed
    
    Thanks for this...Do you need the stuff after "--Actual shift key detection below"? What does that do?

    Thanks again for taking the time.
     
  6. DennisBlah macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #6
    Hi Suikoden,

    you don't really need to have the line with: --Actual shift key detection below
    -- is just for a comment line (single line)
    You can also have
    (* comment
    block
    (multiple lines)*)

    Everything in the comment line or block is not being executed.


    However I had a quick look at the script you mention, but it seems you have to press and hold the Enter key for a set amount of time and then release.

    This is not working quite right with ANY keys using applescript.
    If it's possible to change the button to... in example, control key (we are already using shift) we can do this.
    Code:
    tell application "System Events"
    key down {control}
    delay 0.5
    key up {control}
    end tell
    
     
  7. Suikoden, Feb 14, 2018
    Last edited: Feb 14, 2018

    Suikoden thread starter macrumors newbie

    Joined:
    Feb 11, 2018
    #7
    Why would there need to be a delay in holding down the enter key? surely a "keystroke" is the key down and key up all in one?

    SO if I did the below changes, would it work

    Code:
    display dialog "Bring up your window where you want to hit ENTER 1000000 times, you have 5 seconds to do so after pressing OK"
    delay 5
    
    repeat 1000000 times
       if shiftKeyPressed() is true then exit repeat
    
        tell application "System Events"
            key down {enter}
            delay 0.1
            key up {enter}
        end tell
    end repeat
    
    display dialog "Script Ended"
    
    --Actual shift key detection below
    use framework "Foundation"
    use framework "AppKit"
    
    on shiftKeyPressed()
       set |DnS| to current application
       set cMods to |DnS|'s class "NSEvent"'s modifierFlags()
       return (cMods div (get |DnS|'s NSShiftKeyMask) mod 2 is 1)
    end shiftKeyPressed
     
  8. DennisBlah, Feb 14, 2018
    Last edited: Feb 15, 2018

    DennisBlah macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #8
    Hi,

    like I said key down does not work on all keys, it works on control, option, command and shift.
    So if it’s possible to re-map the enter key in your application.. that would be great, else you’ll need to find something else or write actual application

    I said this because i saw a sleep 50, sleep 45 and other in between..

    If I look at the script in the link :
    Code:
    loop 9{
    Send {enter down}
    sleep 50
    Send {enter up}
    sleep 593
    Send {enter down}
    sleep 50
    Send {enter up}
    sleep 588
    }
    
    If you can use control instead of enter, the script for this step would be:
    Code:
    repeat 9 times
    (* key press check here *)
    tell application “system events”
    key down {control}
    delay 0.05
    key up {control}
    
    delay 0.593
    
    key down {control}
    delay 0.05
    key up {control}
    delay 0.588
    end tell
    
    (* You might want to change ‘exit repeat’ to ‘return’
    so you can stop the whole script instead of stopping the ‘repeat’*)
    
    p.s. the sleep in the original script is in miliseconds, applescript is in seconds, not sure if the above will work (never delayed with lower than 1 decimal before)
    And if you cant change the keys in the application you might jist try
    keystroke return
    instead of key down, delay, key up

    p.p.s I wrote this on my phone, so there might ve typo’s
     
  9. Suikoden thread starter macrumors newbie

    Joined:
    Feb 11, 2018
    #9
    So what you are saying is that I can’t emulate pressing and releasing the enter key in apple script?

    Meaning that the original script you’ve given me is simply “holding down the enter key”?

    I cannot reassign the keys in the application. Does that mean apple script cannot help me? Seems a pretty crazy limitation that you can only emulate pressing a key with only limited keys.
     
  10. DennisBlah, Feb 15, 2018
    Last edited: Feb 15, 2018

    DennisBlah macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #10
    Again like said before, the function 'keystroke' does press and release, you can put whole text in there and it will actually 'type' it.
    but the functions 'key down' and 'key up' only work on 'modifier keys' thus will not work with the 'enter' key.

    This will only affect you if you really need to press and hold the key for certain time.
     
  11. Suikoden thread starter macrumors newbie

    Joined:
    Feb 11, 2018
    #11
    Oh I see, I was getting what you were saying reversed. Thanks for your help, you are awesome
     

Share This Page

10 February 11, 2018