Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Suikoden

macrumors newbie
Original poster
Feb 11, 2018
6
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
 
  • Like
Reactions: OvidKing

DennisBlah

macrumors 6502
Dec 5, 2013
485
2
The Netherlands
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.
 

Suikoden

macrumors newbie
Original poster
Feb 11, 2018
6
1
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.

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.
[doublepost=1518454962][/doublepost]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
 

DennisBlah

macrumors 6502
Dec 5, 2013
485
2
The Netherlands
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 ;-)
 

Suikoden

macrumors newbie
Original poster
Feb 11, 2018
6
1
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.
 

DennisBlah

macrumors 6502
Dec 5, 2013
485
2
The Netherlands
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
 

Suikoden

macrumors newbie
Original poster
Feb 11, 2018
6
1
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

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
 
Last edited:

DennisBlah

macrumors 6502
Dec 5, 2013
485
2
The Netherlands
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
 
Last edited:

Suikoden

macrumors newbie
Original poster
Feb 11, 2018
6
1
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.
 

DennisBlah

macrumors 6502
Dec 5, 2013
485
2
The Netherlands
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.

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.
 
Last edited:

Suikoden

macrumors newbie
Original poster
Feb 11, 2018
6
1
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.

Oh I see, I was getting what you were saying reversed. Thanks for your help, you are awesome
 

OvidKing

macrumors newbie
Nov 9, 2019
1
0
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
Hey!

I just came across this post, I know it’s a year old but I’m running into the same issue as you. I run only macs and every option I’ve found on this is for PC and uses HOTKEY. I’ve also read that others have tried using a virtual console on their Mac running Windows but they say that it’s laggy as hell. Someone else said that they tried boot camp and again, same laggy issue.

I do have a laptop older pc but it’s stuck on windows 7 and PS4 remote play minimum is at 8.1 Windows. This all seems like a huge headache to be honest. I was reading your convos on this post but you never mentioned it you got it to work.

I’m wondering if you found a solution? Or better yet, you could possibly share your findings for the 1000 jump rope trophy script? I’m not worried about the 10K enemies trophy... I have a arcade stick with a turbo button option, a small desktop oscillating fan attached to the joystick and I’m in business.

Fingers crossed this message goes to your email.

Cheers mate..
 
Last edited:

dbean104

macrumors newbie
Jan 15, 2023
1
0
Hi,

I too was looking for an answer to this problem and to find an alternative to AutoHotKeys and get this trophy using a Mac.

I eventually did it using a Python script, heavily borrowing the algorithm from the septomor link posted in the initial query.

I'm afraid it does require you to install python and the pynput module (to send keystrokes), but hopefully it will help somebody. The script and some instructions are available on my GitHub repository here.

Good luck!

Before using python, I did attempt to get something working in Apple Script. However, Apple Script seemed to lack the features require to implement the algorithm in full, and the results were a bit hit and miss. With the script below, I did manage on one occasion to reach > 700 jumps, but often the same script would fail after far fewer.

Even so, I post my efforts here in case anybody has the inclination to take it further:
AppleScript:
-- define properties
property latency : 0.087
property a : 0.674
property b : 0.529
property c : 0.463
property d : 0.43
property e : 0.379
property g : 0.395
property g2 : 0.396

-- player should be positioned with exclamation mark above head
delay 3
-- try to start the game
tell application "System Events" to keystroke return
delay 2
-- accept
tell application "System Events" to keystroke return
delay 2

set i to 1
set currentInterval to a
set extraTime to 0
repeat until i > 1001
    tell application "System Events" to keystroke return
    if i > 1 then
        delay currentInterval + extraTime
    else
        delay currentInterval + latency
    end if
    set extraTime to 0
    set i to i + 1
    if i = 20 then
        set currentInterval to b
    else if i = 50 then
        set currentInterval to c
    else if i = 100 then
        set currentInterval to d
    else if i = 200 then
        set currentInterval to e
    else if i = 260 then
        set extraTime to 0.02
    else if i = 300 then
        set currentInterval to g
    else if i = 500 then
        set currentInterval to g2
    else if i = 600 then
        set currentInterval to g
    else if i = 800 then
        set currentInterval to g2
    else if i = 900 then
        set currentInterval to g
    end if
end repeat
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.