I need to write the following script inline
tell app "Spotify Queue"
addTrack "spotify:track:7sa1xOgT1c8xQfbSL5FrCe"
end tell
like this one
sh('osascript -e \'tell app "Spotify" to playpause\'')
but I can't get it right.
You don't need multilines:
tell app "Spotify Queue" to addTrack "spotify:track:7sa1xOgT1c8xQfbSL5FrCe"
If you do require more than one line, osascript can be used like this:
osascript -e 'tell app "Finder"' -e 'display dialog "Multi-line"' -e 'end'
Related
I have TextEdit open.
I want to enter "hello" in TextEdit, but not by typing it in myself, but using a command from Terminal.
I tried this:
osascript -e 'tell application "TextEdit" to keystroke "hello"'
but it give the following error:
31:48: execution error: TextEdit got an error: Can’t get keystroke "hello". (-1728)
What am I doing wrong?
red_menace is right, this works:
osascript -e 'activate application "TextEdit"'; osascript -e 'tell application "System Events" to keystroke "hello"'
I want to open an app(.app) from a bash shell(.sh using #!/usr/bin/bash) which is located in a folder in /Applications. How do I open it?
If I can open it, can I close it? If yes, how?
Setting your shebang to #!/usr/bin/env bash (which is the preferred way as it's portable), consider the following examples:
Open app:
Utilize open command with the -a option. For instance:
open -a "Safari"
Or, using osascript to execute an AppleScript. For instance:
osascript -e 'tell application "Safari" to activate'
Or a terse equivalent:
osascript -e 'activate app "Safari"'
Close app:
Utilize osascript to execute an AppleScript. For instance:
osascript -e 'tell application "Safari" to quit'
Or a terse equivalent:
osascript -e 'quit app "Safari"'
Note: If bash actually resides in /usr/bin/ on macOS as per your question the above examples will work successfully with the shebang: #!/usr/bin/bash
of course you can do that, please use the terminal command "open".
#!/bin/bash
echo This is a play music script
# play Music from command line in the background
open ~/Downloads/AlanWalker-Faded.m4a &
# wait some seconds
sleep 60
# force quit iTunes
killall iTunes
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'
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\""
I have two very simple OSA scripts to allow logon and logoff of computers in a lab environment. These scripts work flawlessly in Snow Leopard when pushed via ARD, interactively within an ssh session, but they fail on machines running Lion.
Stripped down to its essentials, the logout script looks like this:
osascript -e 'tell application "System Events" to log out'
WORKS when run directly from an interactive shell on a machine
WORKS when pushed from ARD
FAILS with "execution error: The variable out is not defined. (-2753)" when run from an ssh session
WORKS when the script is compiled to an .scpt, then run from ssh (e.g. "/usr/bin/osacript logout.scpt")
The login script is directly based on this. A stripped-down version that exhibits the problem is:
osascript -e 'tell application "System Events" to keystroke "frontend"'
WORKS when run directly from an interactive session
WORKS when pushed from ARD
FAILS with execution error: An error of type -10810 has occurred. (-10810) when run from ssh
WORKS when run as a compiled scpt and run from ssh
Because these scripts work fine interactively, and because they worked fine in all modes in Snow Leopard, I think something must have changed in osascript, but I don't know what, and the error messages aren't very descriptive. Any suggestions would be welcome.
Try escaping the quotes.
So:
osascript -e 'tell application "System Events" to log out'
becomes:
osascript -e 'tell application \"System Events\" to log out'
And
osascript -e 'tell application "System Events" to keystroke "frontend"'
becomes:
osascript -e 'tell application \"System Events\" to keystroke \"frontend\"'
Give that a go and tell us what happens.