Have a script wait until the last script is complete - shell

I have a script that has two apple scripts that open a new tab and do a command. I want the second apple script to wait until the first apple script's command line action is completed. I would rather not have to only to a sleep for x long, before it continues.
Is there a way that I can do this?
Here is what I have:
osascript -e 'tell application "Terminal" to activate' \
-e 'tell application "System Events" to tell process "Terminal" to keystroke "t" using command down' \
-e 'tell application "Terminal" to do script "cd '$current_dir'" in selected tab of the front window' \
-e 'tell application "Terminal" to do script "./my_script arg1" in selected tab of the front window'
------ Wait until this process is finished -------
osascript -e 'tell application "Terminal" to activate' \
-e 'tell application "System Events" to tell process "Terminal" to keystroke "t" using command down' \
-e 'tell application "Terminal" to do script "cd '$current_dir'" in selected tab of the front window' \
-e 'tell application "Terminal" to do script "./my_script different_arg1" in selected tab of the front window'

You can check the busy property of a window or tab:
tell application "Terminal"
set w to do script "sleep 1"
repeat
delay 0.1
if not busy of w then exit repeat
end repeat
end tell
osascript -e 'on run {a}
tell application "Terminal"
activate
tell application "System Events" to keystroke "t" using command down
do script "cd " & quoted form of a & "; sleep 1" in window 1
repeat
delay 0.1
if not busy of window 1 then exit repeat
end repeat
tell application "System Events" to keystroke "t" using command down
do script "cd " & quoted form of a & "; sleep 1" in window 1
end tell
end run' /tmp

Related

Getting several apps in fullscreen in a row via Applescript

