I'm a new user to Applescript and programming in general.

I'm trying to create a script that will 1) load a Renpy project through the terminal and 2) then move the resulting project window to a different monitor using a keystroke command in the application Display Maid.

I can get two different scripts that run successfully on their own:

do shell script "/Applications/renpy-7.3.5-sdk/renpy.sh /Users/username/Documents/Renpy\\ projects/projectname"


tell application "Display Maid"
    tell application "System Events" to keystroke "r" using {control down, command down}

end tell

When I put these together into one script, however, it never gets to the second step. It seems like Applescript wants to wait until the shell script is fully finished before getting to the Display Maid part.

How do I get this to work? I've also given the resulting app accessibility permissions through System Preferences, but that doesn't change anything.

  • Interesting one. I actually use this to my advantage on a script - set things up/launch/wait until quit/tidy up afterwards. I've never considered how you would actually prevent that wait. – Tetsujin Mar 30 at 8:12
  • Hi, probably just adding & to run the *sh script in the background. Add a sleep 1s in the middle (wait a second) to give some time to start to process the command... – Hastur Apr 3 at 16:49
do shell script "/Applications/renpy-7.3.5-sdk/renpy.sh ~/Documents/Renpy\\ projects/projectname 2>/dev/null 1>&2 &"

tell application "System Events"
    set _P to a reference to process "renpy"
    set _W to a reference to window 1 of _P

    repeat 20 times -- 10 seconds max. wait
        if _W exists then exit repeat
        delay 0.5
    end repeat

    if not (_W exists) then return
    set _P's frontmost to true

    -- Display Maid's global hotkey to restore window layout
    keystroke "r" using {control down, command down}
end tell
| improve this answer | |
  • Isn't that my answer? – harrymc Apr 9 at 10:19
  • You would do well to read our site rules, where the most important is "be nice". To answer only the relevant parts in your comment: My answer predates Hastur's comment. You would also do well to read all comments and answers before posting yours. Better also read the post well, because the problem here is that the "do shell" is blocking, all the other stuff is working well for the poster. – harrymc Apr 9 at 12:58
  • CJK - "isn't your answer hastur's answer?" Check date stamps before making accusations, & try to resist the temptation to get up in the face of high rep users. They got that rep through hard work. Reputation, like respect, is earned. Please re-read the Code of Conduct you agreed to when signing up to Stack Exchange. BTW, your solution may work [I haven't tested it] but it doesn't win the rep bounty because it contains no explanation of why it should work. – Tetsujin Apr 9 at 17:09
  • I’m really not bothered about rep bounty. I was bothered about providing the most complete, useful and practical answer for the OP to solve their problem. I also don’t care what someone’s rep is, but if he’s going to make an accusation towards me first, he should consider how offensive such an insinuation is and the response that is likely to receive. Feel free to reply. I won’t engage further in childish to and fro. I’ve hopefully assisted the OP with their needs. But good note about providing explanation, which I will come back and do later. Good day. – CJK Apr 10 at 0:03

According to the do shell script bible, AppleScript waits for the shell command to exit before continuing.

You can bypass this by telling the shell command to run in the background, and by suppressing the command's stdout and stderr by either of:

do shell script "command > /dev/null 2> file_path &"

do shell script "command > /dev/null 2>&1 &"

The > /dev/null suppresses stdout; 2>&1 suppresses stderr (specifically, sends stderr to the same place as stdout), and the trailing & puts the command in the background.

Using this will return control to your AppleScript immediately with no result, leaving the AppleScript script running in parallel with the shell script in the background.

There is no direct support for getting or manipulating the background process from AppleScript, but see this technical note.

This would make the script into:

do shell script "/Applications/renpy-7.3.5-sdk/renpy.sh /Users/username/Documents/Renpy\\ projects/projectname > /dev/null 2>&1 &

tell application "Display Maid"
    tell application "System Events" to keystroke "r" using {control down, command down}
end tell
| improve this answer | |
  • Nice one Harry! Just for the stupid amongst us (… me ;) could you set that directly into the OP's context so I can see exactly how it would look when joining the two examples with your added line? – Tetsujin Apr 2 at 12:04
  • 1
    @Tetsujin: I can't test anything, so better not try it. – harrymc Apr 2 at 13:04
  • @Tetsujin: Without testing, wouldn't just substituting "command" with his command work? – harrymc Apr 5 at 19:15
  • Sorry, I have no idea. I literally don't understand any of it past the opening " I've never used shell script other than inside an applesscript to launch an app – Tetsujin Apr 6 at 7:50
  • @Tetsujin: It seems to my limited understanding that the posted command would become: do shell script "/Applications/renpy-7.3.5-sdk/renpy.sh /Users/username/Documents/Renpy\\ projects/projectname > /dev/null 2>&1 &". – harrymc Apr 6 at 8:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.