Using Applescript to Execute a Complicated Keystroke - applescript

I'm trying to write an Applescript in Automator that will press the left arrow button while holding down control, option, and command. The code I have so far is:
on run {input, parameters}
tell application "System Events"
tell application "Sublime Text 2" to activate
keystroke "left" using {control down, option down, command down}
end tell
return input
end run
However, this is not working. Any suggestions as to how to fix this code?
Thanks!

When using arrow keys you need to target them via key code.
tell application "Sublime Text 2" to activate
tell application "System Events"
key code 123 using {control down, option down, command down}
end tell
ARROW KEY CODES
LEFT: (key code 123)
RIGHT: key code 124)
UP: (key code 126)
DOWN: (key code 125)

You can use any ASCII code, for the arrow keys this will be:
tell application "System Events" to keystroke (ASCII character 31) --down arrow
tell application "System Events" to keystroke (ASCII character 30) --up arrow
tell application "System Events" to keystroke (ASCII character 29) --right arrow
tell application "System Events" to keystroke (ASCII character 28) --left arrow
Links:
Credits
ASCII CODES

Related

Get text from current cursor position From Pages app in Mac

Is it possible to get the text of Pages app, from its current cursor position?
My requirement is like, when user type something in "Pages", I have to show suggestions for the word they are typing.
so I want to find out the current or last word, near current cursor position from "Pages" app.
Either by using AppleScript or Accessibility?
Text is not selected.
I am not looking for "Services" also.
For apps other than "Pages", I used Accessibility and appleScript. but for pages I am not finding any way.
I have also tried below AppleScript, but some reason it works perfectly in "Script Editor", but when I use it in my code, it goes to infinite loop.
tell application "Pages"
activate
end tell
tell application "System Events"
tell application "System Events"
key code 123 using {shift down, command down} -- shift-command-left
end tell
tell process "Pages"
keystroke "c" using {command down}
delay 1
tell application "System Events"
key code 124 -- shift-command-left
end tell
set myData to (the clipboard) as text
return myData
end tell
end tell
If I run this AppleScript in my app, it freeze my Mac only, I have to force quit the Mac to stop it.
This works for me using the latest versions of macOS Mojave and Pages
property theApp : "Pages" -- change value to name of any other application (TextEdit)
tell application theApp to activate
delay 3
tell application "System Events"
tell application process theApp
-- Move the insertion point to the beginning of the previous word.
key code 123 using {option down} -- left arrow key while holding option down
delay 0.2
-- Move the insertion point to the end of the next word. (selects the word)
key code 124 using {shift down, option down} -- right arrow key while holding option and shift down
delay 0.2
keystroke "c" using {command down} -- copies selected wprd
delay 0.2
-- Next 2 key code commands attempt to restore cursor location
key code 124 using {option down} -- right arrow key while holding option down
delay 0.2
key code 123 using {option down} -- left arrow key while holding option down
tell current application to set myData to (the clipboard) as text
delay 4
return myData
end tell
end tell

The keystroke command in applescript can't identify case

tell application "iTerm" to activate
delay 0.1
tell application "System Events" to tell process "iTerm"
keystroke "Lsj!"
end tell
I want know how can i get the right way to enter some character. keystroke command can't identify case.
This should work
set the clipboard to "Lsj!" as text
tell application "iTerm" to activate
delay 0.1
tell application "System Events" to tell process "iTerm"
keystroke (the clipboard)
end tell
wch1zpink's answer should work.
You can also write out the letters individually if needed
keystroke "l" using {shift down} -- uppercase
keystroke "sj!" -- lowercase
That should work
tell application "iTerm" to activate
delay 0.1
tell application "System Events" to tell process "iTerm"
key code 37 using {shift down}
key code 1
key code 38
end tell

How do I a trigger keyboard event in ruby?

I want to trigger certain key-presses like enter, esc and arrow keys. I've googled and am surprised to not be able to find the solution.
EDIT
More specifically, I want to trigger some global keyboard shortcuts through ruby script.
On OS X, you can use AppleScript to do that. Here's an example that performs the keyboard shortcut cmd + alt + ctrl + W
tell application "System Events"
keystroke "w" using {control down, option down, command down}
end tell
For the arrow keys, use key code instead of keystroke:
# Key codes for arrow keys:
#
# LEFT 123
# RIGHT 124
# UP 126
# DOWN 125
tell application "System Events"
key code 123 using {control down, option down, command down}
end tell
You can invoke AppleScript from Ruby by shelling out to osascript:
def osascript(script)
system 'osascript', *script.split(/\n/).map { |line| ['-e', line] }.flatten
end
osascript <<-END
tell application "System Events"
keystroke "w" using {control down, option down, command down}
end tell
END
Sources
Using Applescript to Execute a Complicated Keystroke
https://gist.github.com/dinge/6983008

Applescript Change To Folder Save Dialog

I'm trying to automate opening and saving a file in applescript. I can't seem to get consistent results with the save dialog though. Is it possible to change a save dialog to a specific folder in applescript?
This might help you navigate to a folder once the save dialog is raised:
set the clipboard to "/path/to/your/folder"
tell application "System Events" to tell process "SketchUp" -- I'm guessing on SketchUp name
keystroke "G" using {command down, shift down}
delay 1
keystroke "v" using {command down}
delay 1
keystroke return
delay 1
keystroke return
delay 1
end tell
You can do it and keep your clipboard intact, I think. If your save dialog is in TextEdit, if you last saved something to the desktop, for example, the following would change your destination back to Documents. It's easier just to use ⌘+D for that, of course, but you can use substitute pretty much whatever path you need. If you have a path with a folder having non-AppleScript allowable characters in the path (such as quotes), you can escape each with the backslash ("\") character.
tell application "TextEdit"
activate
try
tell application "System Events"
keystroke "g" using {shift down, command down}
do shell script "sleep 0.2"
keystroke "~/Documents"
do shell script "sleep 0.2"
keystroke return
end tell
end try
end tell

Applescript launching of Spotlight?

I tried to use the following to open the Spotlight search box:
tell application "System Events"
keystroke " " using {command down}
end tell
It simply tries to issue command-space. It does work, but if I save the script as an Application and run it, the Spotlight window shows up and then promptly disappears afterwards.
Why does this happen and what can I do to keep the Spotlight window open?
Alternatively: How do I open the Spotlight search box using Applescript?
Your script opens the Spotlight menu. The keyboard shortcut for opening the Spotlight window is command + option + space...
tell application "System Events" to keystroke space using {command down, option down}
UPDATE: Given your revised answer, I have composed a little script that should do what you want...
set the searchText to the text returned of (display dialog "Enter the name of an item you wish to search for in Spotlight:" default answer "")
tell application "System Events"
keystroke space using {command down}
keystroke the searchText
--[1]
end tell
You can do one of the following things at [1]:
Open the top hit:
keystroke return using {command down}
Move the selection to the first item in the next category:
keystroke (ASCII character 31) using {command down} --down arrow
Move the selection to the first item in the previous category:
keystroke (ASCII character 30) using {command down} --up arrow
Move the selection to the first item in the whole menu:
keystroke (ASCII character 31) using {control down}
Move the selection to the last item in the whole menu:
keystroke (ASCII character 30) using {control down}
Simple add a delay.
tell application "System Events"
keystroke " " using {command down}
delay 5
delay 5
end tell
Or to foolproof this:

Resources