How to write apple script code to automate daily small task? - macos

I have found the applescript code on internet, to start a terminal and launch Elasticsearch
the code is
tell app "Terminal"
do script "elasticsearch-5.5.0/bin/./elasticsearch"
end tell
It works fine,
now I want to add more stuff on it, I need to open 4 more new tab not new window just tab (command + T). and then run different command such as log tail command, start kibana like one after another in each tab.
I am new to applescript and got tired by searching samples and tutorials, can anyone suggest a solution or your idea to achieve the automation.

Terminal's scripting dictionary isn't very good — a lot of 'Apple Event Handler Failed' errors — but you can manage it with code like the following:
tell application "Terminal" to activate
my makeTab("ls -al")
my makeTab("top")
my makeTab("cd ~/Documents")
on makeTab(cmd)
tell application "System Events" to keystroke "t" using {command down}
tell application "Terminal"
do script cmd in last tab of front window
end tell
delay 0.2
end makeTab
Just put whatever commands you want run inside the makeTab() call.

Related

Applescript to change shortcut to be able to clear lines in iTerm2

I am trying to make an applescript to be able to cmd+L to get ctrl+L to clear lines in iTerm as suggested by this answer.
After copying a working, example applescript from here shown below,
tell application "System Events" to keystroke "l" using control down
I am trying to change the application to "iTerm2" as shown below,
tell application "iTerm2" to keystroke "l" using control down
so the shortcut in not global, but I get a Syntax error:
A identifier can’t go after this “"”.
Removing the quotes (this also works in the working example) brings up a different Syntax error:
"A identifier can’t go after this identifier."
What am I doing wrong?
Thanks to #gurkensaas's comment for the correct answer. I post the complete working script below for other people.
tell application "iTerm" to activate
delay 0.1
tell application "System Events"
try
keystroke "l" using control down
end try
end tell
Edit: I ended up using Better Touch Tool for this shortcut since it was much more responsive and fast.

AppleScript running Atom text-editor every time script is run instead of when if statement is true