So I am writing a script that would set up my working environment by opening and then getting several apps in fullscreen via a single command line. I am having trouble with the "getting the apps in fullscreen" part, and the script execution is very confusing to me.
My hardware is a MacBook Pro (15-inch, 2017) running on macOS Catalina 10.15.7.
Here is the part of the script that behaves erraticaly when executed:
script1.scpt:
tell application "Sublime Text"
activate
tell application "System Events"
tell front window of (first process whose frontmost is true)
set value of attribute "AXFullScreen" to true
end tell
key code 123 using {control down}
end tell
end tell
When executed while the app (here, Sublime Text) is not in fullscreen via the command osascript script1.scpt, the app is activated and goes fullscreen, but key code 123 using {control down} does not work, without producing any error message or sound.
When executed while the app is already in fullscreen via the command osascript script1.scpt, the app is activated, stays in fullscreen as intended but I get this error message:
execution error: System Events got an error: Can't get window 1 of process 1 whose frontmost = true. Invalid index. (-1719)
Also, key code 123 using {control down} does not work either.
When executed via a shell script calling script1.scpt while the app is not in fullscreen, the app behaves exactly as in case 1.
When executed via a shell script calling script1.scpt while the app is already in fullscreen, the app behaves exactly as it should and everything seems fine. But if I lengthen the script to get 2 apps in fullscreen in a row as in script2.scpt (see below), the 2nd app does stricly nothing, and I get the error message of case 2 again.
script2.scpt:
tell application "Sublime Text"
activate
tell application "System Events"
tell front window of (first process whose frontmost is true)
set value of attribute "AXFullScreen" to true
end tell
key code 123 using {control down}
end tell
end tell
tell application "Skim"
activate
tell application "System Events"
tell front window of (first process whose frontmost is true)
set value of attribute "AXFullScreen" to true
end tell
key code 123 using {control down}
end tell
end tell
I have been spending hours trying to understand what is going on without success, so any help or clue would be very welcome.
The only way I found to get things working is to separate the code in separate steps with 0.5sec pauses in-between them, like in the following shell script code block. But I would like to find a faster and "cleaner" solution to my problem.
osascript -e "tell application \"Sublime Text\" to activate" ;
sleep 0.5 ;
osascript -e "tell application \"System Events\"" -e "tell front window of (first process whose frontmost is true)" -e "set value of attribute \"AXFullScreen\" to true" -e "end tell" -e "end tell" ;
sleep 0.5 ;
osascript -e "tell application \"Skim\" to activate" ;
sleep 0.5 ;
osascript -e "tell application \"System Events\"" -e "tell front window of (first process whose frontmost is true)" -e "set value of attribute \"AXFullScreen\" to true" -e "end tell" -e "end tell" ;
sleep 0.5 ;
osascript -e "tell application \"Finder\" to activate" ;
sleep 0.5 ;
osascript -e "tell application \"System Events\"" -e "tell front window of (first process whose frontmost is true)" -e "set value of attribute \"AXFullScreen\" to true" -e "end tell" -e "end tell" ;
sleep 0.5 ;
osascript -e "tell application \"Terminal\" to activate" ;
sleep 0.5 ;
osascript -e "tell application \"System Events\"" -e "tell front window of (first process whose frontmost is true)" -e "set value of attribute \"AXFullScreen\" to true" -e "end tell" -e "end tell"'
EDIT: For the moment being, I managed to get my script doing what I wanted it to do pretty reliably. Not completely satisfied though, because I still have to rely on fixed delays and this makes the execution a bit slow. This is what the code looks like:
in .bash_profile:
cd ~/<path>/ ;
osascript workspace_on.scpt
workspace_on.scpt:
tell app "System Events" to tell process "Terminal" to set the value of attribute "AXFullScreen" of window 1 to true
delay 0.6
tell app "Sublime Text" to activate
delay 0.6
tell app "System Events" to tell process "Sublime Text" to set the value of attribute "AXFullScreen" of window 1 to true
delay 0.6
do shell script "open -a skim ~/<path>/main.pdf"
delay 1
tell app "Skim" to set interaction mode of document 1 to full screen mode
delay 0.6
do shell script "open ~/<path>"
delay 1
tell app "System Events" to tell process "Finder" to set the value of attribute "AXFullScreen" of window "latex-template" to true
Just to copy-and-paste my most recent comment from above:
“Like you, I've spent a bit too much time figuring out this problem. It's quite a frustrating situation because I had a few light bulb moments of inspiration that, in theory, ought to have worked, and worked well, but ultimately didn't work at all. ¯_(ツ)_/¯ But in the process, I've at least written out a naive implementation that incorporates conditional checks and operates on each app one-by-one. It's not what I had aimed for in my head, but it works. See below.”
set Apps to {"TextEdit", "ManOpen", "Usenapp", "QuickTime Player"}
tell application id "com.apple.SystemEvents"
set dock to list 1 in the process named "Dock"
repeat with A in the Apps
tell my application named A to activate
set _P to (a reference to the process named A)
set _W to (a reference to _P's front window)
set _B to (a reference to the value of _W's ¬
attribute "AXFullScreenButton")
set _M to (a reference to (_P's menu bar 1's ¬
menu bar items's menu 1's menu items ¬
whose name = "Enter Full Screen"))
tell (a reference to (the dock's ¬
first UI element whose ¬
name = A)) to if ¬
exists then click
tell _W to repeat 60 times -- 60 x 0.5 = 30 seconds
delay 0.5
if it exists then
click _B
if (the value of attribute ¬
"AXFullScreen") ¬
then exit repeat
end if
end repeat
end repeat
end tell
System information:
AppleScript version: 2.8 system version: 12.6 ("Monterey")

How to automate program execution using bash shell

I wrote a program that I want to execute in 30 terminal tabs.
So I have this shell program:
for i in {1..29}
do
osascript -e 'tell application "Terminal" to activate' -e 'tell application "System Events" to tell process "Terminal" to keystroke "t" using command down'
done
And for every terminal windows just opened I want to do something like:
cd "Folder {i}"
python3 script.py
How can I achieve this?
As you are opening a new tab this becomes the front window, so just tell the front window to execute a script, changing the folder as necessary.
for i in {1..29}; do
osascript -e 'tell application "Terminal" to activate' -e 'tell application "System Events" to tell process "Terminal" to keystroke "t" using command down' -e "tell application \"Terminal\" to do script \"cd folder$i ; script.py\" in window 1"
; done
Hope this helps

How to execute commands in new tabs?

I want to start two different databases but want to keep each process running in a separate tab in a terminal. How do I do this within a shell script? I currently have the following code:
#! /bin/bash
mysqld &
redis-server
Each database needs its own tab. This is on OSX.
osascript -e 'tell application "Terminal" to activate' -e 'tell application "System Events" to tell process "Terminal" to keystroke "t" using command down' -e 'tell application "Terminal" to do script "mysqld" in selected tab of the front window'
(based on https://stackoverflow.com/a/7177891/1566267)
The similar command is for redis-server.

Applescript–execute multi line code

I have some apple script code:
tell application "System Events"
key code 97
end tell
How do i write the code as a osascript -e command in Terminal?
Everytime I try using \n or the such, I get errors. Sorry if I'm not being specific enough.
You have a couple of options:
Pass each line of the AppleScript code as a separate -e option:
osascript -e 'tell application "System Events"' -e 'key code 97' -e 'end tell'
Pipe the AppleScript code to osascript's STDIN:
osascript <<END
tell application "System Events"
key code 97
end tell
END
Oh, and you can also save AppleScript code as an executable shell script. Just add #!/usr/bin/osascript at the top of the code and save it as a plain text file:
#!/usr/bin/osascript
tell application "System Events"
key code 97
end tell
Other example:
open -a Terminal && \
sleep 2 && \
osascript -e 'activate application "Terminal"' -e 'tell application "System Events" to keystroke "q" using command down'
the first two lines are just to show the final goal, which is focus the Terminal window and quit it, sending Command+q
Actually -e option accepts new lines:
osascript -e '
tell application "System Events"
key code 97
end tell'

applescript to osascript conversion?

I am using max msp to run shell commands, I have been prototyping code in applescript and need them to run in osascript for example -
tell application "Google Chrome" to close tab 1 of window 1
converts to
osascript -e 'tell application \"Google Chrome\" to close tab 1 of window 1'
I have converted around 10 commands, but am stuck on the very last one which is
tell application "Google Chrome" to activate
tell application "System Events"
tell process "Google Chrome"
do shell script "/usr/local/bin/cliclick/ c:360,550"
end tell
end tell
which I think goes to
osascript -e 'tell application \"Google Chrome\" to activate' -e 'tell application \"System Events\" to tell process \"Google Chrome\" to do shell script \"/usr/local/bin/cliclick c:360, 550\"'
cliclick lets you use the mouse through shell. http://www.bluem.net/en/mac/cliclick/. The c is the command identifier for clicking, so at x360 y550
is my syntax correct? it works when I dont include the c identifier.
Thanks
I tried this and it gave me this error message:
99:151: execution error: System Events got an error: Invalid argument “360,” to command “c”: Expected two coordinates, separated by a comma. Example: “c:123,456” (1)
Solution: you have an extra space before the number 550 (c:360, 550) and the second value gets lost. Remove the space character and it should work (c:360,550)…
Here is a version where I also changed the quoting:
osascript -e "tell application \"Safari\" to activate" -e "tell application \"System Events\" to tell process \"Safari\" to do shell script \"/usr/local/bin/cliclick c:360,550\""

Resources