applescript oneliner - applescript

Is it possible to join applescript lines into one (in ruby it can be done using ;)?

Not really. The most that can be done is to take a simple if-then statement and make it into one line...
if (variable) then
return true
end if
...becomes...
if (variable) then return true
If you were to include the osascript command in a shell script, then multiple line scripts must delimited with -e...
osascript -e 'if (variable) then' -e 'return true' -e 'end if'
But that's about the extent of it. Applescript files aren't straightforward text files like most other programming languages (unfortunately) and we have to rely on its specialized editors for line management.

It depends on your code.
When you use AppleScript for GUI scripting, you can often write a bunch of nested tell blocks as one line.
For example these nested tell blocks:
tell application "System Preferences"
activate
end tell
tell application "System Events"
tell application process "System Preferences"
tell window "System Preferences"
tell scroll area 1
tell button "General"
click
end tell
end tell
end tell
end tell
end tell
They could also be written as:
tell application "System Preferences" to activate
tell application "System Events" to tell application process "System Preferences" to tell window "System Preferences" to tell scroll area 1 to tell button "General" to click

If you really want to avoid -e and force everything on one line you can push everything through echo
osascript -e "`echo -e \"tell application \\"MyApp\\"\nactivate\nend tell\"`"
Where " become \\" and newlines become \n.

I have re-arranged AppleScript from a block format, to a single line format as such:
Block format
tell application <application>
activate
open location <url>
end tell
Single line format
osascript -e "tell application \"<application>\" to activate & open location \"<url>\"";

Related

AppleScript's tell statement unconditionaly opens firefox, ignoring enclosing if block

I'm experiencing the following issue
the script
osascript <<EOF
if false is true
tell application "Firefox" to activate
end if
EOF
will open/activate Firefox unconditionally, ignoring the enclosing if block. Any other statement inside the if block is (correctly) ignored.
If the application to open is anything But firefox, the statement is correctly ignored.
I.E - the following statement is ignored correctly
tell application "calculator" to activate
The issue is not exhibited when running from Script Editor, only via osascript
Is Firefox exclusively handled somehow by AppleScript? How do I overcome this behavior?
Firefox does not support AppleScript in that it does not contain an AppleScript dictionary file, no e.g. Firefox.sdef in its Resources folders within its application bundle, and only supports some of the basic commands of the Standard Suite.
To keep Firefox from opening with the code shown in your OP, you'll need to tokenize (set as a variable) its name, as in the following example:
osascript <<EOF
if false is true
set appName to "Firefox"
tell application appName to activate
end if
EOF
Or:
osascript <<EOF
if false is true
set |Firefox| to "Firefox"
tell application |Firefox| to activate
end if
EOF

applescript, included do shell script not working

I'm not a developer but try to use applescript to do some work for me.
I have a small script to find a application and kill it.
I have read many articles but not found a solution.
tell application "System Events"
set x to first process whose name is "Blotter"
return unix id of x
end tell
try
do shell script "kill " & x
end try
I get the process id as a result.
tell application "System Events"
get process 1 whose name = "Blotter"
--> application process "Blotter"
get unix id of application process "Blotter"
--> 34990
end tell
Ergebnis:
34990
But I'm not able to kill it...
I would be really grateful for a tipp. Thanks
Try this instead:
tell application "System Events"
set proc to first process whose name is "Blotter"
set procID to unix id of proc
end tell
try
do shell script "kill " & procID
on error errstr
display alert errstr
end try
by using return you' rent getting the unix id of the process, you're merely ending the script. Put the unix id in a variable, and then use that variable in your do shell script.

Switching between two Mac terminal windows

I have an interactive bash script running in a Mac OSX bash Terminal window. I would like, from within that script, to
open a second Terminal window, print in it the content of a variable from the script in the first window,
keep that second window open somewhere on the screen while I continue interacting with the first window, and finally
have the second window closed when I do not need it anymore.
Since I am on Mac OSX, I am thinking of using osascript to run Applescript commands opening the second window, pasting the variable content in it and returning control to the first window, but I cannot make it work.
#!/bin/bash
var2print="I want this to print in the text window"
osascript -e '
tell application "Terminal"
tell window 1 # this just renames the first window
set custom title to "Main window"
end tell
do script # this opens a new window
tell window 1
set custom title to "Text window"
set selected to true # my first idea to put focus on this window
activate # my second idea to put focus on this window
end tell
end tell
'
printf "%s\n" "$var2print" # prints in main window, despite all efforts
read -sn 1 -p "Press any key to continue..."
Surprisingly to me, the very last command 'read' also takes place in the main window, but the focus is on the text window and I have to manually select the main window to press a key and end the script.
I have considered letting go of AppleScript and using the gnu-screen command instead, but it seems like overkill for my purpose to simply have some info displayed for a while.
Any help to better understand what's going on and to find a practical solution to switch between terminal windows would be greatly appreciated. W.
You can toggle two windows in Terminal.app with AppleScript this way
tell application "Terminal"
set index of window 2 to 1
end tell
window 1 is always the frontmost window
How about using a dialog box to display your message and go away automagically after a few seconds like this:
#!/bin/bash
bashvar="ZippedyDooDah"
osascript >/dev/null 2>&1 <<EOF
tell application "System Events" to display alert "$bashvar" buttons {"OK"} as warning default button "OK" giving up after 5
EOF
You can do it like this:
osascript -e 'tell app "Terminal" to do script "echo hello"'
Or, you can set a bash variable like this and send that for display:
MSG="FreddyFrog"
osascript<<EOF
tell app "Terminal"
do script "echo $MSG"
end tell
EOF
If you want to send it more than one message, you could make it tell you its tty as the command you pass, then you will have that in a file and you can send it further messages...
osascript -e 'tell app "Terminal" to do script "tty > tty.txt"'
If you now look in the file tty.txt you will see the device special file of the terminal window you created, some thing like /dev/ttys002, then you can do this from your original window
echo "Some stuff" > /dev/ttys002
echo "More stuff" > /dev/ttys002
or, more succinctly
echo hello > $(cat tty.txt)