I'm new to AppleScript and I'm trying to make a simple AppleScript that will pop-up a dialog asking for user input and based on the input it will open one application or another. To do this I've used:
tell application "atom"
activate
end tell
One of the applications is stored on an external hard drive called Tardis. The problem with my code below is that it automatically opens Atom every time I run the script AND the dialog window at the same time. I only want it to open Atom when the input a is given. Thanks for the help! Code is below:
on run {input, parameters}
set inputText to text returned of (display dialog "Options: p - a - j" default answer "")
set p to "p"
set a to "a"
set j to "j"
if (inputText = p) then
tell application "Terminal"
if it is running then
do script "cd Python_Workspace; source ~/tensorflow/bin/activate"
end if
activate
end tell
tell application "Finder" to open POSIX file "/Volumes/Tardis/EXTRA Applications/Sublime Text.app"
end if
if (inputText = a) then
tell application "Atom"
activate
end tell
tell application "Terminal"
if it is running then
do script ""
end if
activate
end tell
end if
if (inputText = j) then
tell application "Terminal"
if it is running then
do script "cd /Java_Workspace"
end if
activate
end tell
tell application "IntelliJ IDEA CE"
activate
end tell
end if
return input
end run
So some applications that support Applescript do - or did, require the application to launch to compile the Applescript at all (if I remember correctly these are applications that support Applescript poorly, which I'm betting is Atom).
This may not be exactly what's happening, especially since if you are launching a compiled Applescript, but maybe you are running a .applescript (text) file.
What I would do, instead of tell application "Atom" to activate (a one line version of what you have), instead: activate application "Atom".
If that does work for you, activate vs tell here's what's going on:
Applescript notices you're talking about Atom. It needs a dictionary to translate your typing into... events.
Applecript can't find the dictionary, so it launches the application, hoping that it can get more information from it when it launches
Atom launches, Applescript realizes that every application supports activate.
Where activate application you're not trying to do anything Atom specific... just activate an app. No dictionary lookup required.
(It's also worth noting that I've been doing this a very long time. I could actually be quoting you information that's a decade or more out of date, or "new" in 10.4 - not 10.14, but 10.4)

Open new Terminal Tab with OS X JavaScript for Automation

I am playing around with JavaScript for Automation on OS X Yosemite.
I am trying to open up a new tab in the terminal application. Here is what I've got so far:
var Terminal = Application('Terminal);
var Tab = Terminal.Tab;
// Activate the Terminal App, creates a new window if there isn't one already
Terminal.activate();
// This contains all the windows
Terminal.windows;
// This contains the first window
Terminal.windows.at(0) // alternatively, Terminal.windows[0]
// This contains the tabs in the first window
Terminal.windows.at(0).tabs
The Terminal.windows.at(0).tabs is essentially an array. It has a .push method. I assumed that I could use the following statement to add a tab to the window:
Terminal.windows.at(0).tabs.push(new Tab());
but it throws a very general error:
Error -10000: AppleEvent handler failed.
The documentation is severely lacking and I'm thinking that this JavaScript for automation thing was just a gimik to get JavaScript developers onboard.
Note: I've seen AppleScript solutions that essentially just tell the System Events Application to press Command + T to open up a new tab. That feels very hacky and makes Command + T hardcoded in there.
the following code works for chrome and safari, but not work for terminal, I am still figuring out the reason, see if this info helps.
chrome = Application("Google Chrome")
newTab = chrome.Tab()
chrome.windows[0].tabs.push(newTab)
see if the following work in your case:
var system = Application('System Events');
var terminal = Application('Terminal');
// tell application "Terminal" to activate
terminal.activate();
// tell application "System Events" to tell process "Terminal" to keystroke "t" using command down
system.keystroke('t', {using: 'command down'});
You can emulate the shortcut for one new tab. Also need declare the target tab
tell application "System Events" to keystroke "t" using {command down}
Look the example with two or more tabs
teel application "Terminal"
do script "cd ~/ && ls" in tab 1 of front window
tell application "System Events" to keystroke "t" using {command down}
do script "cd ~/Applications && ls" in tab 2 of front window
end tell

Getting Mac OS Speakable Items to simulate a keystroke to an application

I'm trying to configure my Mac so that I can use my voice to control page turning (e.g. while exercising).
Based on a combination of http://alvinalexander.com/apple/mac-voice-speech-recognition-software-commands-custom and Typing with Applescript I created a file called Next Page.scpt in the directory of speakable items with the contents:
tell application "System Events" to tell process "Kindle" to keystroke " "
The software is recognizing my "Next Page" voice prompt, as it echo's back that command name, but I'm not getting the effect of typing a space. Nothing is apparently happening and I have no idea how to debug. Prior to issuing this command, I've switched to Kindle itself.
As an aside, the rest of the files in the speakable events directory are XML files and not simple applescript files. I have not tried figuring out and adopting the XML format.
You could try
tell application "Kindle"
activate
tell application "System Events" to keystroke " "
end tell
or sometimes keystroke can be effectively replaced with key code, in your case SPACE would be: key code 49
List of other key codes here
You can use the Speakable Items capability to define the keyboard commands, as follows (extracted from http://support.apple.com/kb/PH14380):
This same document also discusses how to create commands for other functions, including running scripts.
This works for me, using down arrow:
tell application "System Events" to tell process "Kindle" to key code 125
(key code 124, which is right arrow, should also work; back (back a page) could be up arrow (key code 126) or left arrow (123)

Applescript keystroke not behaving as expected

I'm experimenting with Applescript for the first time, and am trying to build a script to setup my default layout of applications for developing at work. This involves placing applications across multiple Mission Control spaces. My problem at hand is simply moving about the spaces. I found in many posts similar to this that such action could be achieved with
tell application "System Events"
tell process "Finder"
keystroke "1" using control down
end tell
end tell
if the appropriate key binding was in place. I made the Preferences change so I could use control+1 to move to the first MC space. However, running the script doesn't do anything. This is the event log output:
tell application "System Events"
keystroke "1" using control down
end tell
No errors that I can see, but again: new to Applescript. I've tried many variations of this command including wrapping control down in curly braces and wrapping the call to "System Events" inside a call to "Finder" like this
tell application "Finder"
tell application "System Events"
keystroke "1" using control down
end tell
end tell
but the output is exactly the same with no shift in view. I think I'm missing something here...
According to this question's responses, I tried adding in a delay to make sure I wasn't stepping on my own feet with running the script with CMD-r but nothing happens. I hear the sound effect when you try to click out of an important focus window (if that makes any sense), it's a short beep. Am I talking to the applications improperly?
EDIT
Ok I got something working, but I'm a little confused why this is the case.
tell application "Finder"
activate
delay 0.2
tell application "System Events" to keystroke "a" using control down
end tell
This accomplishes what I need, but I have to change the key binding to a letter. I can replicate the error tone by pressing control+1 when Applescript Editor is active. I guess there's a shortcut for AE that uses the key combo. But why is that running when Finder is supposed to be active?
To answer your question, in your working code the difference is that you activate the Finder before you issue the keystroke command. Keystroke commands are always sent to the frontmost application so you must always make sure to activate an application first as you have done.
If control-1 didn't work then I suspect either some Finder command uses that combo or some other application uses that in a global context meaning it intercepts that command no matter which application is frontmost. Otherwise it should work for you.
Finally, I would remove your system events line of code from the Finder tell block of code. There's no reason the tell the Finder to tell system events to perform a command. Just put that line on its own after the "end tell" line.
Good luck.
I've just been working with this - I don't want to activate finder before triggering what should be a global shortcut. Keystroke was not working, however I tried key code and that indeed works:
on run {}
tell application "System Events"
key code {18} using {command down}
end tell
return "success"
end run
There is a list of key codes at this question: https://apple.stackexchange.com/questions/36943/how-do-i-automate-a-key-press-in-applescript
This should work:
tell application "Finder"
activate
end tell
delay 0.2
tell application "System Events"
key code 18 using {control down}
end tell
I found this tool to be a good alternative that does not have that issue.
Installation is as simple as:
brew install socsieng/tap/sendkeys
Usage:
sendkeys send --initial-delay 0 --delay 0.001 --characters 'Hello'
https://github.com/socsieng/sendkeys

Resources