Applescript keystroke not behaving as expected - applescript

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

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.

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

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.

How can I use AppleScript to address dialog box in Ableton Live?

I have a collection of Ableton Live files (extension ".als") that I need to cycle through while playing a show. I'd like to dedicate a keyboard shortcut to launch each one, and had intended to use AppleScript for this.
The issue is that each file gets changed as I go through the process of playing the associated song, so that when I press the keyboard shortcut to launch the .als associated with the next song in my set, Ableton opens the "Save changes before closing?" dialog box (at which point what I want to do is select "Don't Save").
Simply pressing command + D at this point will do the trick, but I'd really like to automate this keypress. I can't seem to figure out how to get applescript to do this. I'm an applescript noob, and clicking the "Open Dictionary" option in AS seems to show that Ableton is not officially a scriptable app.
Any thoughts on this? Here's an example of an AppleScript I've been trying. This starts the process of opening the next .als in my set list, but won't click the "Don't Save" button.
tell application "Finder"
activate
open document file "Song 1.als" of folder "Desktop" of folder "User" of folder "Users" of startup disk
end tell
tell application "System Events"
keystroke "d" using command down
end tell
Interesting!
Finally came across tips that made it work:
Add both the Script Editor and Ableton Live to the Accessibility API:
System Preferences > Security & Privacy > Privacy...
Ignore application responses to continue the script during dialog.
LiveLoader.scpt:
-- open file
ignoring application responses -- don't wait for user input
tell application "Ableton Live 9 Suite" to open "Users:username:Desktop:LiveSet Project:LiveSet.als"
end ignoring
-- use delay if needed
-- delay 0.5
-- skip saving file
tell application "System Events"
set frontmost of process "Live" to true
key code 123 -- left
key code 123 -- left
keystroke return -- enter
end tell
Note:
Consider possible security impact.
Perhaps simply disable apps in Privacy List after use. (Could be scripted ;)
Can now also send mouse clicks, for more creativeness. :)
I know this is old. but in the interest of helping others who might find themselves here... heres what i have done.
use a program call Qlab. the free version will be fine.
make an applescript Cue. go to the 'trigger' tab. select midi trigger. hit the midi key you would like to assign the command too. this cue will now launch when it receives this midi note - even when running in the background.
go to the 'script' tab. copy and paste the script below.
you can make the relevant adjustments for each song. Basically each key will close all current ableton files without saving - as requested. and then launch a specific live set. which ever one you have assigned. in this case, the song 'Less Than Nothing'
the code...
tell application "System Events"
set frontmost of process "Live" to true
keystroke "q" using command down
tell application "System Events" to keystroke (ASCII character 28) --left arrow
tell application "System Events" to keystroke (ASCII character 28) --left arrow
keystroke return
end tell
delay 2.0
do shell script "open '/Users/CamMac/Desktop/Less Than Nothing 2 .als' "

How do I invoke enter with keystroke in AppleScript so Web Clipper will save a web page to Evernote?

Lately I have discovered Automator (yes, finally she did that) and I wanted to make an event which runs at a regular interval using Calendar. I wanted to save a particular web page to Evernote using the Web Clipper extension in Safari.
I set up my Web Clipper to start on $, F = Full page, enter = Save.
I have come so far as to actually have an event which works by:
Creating a new document in Automator of the type Calendar
Adding "Get Specified URLs" with the URL I want
Adding "Display Webpages"
Adding "Run AppleScript" with the following code - I'm a total noob at AppleScript so you might say I could have done it in a better way ... then do tell ;) ...:
tell application "Safari" to activate
delay 5
tell application "System Events"
keystroke "$" -- key code 10 = Activate Web Clipper (custom shortcut)
end tell
delay 1
tell application "System Events"
keystroke "f" -- key code 3 = Full page saved by Web Clipper
end tell
tell application "System Events"
key code 36 -- works to save page, however, 'keystroke enter' does not
end tell
5.Saving the document in a Calendar event and set it up to repeat.
I found some help here with a list of key code values, however, I couldn't find "enter" in the list. I used a little free app called Key Codes instead to figure out that enter has the key code 36.
I would rather be able to use keystroke, since it is easier to read than some number. Can anyone help?
i'm not sure this will do exactly what you want but maybe just keystroke return ?
tell application "Safari" to activate
delay 5
tell application "System Events"
keystroke "$" -- key code 10 = Activate Web Clipper (custom shortcut)
delay 1
keystroke "f" -- key code 3 = Full page saved by Web Clipper
keystroke return
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)

Resources