How can I make my Applescript 'do' a script in my custom window?

I'm working on writing an Applescript that gets my Terminal ready for me to make Firefox add-ons.
tell application "Terminal"
do script "cd Public/addon-sdk-1.0"
do script "source bin/activate"
do script "clear"
end tell
When I run this script, my custom Terminal opens along with a regular Terminal window; and the bash script is ran in the regular window.. So, I'm trying to find out how to make the Applescript only open my custom Terminal, and execute the bash script in it.
The answer to your problem is to not use do script but to send keystrokes to your current terminal window with either keystroke or key code. Here's a script I use to do something similar. I just call this from the terminal with osascript myscript.scpt or launch it directly (I use LaunchBar for invoking applescripts) and it opens a new terminal tab (if the terminal is already open), gives it a custom name and then runs whatever commands I feed it. You could modify this to skip creating a new tab and just run in the current terminal window. I only use this approach when I have to do more than just run some standard terminal commands (such as send keys to an interactive python session), otherwise I just create a bash script.
global ENTER_, ESC_
set ENTER_ to 52
set ESC_ to 53
on run_commands(commands, pause)
tell application "System Events"
repeat with cmd in commands
keystroke cmd
key code ENTER_
delay pause
end repeat
end tell
end run_commands
on new_terminal_tab(tab_name)
activate application "Terminal"
delay 0.5
tell application "System Events"
# create new tab
keystroke "t" using {command down}
delay 0.5
# give it a name
keystroke "I" using {shift down, command down}
keystroke tab_name
delay 0.5
key code ESC_ # escape
end tell
end new_terminal_tab
new_terminal_tab("addon-sdk-work")
run_commands( { "cd /Users/username/Documents/dev/projname",¬
". env/bin/activate", ¬
"clear"}, 0.5)

osascript / syntax error: Expected end of line but found command name. (-2741)

I'm running into problems with a shell script that utilizes a small portion of Applescript. When I compile it with Applescript editor it works. It does not though within a shell script.
44:49: syntax error: Expected end of line but found command name. (-2741)
23:28: syntax error: Expected end of line but found “after”. (-2741)
Here is the shell code:
osascript -e 'tell application "System Events" -e 'activate'
osascript -e 'tell process "Application 10.5" -e 'set frontmost to true' -e 'end tell'
osascript -e 'delay 1' -e 'keystroke return' -e 'delay 1' -e 'keystroke return'
end tell
Applescript (that works):
tell application "System Events"
activate
tell process "Application 10.5"
set frontmost to true
end tell
delay 1
keystroke return
delay 1
keystroke return
end tell
[updated] / [solved]
This took care of any kind of problems I was having trying to modify the applescript to work within a shell script:
## shell script code
echo "shell script code"
echo "shell script code"
## applescript code
osascript <<EOF
tell application "Scriptable Text Editor"
make new window
activate
set contents of window 1 to "Hello World!" & return
end tell
EOF
## resume shell script...
It's very cool that you're able to put pure applescript directly into a shell script. ;-)
Each osascript(1) command is completely separate process, and therefore a completely separate script, so you can’t use state (such as variables) between them. You can build a multi-line script in osascript using multiple -e options -- they all get concatenated with line breaks between them to form the script. For a sufficiently long script, a separate file or a “here document”, as you used in your eventual solution, is a good way to go.
Also, if your script is mostly (or entirely!) AppleScript, you can make a “shell” script that simply is AppleScript using a shebang file that invokes osascript:
#!/usr/bin/osascript
display dialog "hello world"
...and then use do shell script as necessary.
Instead of using the -e flag, you can simply store your applescript code in a small text file and call
osascript /path/to/script
Also, if you're telling an application or process to do just one thing, you can write it like this:
tell process "MyProcess" to perform action.
Now that I think about it, running each line separately with the -e flag probably won't work because I don't think all the lines will connect and run as one program. For example, I just tested using osascript -e to set a variable. I then used a separate osascript -e to read the variable, and it couldn't.
[*]

Resources